RMXP / VX / ACE - Draw lines between two points

Version: 0.1.1 (Beta)
Changlog:
  • 2025.12.08 Improved screen size verification, easy color system, anti-lag option.
This script allows you to create colored lines connecting events, or from the player to a map event, eliminating the need to create events in parallel and use game variables.

It's still a script that can be improved.

Possible future improvements:
  • Select colors by ID from a palette (Array)
  • Perhaps increase the line limit (even with the lag issue)
  • Adjusted to work with common events in case a map event does not exist.

License:
  • Both Non-Commercial and Commercial usage is allowed
  • No need to add credits
  • Modifications are released

Download: [RMXP VX VXA] Paint Module.rb

Video:


Instructions:

Create a line between two characters on the map:
Paint.setline(LineID, CharID, CharID, Color, Height Y-Fix)
LineID: 0..7 (To avoid lag, the limit of simultaneous lines is 8)
CharID: A value of 0 will point to the player, while 1 or higher will point to the event on the map.
Color: You can define a custom color or use one of the 8 initial colors, via the color ID or name; the name can be written in lowercase or uppercase letters:
Spoiler: List of colors
"black" or 0
"red" or 1
"green" or 2
"blue" or 3
"yellow" or 4
"fuchsia" or 5
"aqua" or 6
"white" or 7

Y-Fix: The default height is -12 from the base of the character set graph.

Examples:
Enable line 3 to go from the player to the map event with ID 1:
Ruby:
Paint.setline(3,0,1)

Enable line 0 to go from event 2 to event 4 with the color blue:
Ruby:
Paint.setline(0,2,4,Color.new(0,0,255))

Connect line 7 with the standard color, but with graphic 28 higher:
Ruby:
Paint.setline(7,0,4,nil,28)

Delete a specific line:
Paint.eraseline(ID)

Examples:
Delete line 8:
Ruby:
Paint.eraseline(7)

Delete all lines:
Paint.clearlines

Anti-Lag:
This allows for fewer pixels to be drawn, making the game smoother.
Paint::ANTI_LAG = 2 (2 or more)

A brief explanation of how colors work:
The Color class in RGSS functions similarly to other environments, but allows the use of transparency.
The Color.new command is used to return a color based on the values you enter (0~255).

Color.new(Red,Green,Blue)
Color.new(Red,Green,Blue,Transparency)
There are online tools where you can convert colors into values to use in the script.

Here's a website that might help, although MS Paint also does this for color selection:
Hello, very nice script!

May I provide some observations? Feel free to listen to them or not.



I checked the code and saw that you've provided compatibility with other engines, if you want the screen width and height, maybe you can set this code:
Ruby:
class << Graphics
  unless method_defined?(:width)
    def width; Paint::SCREEN_W; end
  end
 
  unless method_defined?(:height)
    def height; Paint::SCREEN_H; end
  end
end #Graphics
That way you can use Graphics.width and Graphics.height just like in vx and ace while still being compatible with any of the three engines.



When I go to another map, the lines persists but this might create a crash if the events with the corresponding ids do not exist.



For the line limit, how much would it cost in performances to separate the bitmap for some lines? That way you could add a check to see if one of the events moved and otherwise not redraw the line?
Maybe creating a user-controlled superset of lines to have static lines vs dynamic? Okay this seems a bit far fetched x)



If you want to turn an array into a list of arguments like you did here:
Ruby:
draw_lines(
  Paint::Lines[i][0],
  Paint::Lines[i][1],
  Paint::Lines[i][2],
  Paint::Lines[i][3]
)

You can use *array an have something like this:
Ruby:
draw_lines(*Paint::Lines[i])
(it works well if your array does not contain more data than just the arguments)
That way you can use Graphics.width and Graphics.height just like in vx and ace while still being compatible with any of the three engines.
I spent over an hour trying to solve this, as the script is made only following the logic of Sprite_Timer, I ended up actually keeping the native resolution of RMXP.
I'll definitely use this. It completely changed my perspective on how to include methods.

(it works well if your array does not contain more data than just the arguments)
Now the use of asterisks that I saw in other scripts makes sense :LZSooo:
A standard color selection system has been included, which can be accessed by a unique number or by the name of one of the 8 listed colors.

An option has been included to set the gap between pixels, which reduces the gap due to the distance between objects or the number of lines drawn:
Example1.png

20251207_235600.png
Maybe you can increase performances by cutting the lines when they are not on the screen?
I'm sure there must be some simple arithmetic to get the intersection between a line and a rectangle.

