My website code stuff
I managed to get a functional version of the life system working in-game. You can hold left click on a cube with the life component added to give it life or hold right click to take its life. Once it reaches 0 health, it is dead completely.
I use the visual scripting system to cast a ray from the center of the screen forward when holding either left or right click. I look at what I hit and assign it and its life component to the C++ component.
Here’s what the graph looks like:
A note on how to read this: Nodes are rounded boxes and the lines which connect them show inputs and outputs. This graph has a bunch of different variable types which are all denoted as differently colored lines: Actors, Booleans, Control Signals, Floats, and Vectors
Here is a list of the boxes (functions):
Branch: Branches to the line trace if either pressingGiveLife or pressingTakeLife is true but not if both are pressed
Range: Gets the variable range from the player’s script - multiplied by the forward vector (which is normalized) to determine how far the raycast should go
Get Component by class: Takes as input the Actor hit and outputs its Life Component. If it doesn’t have one, it instead outputs NULL
Below are the two main functions in the Life Component. The component stores the current life and other various options. When the player holds left or right click, they will call these functions on their target (if that target has this component).
Take life is simple, it takes in amount (how much life to drain per second) and converts that to delta which is how much to take per frame (assuming 60 FPS - this will be changed to be dynamic later). Next it checks if the Actor is alive and if it is, it sucks out life and returns how much was taken. If the Actor is dead, it returns 0.
float ULife::TakeLife(float amount)
{
//Frame-specific value to take
float delta = amount * 1/60;
// If alive
if (life > 0)
{
float newLife = life - delta;
// If alive and damaged
if (newLife >= 0)
{
life = newLife;
return delta;
}
// If alive and brought below 0 health
else if (newLife < 0 && life > 0)
{
float temp = life;
life = 0;
//return exactly as much life as there was left
return temp;
}
}
//If dead
return 0;
}
This function takes in amount and does the same delta conversion. It’s a bit more complicated because it can affect the undead. If undead and the life will bring the Actor to 0 (dead), the Actor will “die.” If the target is alive, they will simply store the extra life.
float ULife::GiveLife(float amount)
{
float delta = amount * 1 / 60;
// Not dead
if (life != 0)
{
float newLife = life + delta;
// If undead
if (life < 0)
{
// If healed
if (newLife < 0)
{
life = newLife;
return delta;
}
// If brought above 0 life
else if (newLife >= 0)
{
float temp = life;
life = 0;
return -temp;
}
}
// If alive
else
{
life = newLife;
return delta;
}
}
return 0;
}