Problem drawing a line

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

I have a Node2d ‘BG’ with a single Node2d child ‘Boss’ with a script attached to each. When the sprite attached to ‘Boss’ is moved, I’d like to draw a line from (0,0) to the sprite but my code doesn’t work. Can someone tell me what’s wrong?

Here’s BG.gd:

extends Node2D

func _draw():
    var from = get_node("Boss").get_position()
    var to = Vector2(0, 0)
    print(from)
    draw_line(from, to, Color(0, 0, 128, 1) , 2, true)

func _update():
    CanvasItem.update()

and here’s Boss.gd:

extends Node2D

var screensize
const SPEED = 100

func _ready():
screensize = get_viewport_rect().size


func _process(delta):
if Input.is_action_pressed('ui_right'):
	position.x += SPEED * delta
elif Input.is_action_pressed('ui_left'):
	position.x -= SPEED * delta

position.x = clamp(position.x, 0, screensize.x)

Even though the Boss sprite moves as expected, the line is drawn from (0,0) to the original position and is not updated.

:bust_in_silhouette: Reply From: SIsilicon
  1. If you think _update() is an override able function then think again. It’s supposed to be something like _process(delta) for example.
  2. CanvasItem is something you extend from. You don’t call functions directly from it. You should remove it entirely.

So this,

func _update():
     CanvasItem.update()

Becomes this.

func _process(delta):
     update()

Thanks for that, my head is in a whirl atm :smiley:

picnic | 2018-12-13 21:17