0 votes

i have tried so many solutions but it just gives me an error or it wont set it at all

if Input.is_action_just_pressed("fire"):
        if currentWeapon == "Gun":
            gunAmmoLefttostr = String(gunAmmoLeft)
            gunAmmoLeft = gunAmmoLeft - 1
            gunAmmoLeftText.text = gunAmmoLefttostr
Godot version 3.4.4
in Engine by (16 points)

2 Answers

+1 vote

While I'm not sure where you're going wrong, I can assure you that the following code works as expected (sets the label's text to 1) assuming the node containing the script has a child label named Label.

func _ready():
    var int_var = 1
    $Label.text = String(int_var)

If you're getting an error, what does it say?

by (14,748 points)

it says "Invalid set index 'text' (on base: 'int') with value of type 'float'."

That implies that the object you're attempting to set the text property on is an integer. That's certainly not what you want. In the above code, my $Label reference points to a Label control. In your case, your variable points to an integer value. For example, this will trigger that exact error:

var float_var = 1.0
var int_var = 1
int_var.text = String(float_var)

Your Label reference is wrong. If you can't find it, paste the code that's causing trouble, along with how you're setting the reference to the object that you're setting the text property on.

this is what the variable is onready var gunAmmoLeftText = get_parent().get_node("UI/HUD/Ammo/AmmoLeft")

This should be really simple, so we're still missing something. Please post the following:

  • The entire script that's failing (formatted as code for the forum)
  • A screenshot of your scene tree
  • Indicate which node in the scene tree contains the (pasted) script

Or, better yet - if the project is available to look at, post a link to a download...

Alright i think i have a (temporary) solution, so using an onready var just doesn't do anything, not even an error/warning. using a var errors because it cant find it. but just pointing to the node through the statement and not the variable makes it work like intended. But if you want to review the code still here it is.

extends KinematicBody

var speed = 10
var h_acceleration = 6
var air_acceleration = 1
var normal_acceleration = 6
var gravity = 50
var jump = 20
var full_contact = false
var aiming = false
var paused = false

var damage = 100

var mouse_sensitivity = 0
var mainsensitivity = 0.03

const SWAY = 30
const gunClipSize = 10

var direction = Vector3()
var h_velocity = Vector3()
var movement = Vector3()
var gravity_vec = Vector3()

onready var head = $Head
onready var camera = $Head/Camera
onready var crosshair = $Head/Camera/Crosshair
onready var ground_check = $GroundCheck
onready var aimcast = $Head/Camera/AimCast
onready var muzzle = $Head/gun20/MoveGun/Muzzle
onready var muzzleflash = $Head/gun20/MoveGun/Muzzle/Flash
onready var fireanim = $Head/gun20/Fire
onready var aimanim = $Head/gun20/Aim
onready var walkanim = $Head/gun20/Walk
onready var firesound = $Head/gun20/Shoot
onready var gun = $Head/gunLoc
onready var gunloc = $Head/gun20
onready var gui = get_parent().get_node("UI")
onready var gunAmmoLeft = $Head/gun20.ClipAmmo
onready var gunAmmoLefttostr = String($Head/gun20.ClipAmmo)
onready var extragunAmmo = $Head/gun20.ExtraAmmo
onready var extragunAmmotostr = String($Head/gun20.ExtraAmmo)
onready var currentWeapon = $Head/gun20.CurrentWeapon
onready var gunAmmoLeftText = get_parent().get_node("UI/HUD/Ammo/AmmoLeft").text
onready var gunExtraAmmoText = get_parent().get_node("UI/HUD/Ammo/ExtraAmmo").text

func _ready():
    camera.fov = 91
    gunAmmoLeftText = gunAmmoLeft
    yield(get_tree().create_timer(0.5), "timeout")
    mouse_sensitivity = mainsensitivity

func _input(event):
    if event is InputEventMouseMotion:
        rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
        head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity))
        head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89))

func _process(delta):
    gunloc.global_transform.origin = lerp(gunloc.global_transform.origin, gun.global_transform.origin, SWAY * delta)
    gunloc.rotation.y = lerp_angle(gunloc.rotation.y, rotation.y, SWAY * delta)
    gunloc.rotation.x = lerp_angle(gunloc.rotation.x, head.rotation.x, SWAY * delta)

