ERROR : Nonexistent function 'size' in base 'Vector2'

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By THE HELIX

I wanted to draw the line/path like in the image

I attached the script ;

    extends Node2D

    var path = []
    var drag_mouse = false
    var circle_size = 30

    func _ready():
      set_process_input(true)

    func _draw():
      if (path.size()):
         for i in range(path.size()):
             draw_circle(path[i] - global_position, circle_size, Color(1, 1, 1))
      pass

    func _input(event):
       if(event is InputEventMouseButton && event.is_pressed() && event.button_index == 1 ):
           path = get_global_mouse_position()
           update()

When i run it and click the mouse on the screen ; I get the ERROR :
" Invalid call. Nonexistent function ‘size’ in base ‘Vector2’. "
Can you please help me solve it ?

:bust_in_silhouette: Reply From: StoneForged

I may be wrong but you are changing path from in array to a vector 2 on the click you need to add the vector2 to the array not make it become the point as size is a function only for arrays. Try something like this and sorry if that makes no sense or doesn’t work.

var path = PoolVector2Array()
var drag_mouse = false
var circle_size = 30

func _ready():
  set_process_input(true)

func _draw():
  if (path.size()):
     for i in range(path.size()):
         draw_circle(path[i] - global_position, circle_size, Color(1, 1, 1))
  pass

func _input(event):
   if(event is InputEventMouseButton && event.is_pressed() && event.button_index == 1 ):
       path.append(get_global_mouse_position())
       update()`

I got rid of the error. Thank you so much.
How can connect the points by dragging and clicking them.

THE HELIX | 2019-06-24 02:41

Sorry for the delay the line 2d node can draw a line in between the points just add the script

$Line2D.points = path.points

I haven’t really used this method so may not work.

StoneForged | 2019-06-24 20:09