-
-
Notifications
You must be signed in to change notification settings - Fork 21k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot call Callable in a draw method (because no way to set_object on Callable?) #75376
Comments
You're kinda right. This is because the object that the callable is attached to is NOT the object (a CanvasItem) that is currently within its drawing routine ( The error messages like: godot/scene/main/canvas_item.cpp Line 599 in 9b0bee8
can be improved for cases when
Currently you'd need to pass the drawing CanvasItem as the Callable's argument. Like so: class CustomDrawNode extends Node2D:
var draw_func: Callable
func _init(custom_draw_func: Callable):
draw_func = custom_draw_func
func _draw():
draw_func.call(self)
# Then instantiate this as follows
var custom_draw_node = CustomDrawNode.new(
func(ci: CanvasItem):
ci.draw_rect(Rect2(0.0, 0.0, 100.0, 100.0), Color.GREEN)
)
add_child(custom_draw_node) I'd say this is expected.
To me this sounds more like a material for a proposal. |
Marking as bug because of the incorrect / confusing error message. |
Ah yeah, that works perfectly well for my use case! I was kind of stuck on this pattern because in JavaScript, given a function you can say "call it with this object as the 'self' (or Thank you very much for the explanation @kleonc ! |
To replace the owner of a callable you can also do (sorry missed this was a lambda for which this doesn't work) (old and new can be the same, just making clere here): callable_new = Callable(target, callable_old.get_method()).bindv(callable_old.get_bound_arguments()) |
This won't always work. It should work e.g. when |
True! Forgot about lambdas |
Godot version
v4.0.1.stable.official [cacf499]
System information
Windows 11
Issue description
If you try to pass a lambda/callable to be used inside a draw function, you get the error below. As far as I can tell, this is because the object that the callable is attached to is NOT the class that is calling the _draw function, so Godot doesn't recognize that it's actually being called inside
_draw
?Steps to reproduce
I think to fix this I would want to do something like
so that the passed in
custom_draw_func
can be called with theCustomDrawNode
as its self?Minimal reproduction project
N/A
Can be reproduced completely with the snippet above.
The text was updated successfully, but these errors were encountered: