Topic was automatically imported from the old Question2Answer platform.
Asked By
Numshv
So i basically trying to make a calendar here and what i want to do is so that when i press the next month button, the text of the current month label will change to the next month.
So here’s the code of my monthlabel node. I use regular label node for this and I’ve add it into the autoload.
extends Label
var time = OS.get_datetime()
var currentmonth = time["month"]
var namemonth = ["null", "Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember"]
var monthname = str(namemonth[currentmonth])
func _ready():
text = monthname
Then I thought this would work
But i guess it doesn’t and i don’t know what to do.
Using the above workflow you will never need to use Autoloads once you know where the node is in the SceneTree and how to access it using get_node, find_node, get_child, find_parent / child.
Caution
Don’t rely on global vars for everything and instead use functions
Answer
extends Label
var namemonth = ["null", "Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember"]
func _ready():
text = get_month_name()
func get_month_name(itr = 0):
var time = OS.get_datetime()
var currentmonth = time["month"] if itr == 0 else itr
return str(namemonth[currentmonth])
Explanations
The line
var currentmonth = time["month"] if itr == 0 else itr
is a ternary which goes like
# if itr variable is equal to 0
# currentmonth = time["month"]
# else
# currentmonth = itr
The line func get_month_name(itr = 0): allows the function to be called without variables
by pre-setting itr to 0
On the back of what @Gluon has said, you don’t have to attach scripts to everything you want to control.
You could have all the code in the script attached to MainScene and reference the text label like this: