0 votes

When I run my game, the "loadOut" variable is NULL, in the loadstats 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])
in Engine by (21 points)

2 Answers

+1 vote
Best answer

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.

by (21 points)

Great that you found a solution! See my comment on the other thread about default value as well, if needed.

0 votes

You do not have a setter? Try this:

var loadOut setget _set_load_out, _get_load_out

plus define the setter. Also might help to set it to an initial value.

Edit: If you only want a getter, you have a space too much, should be

var loadOut setget ,_get_load_out
by (160 points)

Unfortunately, I receive the same behavior with the setter and the space removed.

Added code:

var loadOut setget set_load_out,_get_load_out

func set_load_out(value):
    loadOut = value

Try setting a default value already in the declaration, and check if it is ever changed.

Is there a way to set a default value in-line with a setget?

Image of the default value having a value. Also, break point confirms entry into the getter.
Image of the default value having a value. Also, break point confirms entry into the getter.

Yes, you can set default. Like this:

var loadOut = 0 setget set_load_out,_get_load_out
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.