0 votes

How do I initialize variables of a scene script without using the editor?

I have a grid class with two members.

public int sizex and public int sizey. It uses these variables to setup an array of cells in its Ready() function. I can change sizex from the editor but how can I change size_x via script before the _Ready function is called?

PackedScene gridscene = (PackedScene) ResourceLoader.Load("res://Grid.tscn");
//gridscene.size_x=42;
//gridscene.size_y=42*2;
this.local_grid = (Grid)gridscene.Instance();

I read the manual but did not find something about this in it.

https://docs.godotengine.org/en/latest/tutorials/scripting/nodes_and_scene_instances.html

Godot version 3.1 Stable Mono
in Engine by (18 points)

2 Answers

0 votes

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

by (60 points)
0 votes

Answer from reddit:

"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

https://www.reddit.com/r/godot/comments/m3qg7p/how_do_i_initialize_variables_of_a_scene_script/

by (18 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.