Skip to content
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 varref function to referencing a variable in a instance #3497

Open
ghsoares opened this issue Nov 4, 2021 · 5 comments
Open

Add varref function to referencing a variable in a instance #3497

ghsoares opened this issue Nov 4, 2021 · 5 comments

Comments

@ghsoares
Copy link

ghsoares commented Nov 4, 2021

Describe the project you are working on

ReactGD a plugin to create reactive interfaces with JSX-like syntax

Describe the problem or limitation you are having in your project

In my project, you return a structure from the render function of a ReactGDComponent extended script, which is used to render the node tree. One of the features is to grab the reference of a node like:

extends ReactGDComponent
var btn: Button

func render():
	return <Button text="Click me!" ref=btn/>

which would be parsed into:

extends ReactGDComponent
var btn: Button

func render():
	return {
		"type": Button,
		"properties": {
			"text": "Click me!",
			"ref": varref(self, "btn")
		},
		"children": []
	}

the plugin would use ref passed value of the variable reference to set the variable value to the node reference.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

The function varref would return a VarRef just like funcref returns FuncRef, which would store the instance reference and the variable name string, then it would have functions similar to FuncRef.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

The class VarRef would have the following functions:

get(): Variant Get the referenced variable value
set(Variant val): void Set the referenced variable value
is_valid(): const bool Returns whether the object still exists and has the variable assigned
set_instance(instance: Object): void Sets the instance reference derived from Object of the referenced variable

If this enhancement will not be used often, can it be worked around with a few lines of script?

It requires the creation of a class that stores the instance reference and the variable name and implement the functions needed.

Is there a reason why this should be core and not an add-on in the asset library?

Because it works simillar to FuncRef, which is a core feature, so I don't see a reason to not have VarRef too.

@KoBeWi
Copy link
Member

KoBeWi commented Nov 4, 2021

Because it works simillar to FuncRef, which is a core feature, so I don't see a reason to not have VarRef too.

Actually FuncRef was removed in 4.0 in favor of Callable, which is basically a method pointer. Such concept exists in many languages, while "property pointers" not so much.

Also implementation of VarRef would take less than 30 lines. It could as well be a plugin.

@AaronRecord
Copy link

Aren't all Objects references? I'm not sure I understand.

@ghsoares
Copy link
Author

ghsoares commented Nov 5, 2021

@KoBeWi It could be a addition for 3.5 release, even if it takes small lines of code, it could be useful for some cases like funcref does. But yeah, if not added to core, it can indeed be a small script to add.

@LightningAA If you used funcref before, my proposal would work similar to that, but instead of storing a function pointer in an instance, it would store a variable pointer in an instance, without needing to worry what object and what variable it exactly is, you could set and get the variable from this pointer.

@dalexeev
Copy link
Member

dalexeev commented Nov 5, 2021

Pointers/references and passing arguments by reference in scripting languages have a rather negative reputation. I think that we should not encourage this.

But if you need, then you can pass an array like [self, "my_var"] or use a class like

extends Reference # 4.x: RefCounted
class_name VarRef

var _instance
var _property

func _init(instance, property):
    _instance = instance
    _property = property

func set_value(value):
    _instance.set(_property, value)

func get_value():
    return _instance.get(_property)

@SilencedPerson
Copy link

would #623 be sufficient solution for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants