I have a function in a class that runs on call_deferred when a property has been set. However, it is never called if I create a new instance of the class inside another function. Is that supposed to happen? Is it because the instance is local to the function and has been removed before call_deferred is called? Not sure why this should not work.
Is it possible to do call_deferred on objects local to a function?
peterhoglund a function in a class that runs on call_deferred when a property has been set
I don't understand. Could you post the code?
peterhoglund However, it is never called if I create a new instance of the class inside another function.
Does it run if you don't use call_deferred
? Are there any errors? Are you editing the property before or after adding it to the scene tree? If the solution isn't simple, then does it need to be deferred or could it use await
instead?
- Edited
Yes, the function runs if I don't call deferred on it. Here is an example code
The class:
class_name TestClass
extends RefCounted
var prop: int :
set(v):
prop = v
_validate_prop.call_deferred()
func _init(p_prop: int) -> void:
prop = p_prop
func _validate_prop():
print(prop)
and the class in use:
extends Node2D
var test_class = TestClass.new(12)
func _ready() -> void:
var test_class2 = TestClass.new(18)
Running this will only print 12 and not 18. So the validate function is never run in _ready() if it is call_deferred. I guess it is because test_class2
is removed from memory when the ready function is done so it is not alive anymore at the end of the frame.
peterhoglund I guess it is because test_class2 is removed from memory when the ready function is done so it is not alive anymore at the end of the frame.
Yes I'd say that is exactly what is happening.
yeah, make sense. Here is my problem:
I have a three properties in the class, Year, Month and Day. If any of these is changed I want to validate that the date is correct. However, I only want to do this when I know all of them has been set (if they have been set). I don't want to validate whenever any property is set, because if the month is changed from May to June and directly after the day is changed from 31 to 30 this would result in a error since validate is run after the month is set. That is why I want the validation to happen at the end of the frame.
Any idea how this could be achieved?
peterhoglund Provide only a function that sets them all at once.