Godot Version
4.2.2
Question
For whatever reason, when HP is set to 13, it shows 6 bars (of HP), when it should show 6 bars and 1 half bar. It should only show 6 bars if HP is set to 12. When HP is set to 14, it shows 7 bars and 1 half bar when it should be showing 7 bars. At 15 it shows 7 bars and 1 half bar, which is correct.
entire code:
extends Node2D
@export var maxHP: int = 18
@export var HP: int = 15
var eB: Texture2D = preload("res://images/healthNone.png")
var hB: Texture2D = preload("res://images/healthHalf.png")
var fB: Texture2D = preload("res://images/healthFull.png")
var tempatlas
var regions = [
Rect2(Vector2(238, 0), Vector2(18, 16)),
Rect2(Vector2(224, 0), Vector2(32, 16)),
Rect2(Vector2(210, 0), Vector2(46, 16)),
Rect2(Vector2(196, 0), Vector2(60, 16)),
Rect2(Vector2(182, 0), Vector2(74, 16)),
Rect2(Vector2(168, 0), Vector2(88, 16)),
Rect2(Vector2(154, 0), Vector2(102, 16)),
Rect2(Vector2(140, 0), Vector2(116, 16)),
Rect2(Vector2(126, 0), Vector2(130, 16)),
Rect2(Vector2(112, 0), Vector2(144, 16))
]
# Called when the node enters the scene tree for the first time.
func _ready():
updateHP()
func updateHP():
tempatlas = AtlasTexture.new()
tempatlas.atlas = eB
tempatlas.region = regions[int(truncate_string_at_character(str(maxHP/2), ".")) - 1]
$EmptyBar.texture = tempatlas
tempatlas = AtlasTexture.new()
tempatlas.atlas = hB
tempatlas.region = regions[add_to_odd(int(truncate_string_at_character(str(HP/2), "."))) - 1]
$HalfBar.texture = tempatlas
tempatlas = AtlasTexture.new()
tempatlas.atlas = fB
tempatlas.region = regions[int(truncate_string_at_character(str(HP/2), ".")) - 1]
$FullBar.texture = tempatlas
func truncate_string_at_character(input_string: String, target_character: String) -> String:
var index = input_string.find(target_character)
if index >= 0:
return input_string.left(index)
else:
return input_string
func add_to_odd(num: int) -> int:
if num % 2 == 1:
return num + 1
else:
return num
Something about updateHP() seems to be the problem.