[SOLVED] Scope in gdscript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DaddyMonster

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.

extends ParentCls 

All class and instance variables within inherited classes can be directly referenced eg: print(par_cls_var) or 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(cls_var) but not with print(self.cls_var). Referenced from outside the class with get_node(“ClsName”).cls_var or with $ClsName.cls_var

func _init():
    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.

func _init():
    var surface_tool = SurfaceTool.new()
    do_something()
    self.do_something()

func do_something():
    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.

:bust_in_silhouette: Reply From: UnRealCloud 1
   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)

The variable have to be create.
print(clsvar), is looking in your object (parents too) first then in the global name spaces i guess and don’t find it.
but

print(self.clsvar).

You clearly ask to find a variable clsvar in the object self, and if the object doest not have this variable, you will have an index access error.

Local variables - don't exist in gdscript, everything is either a class or instance variable.

Variable can be local to function → GDScript reference — Godot Engine (stable) documentation in English

It’s normal that your variable in your _init() function is not accessible from somewhere else

var self.surface_tool = SurfaceTool.new() wasn't valid in the _init function

Same explanation as for print(self.clsvar)

Sorry for the delay in replying. That’s helped a lot, many thanks!

DaddyMonster | 2020-07-21 19:39