How to avoid shadowed variables when using _init?

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

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)

:bust_in_silhouette: Reply From: exuin

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

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

Wakatta | 2021-02-20 23:16

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

Calinou | 2021-02-20 23:31

All comments are good, but I think that p_ is the best option for GDScript. Thanks!

abelgutierrez99 | 2021-02-21 11:04

1 Like