0 votes

Hello everyone I hope everyone is well! I'm having a bit of a problem when it comes to keeping my enemies dead after the scene changes from A to B and then back from B to A. I've tried using a singleton to store when the enemies die, and while it does register that the enemies have taken a dirt nap, It doesn't keep them dead when the scene changes back and forth. So my question is how do I keep them dead when I leave the scene and come back? I have some code if it's needed.

in Engine by (54 points)

2 Answers

+1 vote

It will work with singleton.
something you could do is in the ready() function of your mob, make it check if it can find itself in the singleton, if so, trigger the function to kill him.

by (23 points)

Thank you for answering :) but like I previously stated " I've tried using a singleton to store when the enemies die, and while it does register that the enemies have taken a dirt nap, It doesn't keep them dead when the scene changes back and forth. " I'm just not sure how to call it properly to keep them dead ¯_(ツ)_/¯ That's my problem here. Regardless I'm pressing on. Perhaps when I learn more code I'll figure it out until then. c'est la vie!

0 votes

Figured it out. Create a singleton and name it Global and give it a var. Name the var enemy__life = 1. Then in your enemy script create a _process function and write:

if Global.enemy_life <= 0:
    self.queue__free

Now all you'll need to do is create another function that will subtract from Global.enemy__life. And that's it you are done :)

for those that just wanna see the code here you go!

Enemy:

extends Node2D

export var dialog = ["This is test dialog" , "this is new dialog"]


 var character_name : String = "Enemy"
 var lvl : int = 2
 var dialog_index = 0
 var dialog_fin = false
 var can_speak = false


 func _process(_delta):
    if can_speak == true:
    load_dialog()
     if Global.enemy_life <= 0:
        dead()


 func load_dialog():
pass

func dead():
 Global.enemy_life -= 1
  self.queue_free()


  func _on_Area2D_area_entered(_area: Area2D) -> void:
     can_speak = true

  func _on_Area2D_area_exited(_area: Area2D) -> void:
      can_speak = false

  func _on_Enemy_battle_enemy_died() -> void:
     dead()

Just a note but, this code will effect every single enemy that uses this Global value. Regardless if you killed them or not. So you may want to either use this for special occasions or make an export var so every enemy has their own life values.

Happy coding everyone and Farewell!

by (54 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.

Categories