Creating a copy of a node

Godot Version

4.5

Question

So i am making a turn-based rpg and I needed to amke a text that would appear above an enemy when it is attacked, show the amount of damage dealt (or display “dodged” or “blocked” if the enemy dodged or blocked"), float down and disappear. I got it to work, but I ran into an issue. If the player cycles through turns too fast, this will result in the text getting teleported before it can finish the animation cycly, so it ends up looking bad. I’ve tried using duplicate() to make the node leave a clone instead of doing the script itself but i just can’t figure out how to do it. Could someone please help me fix this problem with the duplicate() or tell me about any other way to do it? Here is my current, working code.

extends Label
var enemy = []
var hero = []
var speed = 50
func _ready():
	text = "no"
	for i in range(1, 9):
		enemy.append(get_node("/root/Battle/enemy_team/Enemy" + str(i)))
		hero.append(get_node("/root/Battle/player_team/Hero" + str(i)))

func _on_current_turn_attack_message_hit(target, damage) -> void:
	if target <= 8:
		global_position = enemy[target-1].global_position
		show()
		text = str(damage)
		speed = 50
		await get_tree().create_timer(3).timeout
		hide()
	else:
		global_position = hero[target - 9].global_position
		show()
		text = str(damage)
		speed = 50
		await get_tree().create_timer(3).timeout
		hide()

func _process(delta: float) -> void:
	#while speed >= 0:
	position.y -= speed*delta
	speed -= 20*delta


func _on_current_turn_attack_message_dodged(x) -> void:
	text = "Dodged"
	speed = 50
	if x <= 8:
		global_position = enemy[x - 1].global_position
		show()
		await get_tree().create_timer(3).timeout
		hide()
	else:
		global_position = hero[x - 9].global_position
		show()
		await get_tree().create_timer(3).timeout
		hide()


func _on_current_turn_attack_message_blocked(x) -> void:
	text = "Blocked"
	speed = 50
	if x <= 8:
		global_position = enemy[x - 1].global_position
		show()
		await get_tree().create_timer(3).timeout
		hide()
	else:
		global_position = hero[x - 9].global_position
		show()
		await get_tree().create_timer(3).timeout
		hide()


Make your text balloon a scene and instantiate it whenever you need it.

Also, get rid of those awaits

1 Like

Why would I get rid of await’s?

They are bug factories.

2 Likes

that does not change the fact that I need the node to delete itself once the script is done, so i need the awaits to set the timer. besides, it never caused any issues when I used them before

Let the scene delete itself from a script in its top node. Instead awaiting on the timer, connect a signal handler function to the timeout signal and do the self-deletion there.

I did not understand a single word you jsut said

That’s fine. Are you familiar with Godot’s scenes?

yeah, I am.

Then make a label scene and instantiate that scene from script code whenever you need to show the label.

well that’s what I did, I already figured it out. I just don’t understand why you want me to remove await’s even though they are working fine

So where’s the problem then?

I mentioned awaits in passing, not as a cause of your current problem. You can trust me that the habit of using await in this fashion has a great potential of causing hard to detect bugs, and remove them, or not trust me and continue to use them. It’s your choice.

1 Like

I didn’t even say that there was a problem, because the problem is already fixed

Oh? Then you should have posted the solution.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.