![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Gabriel |
Hi, what I want to do is this:
- Player is in Stage1 and picks up a life upgrade (an Area2d that is queue_freed on body entered);
- Player goes to Stage2 (using change_scene());
- Player goes back to Stage1 and the item does not spawn anymore because it was taken already.
- If the Player goes to the Title Screen (Main Menu) and than goes back to Stage1 the item should be there.
After thinking for a while I decided to use a Singleton (globals.gd) for the spawnable items like this:
extends Node
var spawnable_items = {
"DarkMaze": {
"LifeUpgrade" : {
"pos" : Vector2(-1161,1195),
"id" : 0,
"taken" : false
},
"NormalBullet" : {
"pos" : Vector2(2850,752),
"id" : 1,
"taken" : false
}
}
}
func set_item_as_taken(stage_name, id):
for key in spawnable_items[stage_name]:
if spawnable_items[stage_name][key]["id"] == id:
spawnable_items[stage_name][key]["taken"] = true
I use the “spawnable_items” dictionary to spawn the items on the stages. Then on the pickup script on_body_entered method I use:
globals.set_item_as_taken(get_tree().get_current_scene().get_name(), id)
And it works. But if I go to the main menu (another call to change_scene()) and go back to the stage, the item is not there! So each time I go back to the title screen I have to reset the items that were taken. So on the the autoload script I added:
func reset_taken_items(stage_name):
for key in spawnable_items[stage_name]:
spawnable_items[stage_name][key]["taken"] = false
Then each time I go to the Title Screen I use:
globals.reset_taken_items(get_tree().get_current_scene().get_name())
Again it works, but it does not feel like a good solution:
- I want to implement this “persistent” behaviour through stages on other things (like destructible blocks, dialogs, etc.) and this autoload script can became huge.
- I did not implement a save system yet, but when I do I’ll have to keep track of that too. (I’ll probably won’t be able to just set every “taken” value to false when I go to the title screen),
- Spawning the items through code is not as convenient as just draging than on the GUI.
Any ideas would be apreciated.
Thanks in advance.