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 pass-by-value structs to GDScript #5130

Open
lawnjelly opened this issue Aug 9, 2022 · 3 comments
Open

Add pass-by-value structs to GDScript #5130

lawnjelly opened this issue Aug 9, 2022 · 3 comments

Comments

@lawnjelly
Copy link
Member

lawnjelly commented Aug 9, 2022

Describe the project you are working on

Writing testbed navmesh physics in gdscript before conversion to c++ for the engine.

Describe the problem or limitation you are having in your project

User defined classes in gdscript seem to always pass by reference rather than by value (as would c++). This is wanted in some cases, but in the case of e.g. user types like a Vector2i fixed point type, it seems that the only way to get sane behaviour is to write a duplicate() function manually, e.g.:

class MyVec:
    var x
    var y

    func duplicate():
        var res = MyVec.new()
        res.x = x
        res.y = y
        return res

And call duplicate everywhere:

# does not work, does not copy values
var vec_a = vec_b

# does work, copies by value
var vec_a = vec_b.duplicate()

Note you also have to call duplicate on parameters entering functions so you don't alter the calling data.

This causes no end of bugs because if you miss one manual duplicate, you can end up altering source data to a function (this has wasted me many hours debugging this week).

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

One way around this would to be to allow the definition of struct as well as class. The struct would be exactly the same as class, except by default it would copy by value instead of by reference.

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

struct MyVec:
    var x
    var y

# copies by value
var vec_a = vec_b

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

It would be used often, requires loads of bug prone gdscript to workaround.

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

Can't be implemented as addon.

@Calinou
Copy link
Member

Calinou commented Aug 9, 2022

This is probably a duplicate of #2816. I'd suggest posting your proposal there as a comment.

@Calinou Calinou changed the title Pass by value structs in gdscript Add pass-by-value structs to GDScript Aug 9, 2022
@lawnjelly
Copy link
Member Author

This is probably a duplicate of #2816. I'd suggest posting your proposal there as a comment.

I did see that proposal, which would also solve it if it involved pass by value. However that proposal is quite a bit more complex, and the pass by value is just one aspect, it is more about compactness and Variant support etc.

This proposal is far more minimal - I'm primarily concerned with solving a pressing problem in the language here with something that is less controversial and easier to implement (and thus easier to get into production).

Without knowing the innards of gdscript, this proposal may at a minimum just involve a small alteration to the assign operator, and if we could also add this when passing arguments this would be great.

@alazifk
Copy link

alazifk commented Sep 24, 2022

The proposal only mentions inner classes and structs. How would you do this on a global level?
And how about using annotations to avoid introducing a new keyword? @copy_by_value class might be more self-explanatory for people without a C background.

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

3 participants