Otherwise I might have something that works by calculating y = ax+b and then cutting the positions to put them in the screen. If you are interested (note that this "addon" is adapted for your first release of draw line, not the second that I did not check):
Spoiler: Cutting lines
Ruby:
class Sprite_Paint < Sprite
  OFF = 0  #offset value, if > 0 then there should be a gap between a line and
           #the screen limits (just for testing, keep it at 0 otherwise)
  DUMMY_POS = [-10, -10] #out of screen pos to avoid drawing
 
  #return -1 if x below min, 0 if x between min and max and 1 if x above max
  def cmp(x,min,max)
    return -1 if x < min + OFF
    return 0 if x <= max - OFF
    return 1
  end
 
  #return relationship of x,y with screen limits
  def screen_rel(x,y)
    return cmp(x, -1, Graphics.width), cmp(y, -1, Graphics.height)
  end
 
  def in_screen?(x,y)
    screen_rel(x,y) == [0,0]
  end
 
  #adjust the source
  def adjust_src(src, a, b, min = -1 + OFF)
    return src if in_screen?(*src)
    if src[0] < min
      delta = min - src[0]
      src[0] = min
      src[1] += delta * a
    end
    adjust_y(src, a, b)
  end
 
  #adjust the target
  def adjust_tgt(tgt, a, b, max = Graphics.width - OFF)
    return tgt if in_screen?(*tgt)
    if tgt[0] > max
      delta = max - tgt[0]
      tgt[0] = max
      tgt[1] += delta * a
    end
    adjust_y(tgt, a, b)
  end
 
  #pos = [x,y] such that y = ax+b, ensures that y' is on the screen and pos
  #remains on the axis
  def adjust_y(pos, a, b, min = -1 + OFF, max = Graphics.height - OFF)
    if pos[1] < min
      delta = min - pos[1]
      pos[1] = min
      pos[0] += delta / a
    elsif pos[1] > max
      delta = max - pos[1]
      pos[1] = max
      pos[0] += delta / a
    end
    pos[0] = pos[0].round
    pos[1] = pos[1].round #acually necessary because of adjust_src or adjust_tgt
    return pos
  end
 
  #assuming src and tgt are two position on the screen with src.x < tgt.x
  #calc line forumla y = ax+b and cut src and tgt to fill the screen
  def cut_line_axis(src, tgt)
    #now calc ax + b
    dx = tgt[0] - src[0]
    dy = tgt[1] - src[1]
    a = dy / dx.to_f
    b = src[1] - a * src[0]
    #now we know there is an intersection:
    src = adjust_src(src, a, b)
    tgt = adjust_tgt(tgt, a, b)
    #final check: (there remains some position inconsistencies...)
    #unless in_screen?(*src) && in_screen?(*tgt)
    #  puts sprintf("(%d, %d), (%d, %d)", *src, *tgt)
    #end
    return src, tgt
  end
 
  #given src, tgt two arrays of [x,y], return src',tgt' such that they are
  #on the screen within the same axis, if the axis does not cut the screen,
  #return [-10,-10], [-10,-10] to avoid drawing it
  def cut_line(src, tgt)
    return cut_line(tgt, src).reverse if src[0] > tgt[0] #ensure src.x < tgt.x
    return src, tgt if in_screen?(*src) && in_screen?(*tgt) #no need to cut
    return src, tgt if src == tgt #avoid dumb case
    src_rel = screen_rel(*src)
    tgt_rel = screen_rel(*tgt)
    if [0,1].any? {|i| src_rel[i] != 0 && src_rel[i] == tgt_rel[i]}
      return DUMMY_POS, DUMMY_POS #out of bound in same direction = offscreen
    end
    if src[0] == tgt[0] #straight vertical line case
      src[1] = [[src[1], -1 + OFF].max, Graphics.height - OFF].min
      tgt[1] = [[tgt[1], -1 + OFF].max, Graphics.height - OFF].min
      return src, tgt
    end
    return cut_line_axis(src, tgt)
  end
 
  #overwrite to add cut line effect
  def draw_lines(o1, o2, c, fy)
    p1 = (o1 > 0)? $game_map.events[o1] : $game_player
    p2 = (o2 > 0)? $game_map.events[o2] : $game_player
    #modified after this:
    src = [p1.screen_x, p1.screen_y-fy]
    tgt = [p2.screen_x, p2.screen_y-fy]
  
    src, tgt = cut_line(src, tgt)
  
    dx = tgt[0] - src[0]
    dy = tgt[1] - src[1]
    steps = [dx.abs, dy.abs].max
    if steps > 0
      (0..steps).each do |i|
        t = (i.to_f / steps)
        x = (src[0] + dx * t).round
        y = (src[1] + dy * t).round
        self.bitmap.set_pixel(x, y, c)
      end
    end
  end
end #Sprite_Paint

I ran it with around 50 lines and it seems to improve performances when few are on screen.
I had forgotten to test it, and just like in RMXP, images drawn on the screen don't disappear, and since the target events don't exist, the game crashes :/
I fixed it, but I didn't have time to post the fix on Mediafire.

The performance is still quite slow if there are 8 lines drawn, I'm going to apply those functions you created, because my code is turning into a meaningless mess.
Is it possible to draw a line to followers? I tried to draw one with this script call:

Ruby:
Paint.setline(0, 0, $game_player.followers[1], "Red")

And it resulted in a crash. I also tried surrounding the $game_player.followers part with parentheses and it gave the same result. Given that the script specifically says it draws lines between the player and events or between events, I'm assuming it'd be completely different logic for handling drawing lines between party members, or from party members to other events?
Is it possible to draw a line to followers?
Pretty sure the script is not designed for that. But maybe it can be added.
And followers would typically be negative numbers
Paint.setline(1,0,-1,1)
Is it possible to draw a line to followers? I tried to draw one with this script call:
I'll try to think of a way to implement this, but first I need to implement the adjustment that @Timtrack made

And followers would typically be negative numbers
Exactly, and since the total number of followers can be increased by another script, I'll just use a *-1 to make the value positive.

Original thread: https://forums.rpgmakerweb.com/threads/rmxp-vx-ace-draw-lines-between-two-points.181330/