#Use items that are selected

1 messages · Page 1 of 1 (latest)

finite zodiac
#

I watched a hotbar tutorial and it worked quite nicely, however im having trouble isolating code based on what item is selected, and it doesnt have a second tutorial, i would like to do, for example, something like a key where you can only unlock if the key is selected or a system where you can observe a specific item closer
Does anyone know how i could identify and do if codes based on what item is selected?
I tried doing if ItemData.item_name == ("name") but it didnt work
It was this one: https://youtu.be/JGzzflXDEPE?si=lTjqavyH1KogVB-I

Interactable 3D Item Pickup & Drop from Hotbar, Inventory System using signals, and implementing Viewmodel for the Item in Godot 4

0:00 - Preview
0:24 - ItemData Resource
1:20 - Inventory (Hotbar)
5:24 - Interactable
7:49 - Item Dropping
10:00 - Viewm...

▶ Play video
hexed island
#

I haven't watched the video but here is what I would do.

The hot bar is just a dictionary of items. The key is the position and the value is a reference to the item.

When we press the use button, we do var selected_item = hotbar.get(currently_selected_index)

If the selected_item is null, we don't have an item in that position and we just return
If it isn't null, we just call selected_item.use.

Every item should inherit from a Base_Item class that defines a function named use but simply returns nothing.

In the actual items, we override the use function and do whatever we want with it.

So, for your key example, we could have each key store the door they connect to. Our use function would look something like this

func use() -> void:
        if can_unlock_door:
               EventBus.door_unlocked(door_name)

(Sorry if that looks awful,I'm on Mobile and don't have a tab key on my keyboard 😭)

In that function,the can_unlock_door variable is a boolean that updates whenever we get within the vicinity of the door that we save under door_name.

When we enter the vicinity of the door, the door emits a signal that the player has entered its vicinity and passes its name as an argument. The key then changes the variable to true if the door_name that it has is the same as the door_name that was just emitted.

The door unlocked signal is simply listened for by every door and if its name is the same as the signal that just got sent, the door gets unlocked.

hexed island
#

Ok I watched the video. You don't need a dictionary, an array works

#

your BaseItem class would look smth like this

# We define this class as abstract, it cannot be instantiated from, only inherited
@abstract class_name BaseItem

#Every function will have its own implementation of this function. It is abstract because *every* item must have a use function defined
@abstract func use()

@export var item_data: Resource

#Not every item can be inspected so we dont make this function abstract, we leave it blank so it can be overriden
func inspect() -> void:
    return
    
    
# These functions will stay the same as whatever you already have. They do not get overriden
func interact():
#    Whatever interact code you have
    
func find_first_mesh_instance(node: Node):
#    Whatever code goes here
finite zodiac
#

Sorry if im being dumb but how would this connect to the other class that the tutorial made?

#

Also i tried doing @abstract but it said unrecognized annotation

finite zodiac
#

Lemme update my godot to 4.5 rq and then ill test the @abstract

hexed island
#

Yeahhh it's 4.5 only!

hexed island