EDRO
EDRO

Reputation: 22

Invalid set index z with value of type float in gdscript

im making my first 3d game for a jam and i was writing some code about the player state machine, it was all working, but when i put this code in the state_run(delta) func, it crashes:

if playerIsMoving == false: initialize_idle()

it gives me the error invalid set intex z (base int) with value type of float.

Code:


extends KinematicBody
 
 
var sensitivity = 0.06
var speed = 10
var h_aceleration = 6
var gravity = 20
 
 
var direction = Vector3()
var h_velocity = Vector3()
var movement = Vector3()
var grav_vec = Vector3()
 
onready var head = $Head
 
 
 
enum STATES { IDLE, RUN, ATTACK, DEAD}
var state_cur : int
var state_nxt : int
var test = Vector3.ZERO
var playerIsMoving = false
 
 
 
func _ready():
       
        Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
        state_nxt = STATES.IDLE
 
 
func _input(event):
       
        if event is InputEventMouseMotion:
               
                rotate_y(deg2rad(-event.relative.x * sensitivity))
                head.rotate_x(deg2rad(-event.relative.y * sensitivity))
                head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89))
 
func _physics_process(delta):
       
        if state_nxt != state_cur:
               
                state_cur = state_nxt
       
       
       
       
       
        match state_cur:
               
                STATES.IDLE:
                        state_idle(delta)
                STATES.RUN:
                        state_run(delta)
                STATES.ATTACK:
                        pass
                STATES.DEAD:
                        pass
       
       
       
        print(state_cur)
       
       
        if Input.is_action_pressed("forward") or Input.is_action_pressed("backwards") or Input.is_action_pressed("left") or Input.is_action_pressed("right"):
                playerIsMoving = true
        else:
                playerIsMoving = false
       
       
       
       
        if not is_on_floor():
               
                grav_vec += Vector3.DOWN * gravity * delta
               
        else:
               
                grav_vec = -get_floor_normal() * gravity
 
 
 
 
 
 
func initialize_idle():
        state_nxt = STATES.IDLE
        movement = 0
 
 
func state_idle(delta):
       
        if Input.is_action_pressed("left") or Input.is_action_pressed("right") or Input.is_action_pressed("forward") or Input.is_action_pressed("backwards"):
                initialize_run()
               
 
func initialize_run():
        state_nxt = STATES.RUN
        pass
 
 
func state_run(delta):
       
       
       
        direction = Vector3()
       
        if Input.is_action_pressed("forward"):
                direction -= transform.basis.z
        elif Input.is_action_pressed("backwards"):
                direction += transform.basis.z 
        elif Input.is_action_pressed("left"):
                direction -= transform.basis.x
        elif Input.is_action_pressed("right"):
                direction += transform.basis.x
        else:
               
                playerIsMoving = false
       
        direction = direction.normalized()
        h_velocity = h_velocity.linear_interpolate(direction * speed, h_aceleration * delta)
        movement.z = h_velocity.z + grav_vec.z
        movement.x = h_velocity.x + grav_vec.x
        movement.y = grav_vec.y
       
        move_and_slide(movement, Vector3.UP)
       
        if playerIsMoving == false:
                initialize_idle()
       

I alredy tried to round the value or use the int() but it isnt working, the code is giving error on the "movement.z = h_velocity.z + grav_vec.z"

Upvotes: 1

Views: 761

Answers (1)

Theraot
Theraot

Reputation: 40285

Types.

Look, movement is a Variant, initialized to a Vector3:

var movement = Vector3()

And here you use it as a Vector3:

        movement.z = h_velocity.z + grav_vec.z
        movement.x = h_velocity.x + grav_vec.x
        movement.y = grav_vec.y
       
        move_and_slide(movement, Vector3.UP)

But here 0 is an int not a Vector3:

        movement = 0

So after that movement... Continues to be a Variant, but now it has an int value. So when you use it as a Vector3 it fails. Because an int does not have x, y, z.


You can declare movement to be a Vector3 explicitly:

var movement:Vector3 = Vector3()

Or implicitly (inferred from the value you assigned):

var movement:= Vector3()

And then Godot will tell you that this line:

        movement = 0

Is an error, because you are trying to set an int to a Vector3.


My guess is that you want to set movement to the zero vector, which is like this:

        movement = Vector3(0.0, 0.0, 0.0)

Or like this:

        movement = Vector3.ZERO

Or - similarly to how you initialized it - like this:

        movement = Vector3()

Upvotes: 1

Related Questions