Post

Visualizzazione dei post con l'etichetta code

Working buttons

Immagine
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...

Cloaking device

Immagine
The next problem I want to address is hiding the enemy ships when they're on undiscovered tiles, and obviously showing them as soon as they enter a discovered one. This can be easily done with the Actor's SetActorHiddenInGame() method, but I want to try and make the effect a little nicer, by playing an animation from totally transparent to totally opaque while the ship is moving. But first things first, let's use visibility to make it visible as soon as it starts to move from an undiscovered tile to a discovered one: Since all enemies start from outside the grid, their initial visibility will be set to "hidden" by default: Remember that the method sets the Actor to Hidden, so passing a true parameter means we want it to be invisible, and passing false will show it. Then I created a new int variable in enemyClass.h, and I called it changeTransparency; it can take 3 values: 0: the ship doesn't need to change its transparency. ...

User interface - Adding functionalities

Immagine
In the previous post we created the layout for our user interface. Before going on with the work on it, I want to add some useful stuff to C++ code. I decided to store all informations about the player status (health points, resources, etc.) in the PlayerController class, becuse it seems the most logic place for them. So I'm adding a bunch of new variables: lives:  the number of actual health points left; maxLives:  the maximum number of available health points (it's 3 for now); resources:  the number of collected resources; maxResources:  the maximum number of collectible resources (let's say it's our cargo space, and I'm setting it to 5 for now). All these variables are created with the UPROPERTY macro, so that they're visible from the Blueprints, and can be used to modify the User Interface widget. I'm also going to create some new UFUNCTION methods that will handle the events coming from the User Interface buttons: selectOpti...

Collisions & Spawning a Blueprint class from C++ code

Immagine
In this post we learned about class inheritance. Another case in which this technique is used by Unreal Engine, is when we create a Blueprint class based on a C++ class. We did this for the map at the beginning, and now I want to do the same with both player and enemies starships. There are a couple of reasons why I came to this decision: 1) I want to add new static meshes to the base one, i.e. a simplified shadow under the hull, particle systems for thrusters, etc. and it would be a pain to create them all from code at right positions. 2) I want to use Blueprint Overlap Event to manage collisions between ships, so I don't have to check them from code. The problem here is that we can't simply grab the Blueprint class and drag it into the level, we need to programmatically spawn Actors from code; we don't have a reference to the BP class though, so how can we do it? Well, first of all let's create Blueprint classes derived from our PlayerShipClass and EnemyShi...

Class Inheritance

Immagine
Space exploration can be very boring when you're alone, I think it's time to add some action by spawning enemies around the map. While thinking about the functionalities needed by the enemy ships, I noted that many of them are the same of the player ship. For instance, both need the arrays for directions, the same functions to move and rotate, etc. This is the typical case where class inheritance comes in handy. Class inheritance is one of the core concepts of many OOP (Object Oriented Programming) languages. It basically lets you create a base class where all the common behaviours and properties are stored; you can extend that class to other more specialized classes, where you can write methods and store variables that affect that particular subclass only. We already used this concept in the project, but Unreal Engine managed it for us behind the scenes: all our classes extend the Actor class, so all the basic methods like drawing the mesh, etc. are already writte...