Floating Text not working

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

Using a label and a timer I tried to write a code where when i hit the enemy that damage appears above it. But the text does not appear at the enemy’s position but at the sides. I have found other nodes with get_tree().get_root().find_node(“Enemy”, true, false) but this time it doesn’t work

extends Node2D 

onready var label = get_node("Label")
var amount = 0
var velocity = Vector2(0,0) 

onready var Enemy = get_tree().get_root().find_node("Enemy", true, false)

var floating_text = preload("res://Scenes/FloatingText.tscn")

func _ready():


  global_position = Enemy.position 

  label.set_text(str(amount))

  velocity = Vector2(1,50)

  get_node("Timer").start()

func _physics_process(delta):
  position -= velocity * delta 

func _on_Timer_timeout():
  self.queue_free()

#FUNCTION In Enemy Script
func Text():
 var scene_instance = floating_text.instance()
 scene_instance.amount = -50
 add_child(scene_instance) 
 print ("TEXT")
:bust_in_silhouette: Reply From: Inces
global_position = Enemy.global_position 

don’t mix local with global. If text were to use enemies local value, it would use vector.zero and appear in top left corner

its still appearing elsewhere

javrocks | 2022-02-13 12:35

What is this floating_text You add to the main scene in Text() function ?

floating_text.instance()

I ca’t see this variable being introduced anywhere. This is a reference or a class I assume. You must have given offset to it somehow. Show me a code inside this floating_text scene, check if it is correctly positioned in Vector2.ZERO in editor.

Also, the ,most important thing to actually debug your problem : Where does text appear then ? What does it mean “to the sides”. Show printscreen or describe in detail

Inces | 2022-02-13 15:02

I edited in the floating_text var. When i take away the velocity the text is currently appearing at the bottom right of the enemy, but when i add the velocity back in i can’t even see the text. I want it to appear at the top, float away and then the timer removes it.

I can also share it with u if u like

javrocks | 2022-02-13 15:18

So You need the offset. Open your enemy in editor and add child to it - position2D. Place this node where You want text to start. Now, instead of setting text position to enemy position, set it to Position2D.global_position of enemy.
Since You never even see text when You apply velocity, it is simply too fast. Normally we would try to animate the text position instead of actually moving it in process. You can use animationplayer, tween node or move_toward() function for this. These will let You control position within time much easier. Alternatively just make velocity to a very small values until it works properly :slight_smile:

Inces | 2022-02-13 15:44

I added the position 2d as a child of the enemy named it text_position and set the global_position of the text to that node but it dosen’t wrk. Sometimes i see the text but it’s not where the position 2d is instead it’s far above and other times i can’t see it

javrocks | 2022-02-13 16:15

wait wait wait. Who calls Text() function ? I just realized it is called externally. Did You notice that whenever Text() is called, its position was set far before this call - in ready function ?. When your enemy moves, doesn’t floating text display on the old position of enemy ?

Inces | 2022-02-13 16:38

The enemy is what calls the Text Function. That function is in a different script

javrocks | 2022-02-13 16:44

So You should make him update his position. Do it like this :
Node2D script :

func Text(pos):
    var scene_instance = floating_text.instance()
    scene_instance.amount = -50
    global_position = pos
    add_child(scene_instance) 
    print ("TEXT")

Enemy script :

 Node2D.Text(global_position)

Inces | 2022-02-13 16:58

So u want me to put the function in the floting_text script? Because thats what i did and it gave me an error

javrocks | 2022-02-13 17:06

This is to the game

javrocks | 2022-02-13 18:10

No, not in the floating text scene. Look, I modified your Text() function, so it takes argument “pos” right now. As your Enemy calls this Text() function, he should pass his current global_position to it as “pos” argument, so You are supposed to change this line in Enemy script. I wrote :

Node2D.Text(global_position)

By Node2D i meant Your node who has Text() function, whatever name You used for reference. If You still don’t understand give me code of Enemy, I will show You exactly where it should be

Inces | 2022-02-13 20:24

The Enemy script is really long so before i have to edit the whole thing in. Could u try to describe where to put it. The thing that calls my Text() function is a collision shape, the Enemy node is called Enemy and is a Kinematicbody 2D

func _on_Hurtbox_area_entered(_area):

  OnHit()


	       #DAMAGE/HEALTH
func OnHit():

  Text()

  get_node("Timer").start()

  if not is_hit:
	is_hit = true
	health -= 50
	get_node("Hit").start()
	#print ("health = ", health)
	if health <= 0:
		emit_signal("spawn")
		queue_free()
		
		

func _on_Hit_timeout():
  is_hit = false
	   #FLOATING TEXT
func Text(pos):
  var scene_instance = floating_text.instance()
  scene_instance.amount = -50
  global_position = pos
  add_child(scene_instance) 
  print ("TEXT")

javrocks | 2022-02-13 21:08

Did You mix variables and functions of both scripts into one when You posted the code in the top of the topic ?? Now I have no idea what is what.

Ok, so it is enemy who calls his own Text() function, when it is hit. So, whose code is this ?? Is it sript of floating text ? :

func _ready():


  global_position = Enemy.position 

  label.set_text(str(amount))

  velocity = Vector2(1,50)

  get_node("Timer").start()

func _physics_process(delta):
  position -= velocity * delta 

Inces | 2022-02-14 15:57

Yes that is the script of the floating text which is in another scene. The enemy instances the scene when it is hit and the floating text appears

