How to pass a copy of a string, rather than a reference to it?

bzm3rbzm3r Posts: 2Member

I have a LineEdit node, and I want to pass its contents to another node before clearing its current text. If I do that naively, what happens is that when I clear the current text, the text that the other node got is also cleared...

How do I do a "pass by copy" rather than "pass by reference"?

According to the docs, passing a string to a function should pass it by copy, rather than reference, but I am finding that is not what happens.


Tags :


  • wombatstampedewombatstampede Posts: 41Member

    Can you please post some example code? I tried to reproduce it but everyting worked fine:

        func _ready():
            var s1="Abc"
            var s2=strtest(s1)
            print("String 1 "+s1)
            print("String 2 "+s2)
        func strtest(s):
            return s

    "String 1"
    "String 2 Abc"

  • _807__807_ Posts: 57Member
    edited July 2017

    It´s dificult to say without code. Maybe you are not using correct callback from the node. If you are using a callback (signal) that actualices their state periodicaly (_process, _fixed_process, _input,etc...) that callback take the contents of the line-edit in the moment of actualization (every frame, on input event, etc....). If callback actualizate you variable with line-edit empty, your variable become empty, is not a problem of value-reference, your line-edit give to your callback a value, so.... what kind of method are you using to catch the text of the line-edit?

Sign In or Register to comment.