im not sure if the shape_floor variable is referencing to this node or the Polygon2d?
i made 2 ways that will work to display it (at very least)
red one is method 1
green one is with method 2
the stage.gd code
extends Node2D
var shape_floor
var shape_floor2
# Called when the node enters the scene tree for the first time.
func _ready():
#method1
var temp=Shape.new()
shape_floor=temp.rect(300,300,200,200)
add_child(shape_floor)
#method2
var shape_floor2=Shape.new().rect2(500,500,100,200,self)
Nice, the method 2 is what I was looking for.
There’s only 1 thing I’d want to improve. I’d like the 5th argument “self” to be a default but I can’t achieve it.
I don’t get it…
When I use the default 5th argument “node = self”, the function is not receiving the correct node as it does when I specify self as 5th argument in the function calling.
but it will still need to do something like
Shape.new(self) to create the Shape
but the rect method will stay with 4 parameters
the stage.gd code
extends Node2D
var shape_floor
var shape_floor2
# Called when the node enters the scene tree for the first time.
func _ready():
##method1
#var temp=Shape.new()
#shape_floor=temp.rect(300,300,200,200)
#add_child(shape_floor)
#method2
var shape_floor2=Shape.new(self).rect2(500,500,100,200)
Isn’t there a way to avoid using the “self” in any form when calling the function? some way to make the function to receive the node without having to send it manually…
mmm. that sounds like more trouble than using the self as argument…
What about sending by default the node path instead of the node itself? could it be set as default that way?
I mean, my ideal is to make the calling function as clean as possible and intuitive tu use to make custom shapes
Nevermind. I’m going with the _init(parent) solution.
Thanks
the problem here, the Shape script is not attached to any node, despite it extends from Node. So you dont really can do much with it, anything with this has to be initialized by .new() like an object or RefCounted do, or need to attached the Shape.gd to a real node from code.
the real solution i believe is to make the a Shape Scene, with its root Node being a Node or Node2d, and the child polygon2d is created from code . the Shape Scene with its root node attached with shape.gd will be instantiated and added child on stage Node first before you call .rect methods
another solution is to make this Shape.gd as an Autoload
so you can call it anywhere and it’s alrdy instantiated as a node when the game began
it really all depends how you wanted it to be
also to create multiple shape, you just need to call the rect2 method, dont need to .new(self) for every new shape
this will need you to save the reference of this newly create Shape.new(self) to a Variable first, then for every new shape you want to create, just call this variable.rect2()
I am trying to avoid the use of scenes or autoloads or anything that involves the godot GUI usage. I want this functions to be very portable and use it on every project just by adding this script with no need of configuring anything else.
Yes, the rect2 method you give me is pretty much what I want. It is just a little less intuitive when instantiating the object as it asks you for the self argument.