Hi I am very new to programing (very) and to challange myself I am tryng to program a small Idle game. I am following some tutorials to slowly build the game. However I am unable to find any guids on how to set max capacity for one of my in game resources. I have a resource called "water" and a scripts that handles this resource. I can press a button to increase the amount of water but I want to make a max capacity so when the amount of water reaches that number it can't get any higher. Do I need a new cript ? How would a code like that look like ?
You can clamp it or check the max value using min comparison like this:
var max_water: float = 100.0
var current_water: float = 0.0
# then whenever you are adding or removing (-ve) an amount:
current_water = clamp(current_water + amount, 0, max_water)
# or just check the max value
current_water = min(current_water + amount, max_water)
Clamp ‘clamps’ the value of the given amount to within the two given values.
Min returns the smaller of the two, max returns the biggest of the two.
var min_water: float = 0.0
var max_water: float = 100.0
var current_water: float:
set(value: float):
current_water = clampf(value, min_water, max_water)
This way, whenever you try to change the value of your current_water, it will automatically perform clamping for you, you don’t need to remember to do that anywhere else in your code base.