This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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(clsvar) but not with print(self.clsvar). Referenced from outside the class with getnode("ClsName").clsvar 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.

in Engine by (2,159 points)
edited by

1 Answer

0 votes
Best answer
   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 -> https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/gdscript_basics.html#data

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)

by (288 points)
selected by

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

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 Frequently asked questions and 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 [email protected] with your username.