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 collectitem 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 collectitem 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.