I'm just starting on gdscript (background in python). I was wondering if someone could please clarify scope in gdscript.
This is my best guess so far. I'd appreciate it if you'd correct where I've gone wrong.
All class and instance variables within inherited classes can be directly referenced eg:
print(self.par_cls_var)The language first checks the method, then the class and then the parent class(es)
var cls_var = "foo"
Class variable (not in a method, not indented) - can be referenced from any method within class eg: print(clsvar) but not with print(self.clsvar). Referenced from outside the class with getnode("ClsName").clsvar or with $ClsName.cls_var
var inst_var_1 = "bar"
var self.inst_var_2 = "bar"
Instance variables - including or omitting self makes no difference. Not necessary or "ungdscriptic" not to first declare within _init. Referenced like the class variables above. Local variables - don't exist in gdscript, everything is either a class or instance variable.
Ok, this was my best guess until I instantiated an object and found it was out of scope in other method.
var surface_tool = SurfaceTool.new()
surface_tool.set_vertex(0, 0, 0) # surface_tool obj out of scope
It surprised me that the editor told me
surface_tool was out of scope. Even more when
var self.surface_tool = SurfaceTool.new() wasn't valid in the _init function. So is it the opposite of what I was saying and that there are only local variables and no instance variables or do both somehow exist? Finally, are variables pointers to memory locations or containers of data in gdscript?
Equally, if scope is clearly laid out in the docs and I simply missed the page then a link would be great.