# Import the necessary Verse libraries using /Fortnite.com/Devices using /Engine/Engine # Define our Main Device. This is the "boss" object in our scene graph. # It will hold the UI widget and the logic. device HealthBarController < # This is a reference to the UI Widget in our level. # Think of it as the "container" for our health bar. Widget: WidgetDevice = WidgetDevice() # This is the Material Instance we created. # We need to expose it so Verse can talk to it. Material: MaterialInstance = MaterialInstance() # The player we're tracking. Player: Player = Player() > # This function runs once when the island starts. OnBegin(): void = # Wait for the player to spawn Player.SpawnedEvent += () -> # Every time the player's health changes, update the bar Player.HealthChangedEvent += (new_health: float, max_health: float) -> UpdateHealthBar(new_health, max_health) # The core logic: Update the visual appearance UpdateHealthBar(current_health: float, max_health: float): void = # Calculate percentage (0.0 to 1.0) # Analogy: If you have 100 HP and take 50 damage, you're at 0.5 (50%) health_percent: float = current_health / max_health # Determine color based on health # If health is > 50%, it's green. If < 50%, it's red. target_color: vector4 = if (health_percent > 0.5) then vector4(0.0, 1.0, 0.0, 1.0) # Green (R, G, B, Alpha) else vector4(1.0, 0.0, 0.0, 1.0) # Red # Update the Material Instance parameters # This is where the magic happens! We're changing the "recipe" # for how the texture looks without changing the texture itself. Material.SetColorParameter("HealthColor", target_color) Material.SetScalarParameter("HealthOpacity", health_percent) # Optional: Hide the bar if health is 0 if (current_health <= 0.0) Material.SetScalarParameter("HealthOpacity", 0.0)