+1 vote

I'm not sure about using _init when instanciating a node. I give you a part of my code:

extends Spatial

var N : int
var iL : float
var oL : float

func _init(N=6, iL=0.9, oL=1):
    self.N = N
    self.iL = iL
    self.oL = oL

Debugger says 'N', 'iL' and 'oL' are shadowed, but I don't know how to avoid that. I instanciate the node using .new(N, iL, oL)

Godot version 3.2.3
in Engine by (244 points)

1 Answer

+2 votes
Best answer

You can call the _init parameters something else. Shadowed means they have the same name.

by (8,150 points)
selected by

Yeah you're absolutely correct and its something i had a hard time with when starting GDScript because in C++ you can instantly cast directly to the variable on creation.

so i started doing

func _init(_N=6, _iL=0.9, _oL=1):
    N = _N
    iL = _iL
    oL = _oL

because _ means temporary var in C, C++, C# but in GDScript apparently means ignore

In Godot's codebase, function parameters are prefixed with p_. You could adopt the same convention here in case of conflicts :)

All comments are good, but I think that p_ is the best option for GDScript. 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.