Working buttons

In this post we already bound functions to the pressing of either the "Deploy mine" or"Repair" buttons. Now it's time to make them really work!

First, the easier one: Repair button. In this case it's just a matter of completing the Repair() function; I want all these actions to happen only when we are in the player's turn, which means being in gameState == 0. If we're not in it, simply return.
We also have a boolean variable called repairAvailable, that updates itself based on how many resources we collected; if it's false, let's return as well.
In case none of the above happens, we can actually repair our ship: this means adding 1 life to the player ship, and removing 2 resources from our collection. Please note that calling the AddLives() and RemoveResources() methods, will automatically take care of updating the UI and the repairAvailable variable. (In this case the UI will be redrawn twice, but it's not a problem).

Here's the final code of the Repair() function:



Okay, one is done, let's deal with the "Deploy Mine" button. This is the mesh I created for the mine, using Blender:



I will import the mine body and the small lights as 2 separate objects, so that I can apply them 2 different materials. So after importing the FBX file, I have 2 static meshes in Unreal:



The mine1 material, used for the body, will be a simple material with a texture applied, as I did for the ships and resources, while the mine2 material is an emissive material. I will control it's emissiveness multiplyer with a sine function, so that it will look like a blinking light:



The time box will simulate the passing of time for the sine function: leaving the Period parameter to its default value (1) will create a sine waveform of frequency 1 second. If you want it to blink faster you can just input a smaller value.

So the material emissiveness will now go from 0 to 50 and back in 1 second, giving the illusion of a blinking red light.

Let's apply the materials to the static meshes, and combine them in a single BluePrint actor. As usual, I will create a C++ class called "mine", and derive a Blueprint class called "mineBP" from it.

Now, in its editor I can click "Add new > Static Mesh" and add both the mine body and the lights.



As I did for the ships and resources, I'm going to retrieve a reference of the class for the mine blueprint, so I can spawn at due time. (Please refer to the previous posts for any detail):



Now, let's complete the DeployMine() function, spawning it at the same location of the player ship:



Let's stop for a moment and think about the mine behaviour: it will surely have to explode when an enemy hits it, but it should also explode when the player ship moves on it... except when it just deployed it! So, let's add a boolean variable called justDeployed, that we're going to check before deciding of the collision has to trigger the mine's explosion. It will be set to true when we deploy the mine, and will be reverted to false as soon as we move (at the end of gameState == 1).

We need to make a little change to the Hit() function of the enemy ships:



If the other object is a mine, we should destroy the enemy ship AND the mine itself.

Now, I need to create the same Hit() function for the player ship as well, and check if we're hitting a mine. This, in case we haven't just deployed it,  will trigger an explosion, the camera shake effect, and reduce 1 life for our ship.




All these actions will be performed inside the PlayerShipClassBP editor, responding to a BeginOverlap event.



Now we have a working repair system and we can deploy mines, the game starts to become interesting!



Game you next time with new cool features!

Commenti