I’m currently learning code and Godot! I’m making an rpg type game, at the moment I’m looking for a way to store potion data. If any of you have played Path of Exile. I want to find a way to store many different flasks and have there charges clamped so they don’t go below 0 and don’t go above 4.
I’ve tried dictionaries and arrays but I’m not sure I’m using them right in order to get the effect I want. I also tried making a clamped variable which works but it seems very obscurely written.
My question is, is there any better way to store this kind of data? Something that I’m constantly gaining charges and using these charges.
I would like to make it so i cannot go into the negatives for current charges. For example how do you make it so in your game you dont go negative bullets in your gun?
In my game, the weapon cannot fire if the magazine has 0 bullets, so no ammo is subtracted.
For your flasks, you could do something like:
# Subtracts the amount of charges to a minimum of 0
# This will subtract the entire amount even if there are fewer available
func subtract_charges(amount):
subtracted_charges = current_charges - amount
current_charges = max(0, subtracted_charges)
Alternatively, you can safeguard a negative number:
# Subtracts the amount of charges if available
# if there are fewer charges available, return false
func subtract_charges(amount) -> bool:
var has_subtracted = false
if current_charges >= amount:
current_charges -= amount # 0 or more will remain
has_subtracted = true
else: # not enough charges, return false
has_subtracted = false
return has_subtracted
For the last function, you can then do:
var subtract_succeeded = subtract_charges(4)
if subtract_succeeded:
print("subtracted charges")
else:
print("not enough charges")
Since you are still learning Godot, my first suggestion will be to use resources. I can’t give any specific suggestions about them because I don’t use them.
For my project I am using a singleton to manage the data as well. I am loading an items.json file using this class that manages the items list:
Each item in the json file is then instantiated into a DItem class, so I’m not using the json directly. The DItem class is not an item instance, it only holds data. I use a similar pattern for maps, mobs and furniture.