Printing a variable returns multiple values

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



func _randomize():
	var a = 0
	a = randi()%10 + 1
	$RichTextLabel.set_text(str(a))
	print(a)
func _process(delta):
	if Input.is_action_just_pressed("ui_accept"):
		_randomize()

When the label text is set to “a” and when “a” is printed I am given two values. I have tested this on other nodes and with many variables, with 2 separate values being the minimum. I know I messed up somewhere because i only get one value in a new project and I know the issue of a declared variable being edited by multiple functions. I apologize if this is common sense, as I am new to coding and have no idea how to fix this issue. Any help would be appreciated, thank you.

It seems likely you have this script attached to more than one node, so when you press the key, the function is called on both of them.

kidscancode | 2020-11-29 20:09

This code is attached to a KinematicBody2D that is a child to the main world scene. Should the KinematicBody2D have its own separate scene? I have been putting most things into one scene while nodes that I create have their own scene eg. bullets, afterimage, asteroids. Thank you for the response and all of the help you have provided with tutorials and answers to other users.

edit: The code was originally on a KinematicBody2d but I made a separate node2d and a new script to see if I still got 2 values. That’s why the scrips above says Extends Node2d.

Stratos | 2020-11-29 20:28

change it to prints(self, a) and run it. you will likely see that you have two nodes running the same script at the same time.

scrubswithnosleeves | 2020-12-02 04:34