![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Halflove |
Hey there!
So I’m basically trying to change a variable in one script/class via another script. However, for some reason I can only access constants from the first class and not variables:
extends KinematicBody2D
class_name Player
var velocity = Vector2(0, 0)
const speed = 60
const decel = 0.3
const gravity = 19
const jumpForce = -230
var speedMod = 1.0
var canMove: bool = true
var currentState: String = "default"
Other script:
extends Area2D
class_name ChopOakTree
var canInteract = false
var chopped = false
var respawnTimer = 600
var timer = 0
# Sets tree status to chopped or grown depending on timer status
func adjustChoppedStatus(input: String):
# chopped bool must be disabled when the tree is chopped so that
# the player enter and exit events won't affect the displayed text
# Change sprite animations and timer status dependant on input
if(input == "start"):
Player.currentState = "chop"
Player.canMove = false
elif(input == "chopped"):
$AnimatedSprite.play("chopped")
Player.currentState = "default"
Player.canMove = true
timer = respawnTimer
chopped = true
elif(input == "grown"):
$AnimatedSprite.play("default")
get_node("Node2D/UI/Label").text = ""
chopped = false
(I shortened the scripts a little bit to only show the pertinent info)
So as you can see in the ChopOakTree class I’m trying to access the currentState and canMove variables from the Player class. However, it won’t let me access or modify them, It returns with the error:
Invalid set index 'currentState' (on base: 'GDScript') with value of type 'String'.
I am really confused about this, why can I get the value of a constant from the Player class, but I can’t get the value of a variable from the Player class?
Thanks in advance