Inventory System - Using Items

Godot Version

Godot-4

Question

I have made resources for items, inventory and inventory slots that are hold in an array. I have a working pick up and hotkeys for the slots, I managed to clear the slot when using the item but I’m having a hard time making the item influence player for example increasing his speed. What would be the best way to implement this?

Hey
Well it really depends on your implementation

But your item could have a use method that takes your player data, something like:

class_name SpeedPotion
extends Item

var magnitude: int

func _init(p_magnitude: int = 1) -> void:
    magnitude = p_magnitude


func use(p_user: PlayerData) -> bool:
    p_user.speed += magnitude
    return true

with a calling function like :

class_name Inventory
extends Resource

var items: Dictionary = {}

func _init() -> void:
    items["speed_potion"] = SpeedPotion.new(3)


func use_item(p_user: PlayerData, p_item_name: String) -> bool:
    if items.has(p_item_name) == false:
        return false

    var item = items[p_item_name)

    if item.use(p_user) == false:
        return false

    items.erase(p_item_name)
    return true

Of course it is not copy pastable, I made a LOT of assumptions but it should give a general idea.

I hope it helped, good luck!

1 Like

I created a far simpler system for some boots:

func _on_body_entered(body):
	global.jump_boots = 150
	queue_free()

And the effect:

if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = (jump_velocity - global.jump_boots)

The result is that after the player picks up the boots the player can jump 1/3 higher and reach some previously out of reach areas. :smile:

1 Like

Hey,
I’d advise you to modify the player stats instead of having code scattered all over the place for specific items and actions.

If you were to add logic with different boots that increase via a different factor or boots that make you unable to jump, it would become unmanageable.

In my opinion, having a player.jump_velocity_modifier field will lead to better structured code that is easier to update.

I’m not saying your solution doesn’t work though, I’m just trying to think about scalability.

I hope this helped!

2 Likes

Agreed - thank you.

I love global variables for that reason - where they’re appropriate. I can quickly check and adjust a lot of items in my game in one place.

2 Likes

Thank you for help guys. I managed to make it work for now, and will continue to polish the system :love_you_gesture:

1 Like