0 votes

Hi,
I am very new to GDscript and I'm currently working on a turn-based battle system. The problem I've encountered is that I cannot find a working way to pass a value from the overworld to the battle scene, I want to pass an enemy class object for example and use it in the battle scene for archiving a fully reusable scene, however passing an already created object enemy (with all values in it as part of the class) doesn't work, passing the values themselves to then "assemble" the enemy in the battle scene doesn't work either. I've tried with signals and also without, trying all ways that came to mind and realized maybe this isn't possible. I know the battle scene can be archived by pausing the current tree and adding the new one on top but before that I'd like to try doing it like I initially envisioned, if possible.
So is there a way to do this?
I cannot paste all the codes i've tried beacuse I changed a lot of them, currently I have it as:

#class declared separatley with all values set to null and a single setter (similarly to a constructor in c++) for all functions

#enemy overworld collision event
    func _on_body_entered(body):
        if body.is_in_group("player"):  Script.emit_signal("get_enemy", argument 1 ... argument n)
            get_tree().change_scene("res://BattleScene.tscn")

#singleton for signals
       signal get_enemy(argument 1 ... argument n)

#signal reciever in second scene
      func _ready():
    BattleScene.connect("get_enemy", self, "create_new_enemy")

(All these of course in their respective nodes as commented)
Yet trying to use the values in createnewenemy doesn't yield anything. I've read through and through the documentation not being able to find anything. I know this isn't probably the correct way to do it but I tried everyhing i could by now and I'm really frustrated by not being able to find a solution.

Thanks in advance to anyone willing to help.

Godot version v3.2.3 stable
in Engine by (12 points)

1 Answer

0 votes

You could make a global script that is autoloaded so that every script in the game can get its values. This video explains a bit about singletons. It might be a bit complicated but I think it would work.

https://www.youtube.com/watch?v=Qc8I68w5FOE&vl=en

You could have a variable in the global script like current_enemy, and when you bump into an enemy in the overworld, it set the global scripts current enemy to that enemy, then when you get into the battle scene, you can access the global variables current enemy variable to use that enemy. I hope this all makes sense, and I hope this helps.

by (570 points)
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 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 webmaster@godotengine.org with your username.