-
-
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 synchronized variables / pointers without functions #2817
Comments
Objects, arrays and dictionaries are already passed by reference, not by value. Adding pointers to a scripting language is a controversial proposal. And this is a wrong situation when one value is stored in several places and should always be automatically synchronized. The value should be stored in one place, we can only simplify access to it from different places. |
I think your particular problem could be solved with signals. You can also go though Resources documentation. You can create custom resources via See also #1734 for a more specific use case. |
Found a previous proposal which I think shares some similarities: godotengine/godot#6491. |
I agree with them, a Dictionary, or if you want statically typed variables, a custom Resource, would be a better solution that doesn't add any further complexity to the language. You need only pass a reference to the Dictionary/Resource from the parent to the child. Possibilities include...
Here's an example of option 2. # ancestral_data.gd
class_name AncestralData
extends Resource
var some_var := ""
# composite_node.gd
extends Node
export var data: Resource = null
func get_ancestral_data() -> AncestralData:
return data as AncestralData
# This node reinforces the Composite Pattern.
# It, and other types that share an interface with it, will
# will interchangeably perform similar logic seamlesssly.
# In this case, they will uniformly pass along AncestralData
# if a `get_ancestral_data` method is present in the parent
# and they have this notification implemented identically.
func _notification(what):
match what:
# Executes when this node is attached to a parent node
NOTIFICATION_PARENTED:
var p = get_parent()
if p.has_method("get_ancestral_data"):
var parent_data = p.get_ancestral_data()
if parent_data:
data = parent_data Or, in a simpler, less "safe" or "precise" way, just quickly grabbing the parent property, as you were probably doing before. # composite_node.gd
extends Node
export var data: Resource = null
func _enter_tree():
data = get_parent().data |
You could just use setters and getters right?
You can already do this:
|
Describe the project you are working on
Logic game in which many variables of a Node's children (and also their children!) are changed oftenly and need to be updated in the parent Node.
Describe the problem or limitation you are having in your project
I always must do
$"..".some_var = some_var
or call the setter function after assigning a new value and wanting it to be known in the parent.Describe the feature / enhancement and how it helps to overcome the problem or limitation
You could just assign a variable a new value and it automatically updates in other Nodes where it is used as a pointer.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
var *some_var = $SomeChild.some_var
Btw, something like this is possible in C# (adding
*
in front of a function's parameter)If this enhancement will not be used often, can it be worked around with a few lines of script?
No, not directly this.
Is there a reason why this should be core and not an add-on in the asset library?
Yes, it could be used in any project.
The text was updated successfully, but these errors were encountered: