Signal only emitting in _ready function

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

Hello, i’m new to Godot and i’m trying to convert my work from unity over to Godot but I seem to be stuck on an issue with my inventory and toolbar system where the signal will not emit.

When I press 1 or 2 it will grab the item within my inventory and run Equip(), which works fine, but it doesnt seem to pass the signal. It seems to only pass the signal if I move this code into the _ready() function, which is not what I want.

I have connected the signal through the Node tab

ToolManager.gd

extends Node2D

func _on_Toolbar_player_swinging_tool(item_tool):
  add_child(item_tool)
  item_tool.global_position = position

ToolBar.gd:

extends Node2D

signal player_swinging_tool(tools)
var inventory = load("res://Player/Inventory.tres")
var myItem = null
export (int) var selectedTool = 0
export (int) var toolbarSize = 6

func NavigateToolBar():
  if Input.is_action_just_pressed("NavigateLeft"):
	if(selectedTool <= 0):
		selectedTool = toolbarSize
	else:
		selectedTool -= 1
		
	Equip()

if Input.is_action_just_pressed("NavigateRight"):
	if(selectedTool >= toolbarSize):
		selectedTool = 0
	else:
		selectedTool += 1

	Equip()
	
myItem = inventory.items[selectedTool]


func Equip():
  #Checking if item is a Tool
  if myItem is ItemResource and myItem.type == myItem.ItemType.TOOLS:
	var tools = myItem.Tool
	var item_tool = tools.instance()
	
	emit_signal("player_swinging_tool", item_tool)

Are you sure that the signal is connected to your ToolManager.gd script ?

Legorel | 2021-08-12 16:10

where is your NavigateToolBar(): func called?

Wakatta | 2021-08-20 10:38

Thank you for all your help, I managed to get it fixed. It was an issue to do with how I was calling my toolbar function. As for doing the function under an event, that looks like a better way of doing inputs, I will do that for now on thanks.

I3randon5 | 2021-08-23 09:29

:bust_in_silhouette: Reply From: Wakatta

The Input class is only ever properly used in the _process function and though it can be used elsewhere it is very easy to miss the timing.

Try using the _input(event): function instead with keycodes

Example

func _input(event):
    if event is InputEventKey and event.pressed:
        if event.scancode == KEY_1:
            if(selectedTool <= 0):
                selectedTool = toolbarSize
            else:
               selectedTool += 1
            Equip()
        if event.scancode == KEY_2:
            if(selectedTool >= toolbarSize):
                selectedTool = 0
            else:
                selectedTool += 1
            Equip()