Topic was automatically imported from the old Question2Answer platform.
Asked By
Derik Hammer
When I run my game, the “loadOut” variable is NULL, in the _load_stats func, when it is instantiated. My understanding is that my getter should prevent any such NULL value. Can anyone please help me understand this behavior?
Godot Engine v3.2.1.stable.official
extends Node2D
var loadOut setget , _get_load_out
var _default_load_out = preload("res://equipment/load-outs/player-load-out.gd").new()
func _ready():
pass
func _init():
_load_stats()
func _get_load_out():
return loadOut if loadOut != null else _default_load_out
func _load_stats():
for i in loadOut.stats:
self.set(i, loadOut.stats[i])
I solve the problem. Thank you to Becbunzen for helping out.
I revised my code to this:
func _ready():
_load_stats()
func _init():
pass
Because load stats, in the init, was loading prior to default load out being ready. I noticed that, if I continued the program through a bunch of the iterations in load stats, then loadOut would be returning correctly.
By loading the stats on ready instead, I am effectively delaying the call and letting the default load out populate.
Great that you found a solution! See my comment on the other thread about default value as well, if needed.