Potions Flasks. How to store data and potion charges?

Godot Version

4.3

Question

Hello all!

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.

Using dictionaries can be fine. I use the max ammo property of a magazine to limit the amount that can fit in it:

I load them in by using somewhat intermediate code and it works for my use case.

If you’re learning to code and use Godot, a more easily accessible way would be Resources. Here are some videos explaining the concept:
https://www.youtube.com/results?search_query=godot+resources&sp=EgIIBQ%253D%253D

Personally I am not using resources but only dictionaries and arrays because my project requires it. Each project is different.

that looks similar to how i wrote my flask system.

so i have this so far,

var Flasks : Dictionary = {

“Speed” = {
“min_charges” : 0,
“max_charges” 4,
“current_charges” : 3,
}
}

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")

Alright that makes sense. Thanks! While I have you. how would you suggest i store this data? currently i have it loaded as a singleton at the moment.

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.

Gotcha, ill look into resources and compare them. Thanks for your help!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.