0 votes

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)
Godot version 3.3.2.stable.mono
in Engine by (19 points)

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

where is your NavigateToolBar(): func called?

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.

1 Answer

0 votes
Best answer

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()
by (6,876 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.