javrocks | 2022-02-14 18:20

When the the floating text is instanced by the enemy is supposed to be located in the enemy’s global position but it’s somewhere else

javrocks | 2022-02-14 18:22

If Enemy calls text() and adds child of floating_text to himself, then floating_text should reposition itself automatically to enemy. What happens if You erase this line in floatingtext script ? :

func _ready():
  global_position = Enemy.position #try erasing this line

Inces | 2022-02-14 18:47

Yeh it appears without having that line but its still in the bottom right corner of the enemy

javrocks | 2022-02-14 18:53

Ok, try to use this offset from earlier now, You called it textposition before.

func Text(pos):
  var scene_instance = floating_text.instance()
  scene_instance.amount = -50
  global_position = pos
  $textposition.add_child(scene_instance) 
  print ("TEXT") 

Inces | 2022-02-14 19:36

it says to few arguments for Text()

javrocks | 2022-02-14 20:24

damn sorry I copied the old one, it is supposed to be without argument

func Text():
  var scene_instance = floating_text.instance()
  scene_instance.amount = -50
  global_position = pos
  $textposition.add_child(scene_instance)  #only this line is different 
  print ("TEXT") 

Inces | 2022-02-14 20:39

It says pos insn’t declared that line isn’t different from what u posted before

javrocks | 2022-02-14 21:08

I am sorry I am tired of it and rushing it. I meant different from your original code, I wanted You to only add this line to original code, without any pos argument, but I keep pasting wrong.

func Text():
  var scene_instance = floating_text.instance()
  scene_instance.amount = -50
  $textposition.add_child(scene_instance)  #only this line is different 
  print ("TEXT") 

I hope You still have this node textposition as child of enemy ? Remember, Position2D node that was offset from enemy sprite

Inces | 2022-02-14 21:20

Ok its in the correct position now it’s just that my enemy node rotates and the Position 2D rotates with it making the text appear upside down. Any tips for this

javrocks | 2022-02-14 21:53

Oh no haha.
We will have to go back to sollution, where I thought floatingtext is not a child of enemy… :stuck_out_tongue:
But first let’s try some trick.

func Text():
  var scene_instance = floating_text.instance()
  scene_instance.amount = -50
  $textposition.add_child(scene_instance)  
  scene_instance.set_as_toplevel(true)     #here
  print ("TEXT") 

Inces | 2022-02-14 22:14

It just spawns at Vector(0,0)

javrocks | 2022-02-15 00:54

func Text():
  var scene_instance = floating_text.instance()
  scene_instance.amount = -50
  $textposition.add_child(scene_instance)  
  scene_instance.set_as_toplevel(true)   
  scene_instance.global_position = $textposition.global_position 
  print ("TEXT") 

Inces | 2022-02-15 13:12

It still only works when the enemy faces me. When the enemy rotates and turns the other direction the Position 2D node rotates with it and the text is underneath the Enemy

javrocks | 2022-02-15 13:58

Ok, now it has to work :stuck_out_tongue:

func Text():
  var scene_instance = floating_text.instance()
  scene_instance.amount = -50
  scene_instance.set_as_toplevel(true)    #lines inversed
  $textposition.add_child(scene_instance)  
  scene_instance.global_position = $textposition.global_position 
  print ("TEXT") 

Inces | 2022-02-15 17:09

Nope still doesn’t work in the other direction

javrocks | 2022-02-15 17:38

what do You mean in other direction ? Is it supposed to rotate in the end ??

Inces | 2022-02-15 18:28

In other words when the enemy is facing to the right the text appears above it because the Position 2D node is above it, but because the enemy is following the player when it rotates 180 degrees the Position 2D node also rotates 180 degrees and is now at the bottom. I have it set so when it rotates far enough left the texture will FLIP H and face to the left but the Position 2D is still at the bottom and that’s where it spawns

javrocks | 2022-02-15 19:39

I don’t understand why toplevel doesn’t work here, it should stop node from behaving like a child.
Too bad, we have to get back to earlier structure - floating text can not be a child of enemy.

func Text():
  var scene_instance = floating_text.instance()
  scene_instance.amount = -50
  get_tree().get_root().add_child(scene_instance)
  scene_instance.global_position = $textposition.global_position 
  print ("TEXT") 

Inces | 2022-02-15 21:28

still doin the same thing

javrocks | 2022-02-15 21:37

no man, it is not possible to rotate along with enemy with this code above. But I realize, that when enemy is already rotated, floating_text appears at wrong side at start. In this case we have to pass fixed position instead Position2D

func Text():
  var scene_instance = floating_text.instance()
  scene_instance.amount = -50
  get_tree().get_root().add_child(scene_instance)
  scene_instance.global_position =global_position + Vector2(x,y) #look below
  print ("TEXT") 

In X and Y choose coordinates where text should always appear related to the player. Best use raw Vector of Local position of your textposition here.

Inces | 2022-02-16 09:21

OK that code u posted last, It is working! Thanks Man or woman idk

Even though the line below didn’t seem to wrk what does was it suppose to do?

set_as_toplevel(true) 

javrocks | 2022-02-16 11:37

Wow, that was unexpectedly problematic :slight_smile:
set_as_toplevel(true) stops child node from copying position and rotation of a parent. It works every time I use it in my code.

Inces | 2022-02-16 13:10

Would u happen to know how to get a rotation value between 3 and -3. My rotation value just keeps increasing when the node rotates

javrocks | 2022-02-16 13:39