Topic was automatically imported from the old Question2Answer platform.
Asked By
rohezal
How do I initialize variables of a scene script without using the editor?
I have a grid class with two members.
public int size_x and public int size_y. It uses these variables to setup an array of cells in its _Ready() function. I can change size_x from the editor but how can I change size_x via script before the _Ready function is called?
in order to declare a variable in the script but initiallize it in the editor you use export variables, in gdscript you write “export” behind “var” which makes export var my_variableand on c# you write something like this
[Export]
public int my_variable;
[Export]
I dont use c# but after looking it up i think this is how you declare an export variable in mono
“Until a node is added as child to a parent, Ready will not be called at all, so set the variables before yourself”.
PackedScene gridScene = ResourceLoader.Load<PackedScene>("res://Grid.tscn");
Grid grid = (Grid)gridScene.Instance();
grid.size = new Vector2(42, 42 * 2);
AddChild(grid); // _Ready method will be called after it's added to the scene tree