Hello I require help will a little problem I have discovered. Need to make a chest that spawns a key in my game. The key it’s self on it’s own works as intended. However when the key is instanced by the chest and placed as a child of the chest. The key no longer works and the frame rate slowly starts dropping . I have determined that it is in fact only when the key is instance from the chest this happens . So I need to know how to change the parent of the key scene . The code for the instance is as follows. Var SPAWN = preload( key scene)

Func open() Var spawn = SPAWN.instance() Get_parent().add_child(spawn) Spawn.position = $Position2D.global_position

Any and all help would be Appreciated

Hello good sir!

After that signal gets for "_on_open_point_area_entered(area)" called to define:

used = "yes"

... the process(delta) function gets called once every frame, and executes Open() -- which triggers the animation; creates the instance of SPAWN, and deposits spawn as a child into the node hierarchy -- once every frame.

If you are pulling 60 frames per second... your node hierarchy is accumulating a new instance of SPAWN 60 times per second; which is allocating new RAM 60 times per second. This is an infinite resource consumption loop... you want to avoid these... one of them will lead to a memory leak (which will single-handedly slowly drop your framerate)... 10 of them will bring the process loop of any game engine to it's knees.

(Infinite resource consumption loops are useful for stress testing a CPU. If you are attempting to cause CPU failure due to overheating because you're working as a Quality Assurance engineer for Intel, or something; you might write an Infinite resource consumption loop to test the limits of the CPUs that just came off of the assembly line. -- But if you're trying to write a game, you may "stress-test" a CPU; but usually it's an unintentional side effect rather than an intentional goal; stable games don't have them)

So... as a suggestion:

func Open():
	if used == "yes":
		$anim.play("opened")
		var spawn = SPAWN.instance()
		get_parent().add_child(spawn)
		spawn.position = $Position2D.global_position
		# Now add the line below
		used = "no" 

<-- That might help. (If someone with more experience disagrees with my assessment, please speak up)

riphle, on this problem that you are working on... I was wondering if you had generic keys that open any and all locks that you define in your game?

Gray wolf117 thank you vary much it didn’t occur To me that it was spawning the key infinitely. Your post was was a great of great insight .

The game it self is similar to Zelda with some differences The plan is to have a half small keys that open basic door and boss keys that open the boss door . In a previous project I had gotten it to work but the keys were just placed in the scene. I was trying to jazz it up a bit with the chest

"Jazzing it up" is how you learn. : )

Actually... now that I've had a chance to think about what you're doing a little bit, that "Open()" function probably does not belong in a process(delta) loop. Instead, you could have that function triggered when an action is taken.

So... in that process(delta) function above, I'd change it to:

func _process(_delta):
	pass

In your main key event object (referring to "keyboard" keys, not key-for-lock keys):

func process_key_event(event):
	if(event.is_action_pressed("ui_accept")):
		print("ui_accept triggered")
		emit_signal("ui_accept_signal")

In your chest object (beware pseudocode below -- the interpreter will fail if you use this literally):

self.connect( "ui_accept_signal", self, "_on_ui_accept_signal")

func _on_ui_accept_signal():
	if <character_proximity_to_chest_with_id_condition_is_true>:
		Open()

Note that this differs from your current logic for determining proximity, so you may need to shift things around a bit. If the idea I propose sounds interesting... use it; if you have another approach that you'd like to try, then feel free to do that instead.

Well I spent the day thinking it over about how I want to do it and I didn’t really understand the code you had written . I thank i was over thinking it way to much. The solution I needed was just a way to call the open() only once with out it being able to be called a second time. So here is the solution I came up with . I turned it in to a state machine. where after the key spawns The chest is set in to a new state where nothing happens. I pretty pleased I could do it with a simple fix and I am vary great full to the help and guidance you provided. My game will likely only be a personal game. Seen only by a few people but I would still like if you have no problems with it to put your user name in the credits of the final product. If you have a problem with that please inbox me/ comment here I will attach a photo on the final code I used

"So here is the solution I came up with . I turned it in to a state machine. where after the key spawns The chest is set in to a new state where nothing happens. I pretty pleased I could do it with a simple fix"

That's how this works. You come up with an idea... and you just keep chiseling away at it until you come up with a solution that produces behavior that you like. Over time, you get better at controlling the Rube Goldberg machine. I'm glad that you were able to develop a solution that you like!

I like your FSM. Maybe, when you're feeling confident enough, you may be willing to try and build a bigger FSM for a monster AI. I have a link that's an example of this [1]. Here's a second example, I think this one is better, tbh [2]. I keep looking, and I find one even better, lol! [3]

[1] https://gamedevelopment.tutsplus.com/tutorials/finite-state-machines-squad-pattern-using-steering-behaviors--gamedev-13638

[2] https://homepage.cs.uri.edu/faculty/hamel/courses/2015/spring2015/csc481/lecture-notes/ln481-003.pdf

[3] https://www.cc.gatech.edu/~surban6/2016-cs4731/lectures/2016_06_02_DecisionMaking_FSM.pdf

"Seen only by a few people but I would still like if you have no problems with it to put your user name in the credits of the final product."

I had no expectations for credit, I was just happy that I could help, a bit. I have no problems with you putting my user name in the credits if you wish to do so, or you can leave it out; either option is fine by me. : )

Fsm’s are cool and I plan on implementing them for my monsters. Right now they just wander and I want them to eventually move towards the player when the player enters a Radius

2 years later