Resources management game mechanic

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

so I have a game mechanic im playing with at the moment. I have a harvester moving from its spawn point to the nearest mine and then back to the spawn point. (think command and conquer harvester ) my problem is i have the resources node detecting if it has been entered and starting a counter every 5 second it decreases its remaing gems by one. when it gets to 0 i want it to queue_free and update the list of nodes in the array. However it just crashes when i use

"if Remaining_gems ==0:
		queue_free()"

the code in the Gem node is:

    extends Area2D

var Remaining_gems = 5

func _ready() -> void:
	pass # Replace with function body.

func _process(delta: float) -> void:
	$Label.text = str(Remaining_gems)
	print(Remaining_gems)
	if Remaining_gems ==0:
		queue_free()

func _on_Timer_timeout() -> void:
	if Remaining_gems > 0:
		Remaining_gems = Remaining_gems - 1
	elif Remaining_gems == 0:
		Remaining_gems = 0

func _on_Gem_body_entered(body: Node) -> void:
	$Timer.start()

func _on_Gem_body_exited(body: Node) -> void:
	$Timer.stop()

the code in the Main level is

func _ready() -> void:
	Fruit.append_array($GemPoints.get_children()
func _process(delta: float) -> void:
$Label.text = str(Remaining_gems)
print(Remaining_gems)
if Remaining_gems ==0:
    $Timer.stop() // just try
    queue_free()

and Maybe
The problem must be elsewhere.

if Remaining_gems == 0
other place is using here and when equal to Zero the problem occurs
Sample
Fruit.append_array($GemPoints.get_children()
$GemPoints.get_children
Maybe it’s causing trouble because he doesn’t have children.
I hope I was able to explain

ramazan | 2022-03-08 11:05

:bust_in_silhouette: Reply From: w411-3

I don’t think I completely understand your goal, but the thing that sticks out to me is how you’re calling queue_free inside the object you mean to remove. In technical standards, I’m not sure why this is odd to me, but this looks like a good place to use a “manager”.

That would be a new node/scene that decides when to spawn or queue_free these resources instead of doing it themselves. In fact, even without a new node, if you could queue_free the object in your “main” scene instead, I think it would at least work. But learning the concept of a manager to do this would be beneficial probably.

:bust_in_silhouette: Reply From: hermes_belmont

I’d write it more like this way…

extends Area2D

var Remaining_gems = 5

func _ready() → void:
pass # Replace with function body.

func _process(delta: float) → void:
print(Remaining_gems)
$Label.text = str(Remaining_gems)
if Remaining_gems <= 0:
queue_free()

func _on_Timer_timeout() → void:
if Remaining_gems >= 1:
Remaining_gems -= 1

func _on_Gem_body_entered(body: Node) → void:
$Timer.start()

func _on_Gem_body_exited(body: Node) → void:
$Timer.stop()

I dont think there’s nothing wrong actually…
this is my first answer here by the way…