#Autoload broke my Code, how do I fix?

32 messages · Page 1 of 1 (latest)

sage mountain
#

So, in an effort to have an enemy mob communicate with my main scene, I set my main scene to autoload, so it could be referenced for a signal. (See first image)

However, this has caused an error to happen on boot of my game. I use a "PathFollow2D" node to randomly spawn enemies off screen, and autoload appears to be be doing something on boot that causes this error to crash the game, even though this scene isn't the first to load, and it functioned nomally prior to autoload. (See second image)

Any idea how to fix this issue, or what exactly is causing it technically?

marble hatch
#

can you show what the scene tree looks like? a "node not found" error implies you're trying to reference a node that isn't actually there

topaz pike
#

might have better luck putting it back where it was, and not as an autoload

#

you can achieve your goal by just making an autoload that contains signals that you can emit and connect from other scripts

#
# this is an autoload script
# lets call it GlobalSignals

# you define your global signals in it e.g.

signal slime_dead()
----------------------------------------

# this is how to connect to the signal #from other script

GolbalSignals.slime_dead.connect(my_func)
----------------------------------------
#this is how to call it from any other #script

GlobalSignals.slime_dead.emit()
---------------------------------------
#

As an example

sage mountain
#

When the scene is "ready", it attempts to spawn multiple mobs at the start.

#

I think because I'm autoloading it, it's attempting to call a node that isn't present yet, since it loads the main menu, while trying to spawn mobs since the autoloader detects the scene as "ready".

#

So, that leaves the question: How could I delay this until the scene is properly done?

marble hatch
#

i think the best course of action is to stop autoloading that scene and instead have a separate autoload script to pass signals and such

vital belfry
#

Also instead of starting the spawn functions on "ready", start them as a callback to receiving a signal.

E.g. somesignal.connect(spawn_mobs)

sage mountain
#

I'll give that a shot, should it be a specific node?

vital belfry
#

If u really dont want a autoload Signals Manager like @marble hatch suggested, alternative you can:

  • put mainscene into a "group" and reference it in the other scene via .get_first_node_in_group
sage mountain
#

(the global signals thing)

vital belfry
marble hatch
vital belfry
#

After assigning Main to a group.
Calling `get_tree.get_first_node_in_group("groupname") should return the Main scene

#

But Zet's method is a neater coding pattern that you should use

sage mountain
#

OHHH I CAN JUST AUTOLOAD FROM AN INDIVIDUAL SCRIPT, NEAT

sage mountain
#

Okay, this is currently how I have the node set up. It's separate from both Mob and the Game Scene. That being said, how do I connect this to Mob and Game Scene? I can obviously bridge a signal from Mob, to here, to Game Scene, but I'm not sure how to make the connection properly.

marble hatch
#

you use emit() to emit signals, and you use connect() to connect signals to functions (so that the function will run when the signal is emitted). usually connections will just be made by whatever node needs the connection in their _ready() function. example:

# script 1
func _ready():
    SignalManager.signal_name.connect(function_name)

# script 2
func some_function(some_arg):
    SignalManager.signal_name.emit(some_arg) # args of course aren't necessary in every case, but i'm just demonstrating that they can be used
#

and in this case a better way to handle it would be to simply have the enemies emit the signal on death, rather than connecting a function to signal so that it can connect to another function

sage mountain
#

Right, I do have them plan to emit the signal upon death.

#

So how will SignalManager in this case be able to hear that signal, so it can in turn signal the game to update the score?

marble hatch
#

the signal manager doesn't need to hear the signal unless the signal manager itself is going to do something with it. instead you'd have the game manager (whatever is handling score here) handle whatever needs to happen with that. something like:

# enemy script
func death_function():
    GlobalSignalsNode.score_update.emit(score_value) # the enemy tells the signal manager that it died by telling it to emit a signal

# game manager script
func _ready():
    GlobalSignalsNode.score_update.connect(update_score) # connecting the score update function to the signal manager

func update_score(value: int):
    score += value # now this line will run whenever an enemy dies
sage mountain
#

Okay, I think I get it

#

I got to a point with this error, though, which happens when the score should update (the enemy is killed)

#

NEVERMIND I FORGOT TO SET HUD TO HAVE ACCESS AS A UNIQUE NAME