Skip to content

Tutorial Bar

Kamran Wali edited this page Dec 2, 2024 · 2 revisions

Added bar feature which acts like any normal bar. This can, for example, be used for character health. The get_normal() method in the bar is a powerful function that allows you to sync the bar with any other features. Below I will briefly explain what each method does.

  1. void set_max(int value) - This method sets the maximum limit for the bar. The max limit can NOT be less than 1. If a value of less than 1 is given the bar will make the max limit to 1. If this method needs to be called then calling it once when the scene is ready is recommended. This will make sure to avoid any wrong results. Also make sure that this method is called before set_current(value) method. Otherwise setup might give wrong results.
  2. void set_current(int value) - This method sets the current value of the bar. The current value can NOT be less than 0 or more than max value. If a value of less than 0 is given then the current value will be set to 0. If a value of more than max is given then the current value will be set to max value. If this method needs to be called then calling it once when the scene is ready is recommended. This will make sure to avoid any wrong results. Also make sure that this method is called after set_max(value)(int value) method. Otherwise setup might give wrong results.
  3. float get_normal() - This method gets the normal value for the bar which is in the range of 0 to 1. You can use this value to sync other features with the bar.
  4. void add(int value) - This method adds value to the bar's current value. The current value of the bar will never go above maximum value.
  5. int get_value_max() - This method gets the maximum value of the bar.
  6. int get_value_current() - This method gets the current value of the bar.
  7. bool is_full() - This method checks if the bar is full that is current value equals to max value.
  8. bool is_depleted() - This method checks if the bar is empty that is current value equals to 0.
  9. void restore() - This method makes the bar full again that is current value equals to max value.
  10. void subtract(int value) - This method removes value from the bar's current value. The current value of the bar will never go below 0.

I have added different type of bars with different functionalities. I will explain how to use them briefly below.

  1. normal_bar - This bar acts like a normal bar which means that adding a value will just add the value and subtracting a value will just subtract it. To use this bar make sure to set the maximum value at the scene start otherwise the default value will be used as maximum which is 1. Optionally you can set the current value as well if you want to at the scene start otherwise its value will be 0.
SomeScript.gd

extends Node
@export var bar: COP_BaseBar

func _ready() -> void:
  bar.set_max(10)
  bar.set_current(10) # Optional: If you also want to set the current value at the start but it is NOT necessary if NOT needed

## This method hurts the character with the given value.
func hurt(value: int) -> void:
  bar.subtract(value) # Removing health by subtracting bar's current value

## This method heals the character with the given value.
func heal(value: int) -> void:
  bar.add(value) # Adding health by adding to bar's current value

## This method checks if the character is dead or NOT.
func is_dead() -> bool:
  return is_depleted()

## This method returns the health's normal value so that the UI bar can be synced.
func get_health_normal() -> float:
  return bar.get_normal() # The health's normal value which is the bar's normal value
  1. normal_bar_values - This bar is same as the normal_bar in functionalities. The only difference is that you must provide the max and current value in the script itself. It has two properties which are COP_FixedFloatVar _max and COP_FixedBoolVar _is_set_cur_value. The _max property takes in a COP_FixedFloatVar value which is the maximum limit for the bar and the value MUST be greater than 1. The _is_set_cur_value property takes in a COP_FixedBoolVar value which is the flag that decides if to set the current value of the bar as max value or NOT. True means the current value at the start will be same as the max value. False means current value will be 0 at the start. You don't need to call the set_max(value) and set_current(value) methods at the start but you do have the option to do so if you want to. You can use the code example in 1. normal_bar and just remove func _ready() method from it.
Clone this wiki locally