utopia-rise/godot-kotlin-native

Local property copies updating the original object automatically

CedNaru opened this issue · 1 comments

Describe the problem or limitation you are having in your project:
Having to use syntax like:

position{
    x = 2
}

create a semantic difference between the properties of the Kotlin Script and the properties of the object the script is based on. This difference doesn't exist in Gdscript, so this particulary must disapear.

Describe how this feature / enhancement will help you overcome this problem or limitation:
This feature will allow you to write position.x = 2 without worrying about the local copy.

Show a mock up screenshots/video or a flow diagram explaining how your proposal will work:
/
Describe implementation detail for your proposal (in code), if possible:

The root of the problem is that a godot object doesn't return a pointer when getting a core property but a copy. If we try to update this copy, we have to call the setter either manually with:

it = position   //getter called, return a copy and assign it to the variable "it"
it.x = 2 //local copy updated
position = it  //setter called, varaible "it" copied into the object.

or with
position{ x = 2} where the setter is called automatically at the end of the lambda.
Sadly I don't know a way to get the pointer to the original core property.

I want to implement a mechanism calling the setter every time, we write something like:
position.x = 2.
It can be achieved in the following way:

  • Adding a optional "owner" pointer to the core wrappers. If the owner is not null, it means this core object is a copy of a property inside another Godot object. Otherwise, it just means this is a standalone core created in the script as parameter or as a temporary object.
  • Adding a setter to every properties of a core object, this setter will check if the core object has a owner. If that's the case, the core object will copy itself into its owner.
  • The Api Generator must generate the code setting the owner when a copy is obtained from an object property getter.

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

Is there a reason why this should be in this project and not individually solved?:
It required to change the core wrappers and the api generator.

After debating, no simple solution worth being implemented was found.
Issue closed.
Maybe someone will have a brilliant idea someday.