Random beginner-question: Invalid operands 'String' and 'int' in operator '+'.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

Hi everyone,

depending on entered numbers (coming from a simple Label) a calculation is going on within a Timer node to determine the timer duration.

	
	...
	var entered_numbers = get_node(the_entered_numbers).text

	result_for_timer_duration = 3600 / float (entered_numbers)

	set_wait_time(result_for_counter_duration)
	start(result_for_counter_duration)


func _on_Timer_timeout():
	print ("timeout")

So this prints in the „rhythm“, slowly or quickly, depending on the entered numbers.

What I want is another Label to +1 on every timeout, so I added a Label as a child to the Timer node („AddUpLabel“) and after the print this line of code:

    	$AddUpLabel.text += 1

which throws the error: Invalid operands 'String' and 'int' in operator '+'.

What am I missing here?

I don’t really understand. Do you want to take the add-up text and increment it? Maybe try this?

$AddUpLabel.text = str(int($AddUpLabel.text) + 1)

Sorry if this isn’t what you meant. Also, this implementation is kind of weird. maybe have a seperate variable you increment and set $AddUpLabel.text to that variable.

CollyBlargle | 2021-07-26 10:37

Perfect, thank you!
(Still a noob, I see what’s going on when shown, but can’t quite get there on my own yet…)

pferft | 2021-07-26 10:57

Please stop adding "Random beginner-question: " … it is of no use (you are not posting a random question) and makes search for ‘random’ harder.

Thanks :slight_smile:

clemens.tolboom | 2021-07-26 12:14

You’re right! I’ll skip this in the future.

pferft | 2021-07-26 13:38

:bust_in_silhouette: Reply From: anuke

Yes @CollyBlarge right. You have to do type conversion.

var counter = 0;
...
counter += 1
$AddUpLabel.text  = "Some Text label" + String(counter)