#[solved] How do I use a signal from an autoload to call a function with an argument?

1 messages · Page 1 of 1 (latest)

quiet saffron
#

I am trying to get a signal from one scene to initiate a save in the main scene. The signal is emitted properly and when there is nothing in the brackets of the save_bird function it is received fine, but then the function doesn't work correctly. Is there any way to fix this?

atomic phoenix
#

Can you show the lines where you declare and emit the signal?

quiet saffron
atomic phoenix
# quiet saffron

Alright, so it looks like you're emitting the signal with zero arguments, which is why the function isn't working properly. You'll want to put your data in the brackets of your emit() call.

quiet saffron
#

I tried that but got an error. Is there something I'm doing wrong?

atomic phoenix
#

I'll check on this in a bit, just getting ready to head out haha

quiet saffron
#

Thank you. I need to go to bed anyway

atomic phoenix
#

Emitting a signal is the same thing as calling a function. You need to emit it with the right argument for the function to read. Just putting the word "data" there won't do, you'll need to send it the data that the function expects.

quiet saffron
#

I managed to send the signal nut now I'm getting an error about an int even though there isn't one

#

This is the file I'm trying to save ⬇️

atomic phoenix
#

Well, there is an int. You're sending the score to the save function, which is just a number, and not putting it into an object.

If you want to use ResourceSaver to save to file, the best way to do this is to make your own custom Resource class. Start by making a new script that has extends Resource at the top, add another line that says class_name SaveFile, and give it an @export variable called "high_score" or something.

Now, in your save_bird function, do this:

func save_bird(data):
  var file = SaveFile.new()
  file.high_score = data
  ResourceSaver.save(file, save_path)
quiet saffron
#

I'm sorry to bring up more issues but I'm still struggling with this save system. The save function is working (I think) and prints "save" When I want the game to save. When I load the game up though it recieves the signal and prints "siganl recieved" but doesn't actually load the save and does not print "loaded"

atomic phoenix
#

I think you need to take a moment to understand exactly what's happening here. KidsCanCode's tutorials are great, but it looks like you're a bit confused by the way he's going about things.

The load_data function, as it is now, won't actually do anything. It loads the resource, but it sends that resource back to the function that called load_data(). That's what return means, it's "returning" some data to the source function.

You'll need to set up this function to, instead, apply the resource's data back into the game. Get rid of that return load(data) line. Create a new variable to hold this resource. Then, set the high_score in your autoload to the resource's high_score.

Simply copying code snippets isn't a good way to learn. You need to also try to understand what this code does, how it works, why it's used instead of another method, etc.

quiet saffron
#

I did it, thanks for all of your help. Do you have any advice on learning things I'm struggling to understand in the future? I read the documentation while troubleshooting this but didn't understand it very well

#

[solved] How do I use a signal from an autoload to call a function with an argument?

crimson crown