0 votes

I have a parent-Node2D with an image; on witch I draw and use setpixel etc.
It has a childe-Node2D, witch is almost the same as it parent.
Now, how can the child use the functions of the parent, so they draw in the child instead?

in Engine by (50 points)

2 Answers

0 votes

By connecting to it's draw signal, so the functions can be called at the right time.

extends Node2D # Parent node

func _ready():
    $child.connect("draw", self, "draw_on_child")

func draw_on_child():
    $child.draw_circle(Vector2(), 30.0, Color.green)
by (5,192 points)

First: Thank you.
Second: Nope I don't get it.
I'm guessing 'drawonchild' tells the 'drawcircle-function in the child-node' to work?
I thought all draw-functions had to be in _draw()?
Well... I fiddled a bit, and now the child can draw circles on the parent. (most parents wouldn't like that! HA!) But I want the child to tell the parent to draw on the child.
I was perhaps not clear enough in my question?
Let's say I have a bunch of functions in some node (parent or otherwise). As a kind of library of useful stuff.
I can use them from other nodes via get
node(someplace). But (in my case(so far)) I can only get them to change data. Not draw stuff in the node that calls them.
I'm still baffled, so probably avencherus's answer is correct.

edit: why is my text oblique? Strange thing are going on...

edit too: I'm sure avencherus's answer is accurate but my question was questionable?.

0 votes

adding a little example:
in the parent;

func point(img:Image, start:Vector2 , color:Color):
img.lock()
img.set_pixel(start.x , start.y , color)
img.unlock()

in the child:

func _ready() -> void:
image = Image.new()
image.create(imageSize.x, imageSize.y, false, imageFormat) 
image.fill(Color(1,0,1,1))
imageTexture = ImageTexture.new()
imageTexture.create(imageSize.x,imageSize.y, imageFormat, 0)    
set_texture(imageTexture)
$parent.point(image,Vector2(10,10) , Color(1,1,0),22)
imageTexture.set_data(image)    

doesn't work...
If I call to a similar function in the child self: it works.
edit: pasted in a bit more code

by (50 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.