Monday, December 22, 2014

Initialization and Post Initialization

Since our last tutorial I have added a few more 'UPROPERTY'.  The these will help with creating the functionality we are looking for.  In this tutorial lets look at initializing the grenade as well as adding a post initialization component.



When we initialize any class in the unreal engine we want to setup the 'CollisionComp', 'MovementComp' and other useful information like does this actor tick or not?



CollisionComp defines how the class will interact with the game world.  The most important functions of the collision component are 'InitSphereRadius' and 'SetCollisionResponseToChannel'.

The 'InitSphereRadius' creates a capsule around the class which handles all the collision.  Without this there would be no collision for the class.  Creating a collision capsule also improves performance since it simplifies collision detection.  

The 'SetCollisionResponseToChannel' allows you to define how this class will interact with other objects.  For example you could set 
CollisionComp->SetCollisionResponseToChannel(ECC_WorldStatic, ECR_Ignore);
This will allow the class to travel through walls and other static meshes.  As you can image you can get create with all the different collision response channels.

MovementComp defines all the aspects of the classes movement.  There are many cool functions that allow you to set things such as: InitialSpeed, MaxSpeed, ProjectileGravityScale, etc.


Lastly 'Tick' is a function that will run 'x' milliseconds.  Tick offers a lot of cool potential.  For example if you wanted to add a 'Damage Over Time' effect for player characters you could add something like the following:
if (IsPoisoned == true)
{
Health -= 10.0f;
}

In the above example, for every second 'IsPoisoned ' is true, the player will take 10 damage to his/her health.

'PostInitializeComponents' is a function that runs once the class is spawned in game.  In our example of the grenade, that would be once the player presses the key to start to throw a grenade.  Then we are going to do three things in our 'PostInitializeComponents'.



First we are going to set the life span of the grenade.  This is going to be when the grenade explodes and is then subsequently removed from the game.  We set the life span to 4 seconds, which is standard for a grenade's life.

Next we are going to tell the grenade who created him.  This will be useful for obtaining statistical information, like whose grenade killed the enemy.  We could also do interesting things like player perks that increase the damage of grenades.

No comments :

Post a Comment