Label3D array not appending more than once?

4.2.2

I have a section of code that despite being called every 7s via an append on a timer, only displays the first append. Can someone help?

Full code:
var tasks =
var taskdone =
var new_task = [“Make serum”, “Research”, “Study results”, “Hide”]
var rand_task = new_task.pick_random()

func _ready():
pass # Replace with function body.

func _process(delta):
if tasks.is_empty() != true:
for task_name in tasks:
%TaskBoard.text = str(task_name)

func _on_newtask_timeout():
tasks.append(rand_task)

for task_name in tasks:
    %TaskBoard.text = str(task_name)

This for loop goes through each task and sets the task board to that string. If we un-roll the loop it would look like this:

%TaskBoard.text = "Make serum"
%TaskBoard.text = "Research"
%TaskBoard.text = "Research"
%TaskBoard.text = "Make serum"
%TaskBoard.text = "Study results"
%TaskBoard.text = "Research"

Which results in only displaying the last “Research” right? You will need to add each task, I also recommend not doing this in _process as it can be rather expensive to set text labels and manipulate strings every frame.

func _on_newtask_timeout():
    tasks.append(new_task.pick_random())
    # create a new blank string
    var all_tasks: String = ""

    # add each task to the string
    for task_name in tasks:
        all_tasks += task_name + "\n" # \n makes a new line; like hitting enter, but for code

    # apply the accumulated string to the label
    %TaskBoard.text = all_tasks

Make sure to paste code with formatting:

1 Like

I think the problem here is that each time you loop over an item in tasks, you’re setting the text property of your %TaskBoard to that value. That means that it’ll replace the value on each iteration of the loop rather than add the text to it.
You’re also setting rand_task to the result of new_task.pick_random() at the start, but not calling the function again, so the value will always remain the same.

Those two things would mean that the text in %TaskBoard will always remain the same.

I’d suggest updating the value of rand_task in your _on_newtask_timeout so that each time it picks a random value, and updating the logic to set the %TaskBoard text value to concatenate the strings of each task.

1 Like