How to have enemies only load once?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GlitchyTurtle

I have a pretty basic RPG system, and I have my enemies/items in the first area scene - the problem is players can just walk in and out of the scene over and over to get an infinite supply of the items.

It sounds like you just need to ensure the code that spawns the enemies/items only runs once. That could be as simple as checking some boolean variable prior to the spawn and then only spawning when its value is false. Then, after the initial spawn, set its value to true.

Bottom line, there’s probably a straight forward way to fix the issue, but the details will depend on how/where the spawning is being done. Can you share the relevant code?

jgodfrey | 2022-03-10 16:48

:bust_in_silhouette: Reply From: ponponyaya

A simple idea is that You can use a varible to check if it has the specific item or not.
For example var has_sword : bool.
P.S. If you have lots of items then use array or dictionary.
When player enter the scene, check the value of has_sword. If has_sword is false, then queue_free the sword.

:bust_in_silhouette: Reply From: hermes_belmont

create an autoload script.
for example called “gamedata”

on that script create a variable called for example

var unique_enemy_list =

or

var unique_items_list =

then on your enemy sript,
create hima variable “numberID”
go to the ready function, and check if that ID is in the
gamedata.unique_enemy_list, if it’s there, queue.free()
otherwise, it will be spawned as normal.
and write :

export var numberID = 0 # being an export variable, so you can make multiple of these enemies and only change this variable on the editor.

func ready():

if gamedata.unique_enemy_list.has(numberID ):
queue.free()
#no need for else statement.

and once you killed that enemy or caught that item go and add it to that list.

gamedata.unique_enemy_list.append(numberID)

so the next that you load the scene, it will check in the ready function and it will queue free instead of spawning.

:slight_smile: what do you think?