This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

How to do Setter/Getter in (C++) NativeScript

sent44sent44 Posts: 27Member

Question already said almost everything.
I mean I want something like this in GDScript // same with setget behavior

native.variable = something
print(native.variable) # print: "something else" because setter function in NativeScript add "else"

Tags :

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 2,561
    Accepted Answer

    In the Godot C++ source code, getters and setters are just functions that get used. For example, if you want to get the global transform of a Spatial node, in C++ you need to use Transform trans = node.get_global_transform(), unlike in GDScript where you can use var trans = node.global_transform (though under the hood, the GDScript code does the same thing).

    You'll probably need to make setter and getter functions, and then use them directly. So in your example, it would probably need to be something like the following (C++ like pseudo code):

    native.set_variable(something);
    print (native.variable)
    
    // example setter function
    native::set_variable(variable p_argument) {
        variable = p_argument; // normal setter
        variable = "something else"; // override in the setter
    }
    

    That said, there may be a way to define getter and setters in C++ so you can just modify the variable, I just do not know what they would be right off. I know in the Godot source code, getter and setter functions are used directly, with direct variable modification being used when necessary.

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 2,561Admin
    Accepted Answer

    In the Godot C++ source code, getters and setters are just functions that get used. For example, if you want to get the global transform of a Spatial node, in C++ you need to use Transform trans = node.get_global_transform(), unlike in GDScript where you can use var trans = node.global_transform (though under the hood, the GDScript code does the same thing).

    You'll probably need to make setter and getter functions, and then use them directly. So in your example, it would probably need to be something like the following (C++ like pseudo code):

    native.set_variable(something);
    print (native.variable)
    
    // example setter function
    native::set_variable(variable p_argument) {
        variable = p_argument; // normal setter
        variable = "something else"; // override in the setter
    }
    

    That said, there may be a way to define getter and setters in C++ so you can just modify the variable, I just do not know what they would be right off. I know in the Godot source code, getter and setter functions are used directly, with direct variable modification being used when necessary.

  • sent44sent44 Posts: 27Member

    Oh, that sad.
    It will force me to modify code many places to implement native class correctly.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.