-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
Add a way to detect a change of specific properties in a script #2837
Comments
3.3, both options work for me: func _set(prop: String, value) -> bool:
print("_set: ", prop, " = ", value)
return true
func _ready() -> void:
self.my_var = 42 var my_var: int setget set_my_var
func set_my_var(value: int) -> void:
print("set_my_var: ", value)
func _ready() -> void:
self.my_var = 42 |
could you pls provide a full script example? Can't get it working extends Node2D
var x
func _set(property:String, value)->bool:
print(property, value)
return true
func _ready():
self.x = 10 linux, 3.3, 3.3.2 |
Either use This will work: extends Node2D
# var x
func _set(property:String, value)->bool:
print(property, value)
return true
func _ready():
self.x = 10 |
Thanks, this works As for setters, using them is not a good idea for example when having many script properties. |
It's okay. |
Oh but I can't set the property then? extends Node2D
func _set(property:String, value):
return true
# how to set the property?
func _ready():
self.visible = false
print(self.visible)
--> true |
var armor: int:
set(value):
print("armor setter triggered")
armor = value
func _set(prop: StringName, value):
print(prop, " catched by global setter")
return true
func _ready():
self.armor = 1
self.visible = false
print("armor = ", self.armor)
print("visible = ", self.visible)
|
Use a different variable or store the value in a dictionary.
Yes, this adds some boilerplate code, but it doesn't really pose any particular problems. This approach is used in many other programming languages. In 4.0, we decided to reduce the boilerplate code by replacing getters and setters with properties that are written after a variable declaration, without creating a function.
Yes, we should probably improve the It is better to ask @reduz and @vnen whether to change the behavior of I don't know why built-in properties are handled differently, but in 3.x there is a similar problem with virtual methods, which was fixed in 4.0-dev, and now you need to explicitly call the method in the parent class using the |
I mean when using It seems Weird. |
Describe the project you are working on
2D MOBA game as a hobby and university course project
Describe the problem or limitation you are having in your project
I want to synchronize the object fields on the server with the object fields on the client side and notify the interface of this change. In other words, I want to have a setter for all variables. There are several implementation options, but they all have their pros and cons:
a) Preferred. Inspired by this answer. Does not work (tested in 3.2 and 4.0)
Expectation: a line will be printed for "visible" and two for "armor".
Reality: only the line for "visible" is printed.
Note: You may notice that this solution uses a dictionary as well as the next one, but I can't avoid this in my code, because I need to store variable attributes somewhere, which is a topic for another proposal
b)
Pros: works as expected
Cons: requires large amounts of code when there are a lot of variables, as is often the case in MOBA
c) Inspired by this post.
The "Why is this useful?" section describes the situation quite accurately.
Pros: works as expected
Cons: auto-completion does not work, it is not possible to detect the absence of a variable at the compilation stage, there is no typing, additional code is required to display variables from
stats
for my gd2puml converter (used to build UML class diagrams from existing GDScript code)d) Same thing, only with a class instead of a dictionary.
Pros: works as expected.
Cons: there is no autocompletion and there is no way to detect a mistake in writing the variable name at the compilation stage.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
I would like to change the order of checking for the presence of a variable before calling the
_set
function so that solution a works. I think it will be enough to swap the lines inObject::set (core/object/object.cpp)
. After that, more variables will be passed through_set
, but it already handles the excess, as can be seen in the example withvisibility
.Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Code from solution a will work.
If this enhancement will not be used often, can it be worked around with a few lines of script?
Yes, quite - there are three working solutions, but all of them are not ideal.
Is there a reason why this should be core and not an add-on in the asset library?
The easiest way would be to change this in the core
The text was updated successfully, but these errors were encountered: