Variable change but get back to the original declared value

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

im having an issue with the following code:

 var item_id = 0
    
 func collect_item(id):
 	item_id = id
  	print(item_id)
    
 func _physics_process(delta):
   	print(item_id)

when i start the scene it prints ‘0’, and when i call the collect_item function it prints the input value i defined then go back printing ‘0’ again. I need to change the variable value until i call the collect_item function once more.

EDIT

i could solve the problem thanks to the user “cybereality” from godotforums.org the problem was that i the connection was being made the wrong way here is the full answer got there:

It looks like it has something to do with how you connect the signals. I made this simple example that works.

Node2D (for the player or parent object):

extends Node2D
signal Collect
var item_id = 1
func _ready():
    connect("Collect", self, "collect_item")
func _physics_process(delta):
    print("process: ", item_id)
func collect_item(var id):
    item_id = id
    print("collected: ", item_id)

Another Node2D as the child of the first object:

extends Node2D
signal Collect
onready var player = get_parent()
func _input(event):
    if event.is_action_pressed("click"):
        player.emit_signal("Collect", 777)

And I added an input event when the mouse is clicked called “click”. Works fine.

From what you give here, the code seems fine. Without more codes can’t really help you

lowpolygon | 2020-01-10 08:26

Here is the full code of the Player.gd, and after that the Collectable.gd (the script that triggers the “collect_item(id)” function)

extends KinematicBody2D

const ACCELERATION = 10
const MAX_SPEED = 120
const GRAVITY = 10
const JUMP_POWER = -150
const FLOOR = Vector2(0, -1)

const FOCUS_SPEED = 1
var focus_in = 1
var focus_out = 0.4

var velocity = Vector2()
var direction = true
var on_ground = true
var double_jump = false
var item = "No item"
var item_check = false
var item_id = 1

##COLLECT
func collect_item(id):
	print("previous: %s" %item_id)
	item_id = 9999
	print(item_id)

func _physics_process(delta):
	#print(item)
	print(item_id)
###DIREITA
	if Input.is_action_pressed("ui_right"):
		$AnimatedSprite.flip_h = false
		if on_ground:
			$AnimatedSprite.play("run")
			velocity.x = min(velocity.x + ACCELERATION, MAX_SPEED)
			direction = true
		else:
			if direction:
				if (velocity.x > 50):
					velocity.x = min(velocity.x, MAX_SPEED)
				if (velocity.x < 50):
					velocity.x = 50
			else:
				direction = true
				if (velocity.x > 0):
					velocity.x = min(velocity.x / 2, MAX_SPEED / 2)
				if (velocity.x <= 0):
					velocity.x = 50
				
		if sign($Position2D.position.x) == -1:
			$Position2D.position.x *= -1
###ESQUERDA		
	elif Input.is_action_pressed("ui_left"):
		$AnimatedSprite.flip_h = true
		if on_ground:
			$AnimatedSprite.play("run")
			velocity.x = max(velocity.x - ACCELERATION, -MAX_SPEED)
			direction = false
		else:
			if !direction:
				if (velocity.x < -50):
					velocity.x = max(velocity.x, -MAX_SPEED)
				if (velocity.x > -50):
					velocity.x = -50
			else:
				direction = false
				if (velocity.x < 0):
					velocity.x = max(velocity.x / 2, -MAX_SPEED / 2)
				if (velocity.x >= 0):
					velocity.x = -50
				
		if sign($Position2D.position.x) == 1:
			$Position2D.position.x *= -1
			
###PARADO
	else:
		if on_ground:
			$AnimatedSprite.play("idle")
			velocity.x = lerp(velocity.x, 0, 0.2)
		else:
			velocity.x = lerp(velocity.x, 0, 0.05)

###PULO
	if Input.is_action_just_pressed("ui_up") && on_ground:
		velocity.y = JUMP_POWER
		$AnimatedSprite.play("jump")
		on_ground = false
		double_jump = false
	elif Input.is_action_just_pressed("ui_up") && !on_ground && !double_jump:
		velocity.y -= 100
		$AnimatedSprite.play("jump")
		double_jump = true
	
###ITEM TAB
	if Input.is_action_pressed("ui_focus_next"):
		focus_in = lerp(focus_in, 0.4, FOCUS_SPEED * delta)
		if focus_in != 0.4:
			$Camera2D.set_zoom(Vector2(focus_in, focus_in))
			focus_out = focus_in
		$Camera2D.offset_v = 1
		$AnimatedSprite/Control/Item_label.visible = true
		$AnimatedSprite/Control/Item_label.text = item
		
	else:
		focus_out = lerp(focus_out, 1, FOCUS_SPEED * delta)
		if focus_out != 1:
			$Camera2D.set_zoom(Vector2(focus_out, focus_out))
			focus_in = focus_out
		$Camera2D.offset_v = 0
		$AnimatedSprite/Control/Item_label.visible = false
	
	velocity.y += GRAVITY ### EFEITO GRAVIDADE
	
	if is_on_floor(): 
		on_ground = true 
	else: 
		on_ground = false
		if velocity.y < 0:
			$AnimatedSprite.play("jump")
		else:
			$AnimatedSprite.play("fall")

	if direction:
		$Camera2D.offset_h = 1
	else:
		$Camera2D.offset_h = -1

	velocity = move_and_slide(velocity, FLOOR)

Collectable.gd

extends Area2D
#const MyNode = preload("Player.gd")
onready var player = load("Player.gd").new()

signal Collect

var trigger = false

func _on_Collectable_body_entered(body):
	connect("Collect", player, "collect_item")
	trigger = true
	
func _on_Collectable_body_exited(body):
	trigger = false

func _input(event):
	#player.connect("Collect", player, "collect_item")
	if trigger:
		if event is InputEventKey:
			if event.scancode == KEY_ESCAPE:
				queue_free()
				emit_signal("Collect", 1)

JVStorck | 2020-01-10 17:47

:bust_in_silhouette: Reply From: Jason Swearingen

I am guessing that item_id in collect_item() is creating a new variable, not reusing the class variable you defined on the first line.

try this to see if I’m correct:

 var item_id = 1

 func collect_item(id):
    print("previous id=",item_id)
    item_id = id
    print("new id=",item_id)

 func _physics_process(delta):
    print("physic id=",item_id)

If my guess is correct, then the print("previous id=",item_id) line will not print 1.

unfortunately i think its not that problem, the output of the physicsprocess() function is ‘1’,
then i trigger the collectitem() and it prints “previous = 1” “new = id”, then when it get back to physicsprocess() the print is ‘1’ again.

JVStorck | 2020-01-10 17:52