#Godot setter function does not have the requirements so it falls to zero

5 messages · Page 1 of 1 (latest)

novel ferry
#

Sooo, i was just setting this, and it works like a charm in-editor, but the second i run it ingame all item quantities go to zero, i figured it was because the quantity is set to zero if the item data does not have the name i put in the inspector, but i mean, that IS what should happen, but it appears that when I run the game, the quantity is set before the name making it to be set to zero, and i have no idea for fixing it.(Awkple and Beshrie are the only items in ItemData, and sorry for the long message...)

extends Resource
class_name ItemStack

@export var item_name : String:
    set(value):
        item_name = value
        if ItemData.item_data.has(item_name):
            item_quantity = clamp(item_quantity, 0, ItemData.item_data[item_name]["StackSize"])
        else:
            push_warning("Item name is not any item in the game!")
            item_quantity = 0

@export var item_quantity : int :
    set(value):
        item_quantity = value
        if ItemData.item_data.has(item_name):
            item_quantity = clamp(value, 0, ItemData.item_data[item_name]["StackSize"])
        else:
            item_quantity = 0

func isEmpty() -> bool:
    return item_quantity == 0```
That console image is what happens when i print in a for loop, the item name and quantity after
i have 3 slots but the update is called twice at the start, thats why they repeat
brisk ocean
#

In your setters, you can add a line just after the value setting: if not is_node_ready(): await ready I've had to use this to fix multiple different issues (mostly related to tool scripts), but it might help here, as well.

novel ferry
#

How could i use is_node_ready(): in the case of this being a resource?

novel ferry
#

Still, thanks!(I found a not so secure way but if I don't commit any errors while setting items, everything should be fine)

brisk ocean
#

Ah, I didn't realize it was a resource (derp). So, with a resource, you need to click the dropdown at the top of it, and select "make unique" unless you assigned them right there. If this is a saved custom resource (as a .tres file) and you just added the same .tres fil to each one of these slots, you will see the changes live in the editor, but it will 'load' what was in the file before you made those changes. Where, if you select "make unique" it just binds the resource to the scene its in, and considers it a unique instance.