func flash(obj, length):
    obj.visible = true
    yield(get_tree().create_timer(length), "timeout")
    obj.visible = false

func _physics_process(delta):

    direction = Vector3()

    if gui.get_node("Pause/Background/Resume").pressed == true:
        gui.get_node("Pause").visible = false
        mouse_sensitivity = mainsensitivity
        yield(get_tree().create_timer(0.05), "timeout")
        paused = false
    if Input.is_action_just_pressed("pause"):
        if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
            mouse_sensitivity = 0
            gui.get_node("Pause").visible = true
            paused = true
            mouse_sensitivity = mainsensitivity
            gui.get_node("Pause").visible = false
            yield(get_tree().create_timer(0.01), "timeout")
            paused = false
    if paused == false:
        if Input.is_action_just_pressed("aim"):
            crosshair.visible = false
            aiming = true

        elif Input.is_action_just_released("aim"):
            crosshair.visible = true
            aiming = false

        speed = Input.get_action_strength("move_forward") * 10

        if Input.is_action_just_pressed("fire"):
            if currentWeapon == "gun":
                gunAmmoLeft = gunAmmoLeft - 1
                #gunAmmoLefttostr = float(gunAmmoLeft)
                gunAmmoLefttostr = str(gunAmmoLeft)
                gunAmmoLeftText = gunAmmoLefttostr
            flash(muzzleflash, 0.2)
            if aimcast.is_colliding():
                var bullet = get_world().direct_space_state
                var collision = bullet.intersect_ray(muzzle.transform.origin, aimcast.get_collision_point())

                if collision:
                    var target = collision.collider
                    if target.is_in_group("Enemy"):
                        print("hit enemy")
                        target.health -= damage

        if ground_check.is_colliding():
            full_contact = true
            full_contact = false

        if Input.is_action_just_pressed("jump") and (is_on_floor() or ground_check.is_colliding()):
            gravity_vec = Vector3.UP * jump

        if Input.is_action_pressed("move_forward"):
            speed = Input.get_action_strength("move_forward") * 10
            direction -= transform.basis.y
        elif Input.is_action_pressed("move_backward"):
            speed = Input.get_action_strength("move_backward") * 10
            direction += transform.basis.y
        if Input.is_action_pressed("move_left"):
            speed = Input.get_action_strength("move_left") * 10
            direction -= transform.basis.x
        elif Input.is_action_pressed("move_right"):
            speed = Input.get_action_strength("move_right") * 10
            direction += transform.basis.x

    direction = direction.normalized()
    h_velocity = h_velocity.linear_interpolate(direction * speed, h_acceleration * delta)
    movement.z = h_velocity.z + gravity_vec.z
    movement.x = h_velocity.x + gravity_vec.x
    movement.y = gravity_vec.y

    move_and_slide(movement, Vector3.UP)
    if not is_on_floor():
        gravity_vec += Vector3.DOWN * gravity * delta
        h_acceleration = air_acceleration
    elif is_on_floor() and full_contact:
        gravity_vec = -get_floor_normal() * gravity
        h_acceleration = normal_acceleration
        gravity_vec = -get_floor_normal()
        h_acceleration = normal_acceleration

The problem in that code is in _ready.

func _ready():
    camera.fov = 91
    gunAmmoLeftText = gunAmmoLeft

Specifically, that last line, where your assign gunAmmoLeftText = gunAmmoLeft. I assume that gunAmmoLeft is an integer. So, from that point on, your gunAmmoLeftText variable is just an integer. So, when you later try to set its text property, you get the original error you reported.

jgodfrey is right, I will just add this tip! Since labels have a property text, if you name your labels as yourLabelText, you might missread it as yourLabel.text, leading to this kind of issues. I suggest LabelAmmoLeft or AmmoLeftLabel.

0 votes

look at on base 'int' on your error, this means that you are trying to set the text property on something that is an int, where it should be a Label Node. So the problem is not on the code snippet you provided, but before. Make sure gunAmmoLeftText is actually refering to your desired Label.

by (418 points)

this is what the variable is onready var gunAmmoLeftText = get_parent().get_node("UI/HUD/Ammo/AmmoLeft")

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.