Post

Visualizzazione dei post da giugno, 2017

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

Finalizing the map

Immagine
The last step to complete the map behaviour is having undiscovered tiles, that will be discovered when a starship travels across them. Let's create a bunch of functions to handle this. The first thing I did was creating a new grey-colored material for the undiscovered tiles, which is assigned by defualt to the  hex asset. Next, I created an helper array of UPrimitiveComponent* in MainMap.h , to be filled when I spawn the tiles. This way, I always have a quick reference to what object is inside every cell of the map: Then I added the highlighted lines to my MainMap constructor: All the tiles are flagged as "undiscovered" at the beginning, and I store the asset pointers in the tilesArray variable.  After this, I moved to the MyPlayerController class, and added the following lines after the creation of the p1 variable: What I'm doing here, is passing a reference of the map to the player's ship, and then calling a new method ...

Playing with materials

Immagine
In the last weekend I didn't have much time to dedicate to the project, so I decided to take a brake from coding and spent some time enhancing the visuals by playing around with materials. I decided to give the game a temporary background, so I googled for a starfield texture with some nice colours, and I cropped the image to have a 2048x2048 square texture. Then I modeled a square plane in Blender and I imported it in Unreal Engine, together with the star texture. By using the "front view" in the editor, I rotated the plane to be roughly perpendicular to the camera. The next step was creating a new Material, clicking on "Add new" and then choosing "Material". In the material editor I right-clicked and selected "Texture Sample". In its options I chose my star texture, then I connected the output pin of the texture box to the "Emissive color" pin of my material. Clicking on the material box, the left ...

Referencing the map from PlayerController

Immagine
Now that I'm starting to write the game logic inside the Player Controller, I want a way to reference my map from it, so that I can have access to its tiles, and eventually pass it to my Player ship and enemy ships, to let them perform their own logic and know what tile they're on, etc. I found 2 ways to achieve this goal, one using blueprints and the other from code, and I want to show you both: Blueprints procedure: Since Player Controller and MainMap are both Blueprint classes and I created the map by dragging it inside the level editor, I don't have a reference of it inside the code as I have, for example, for the Player ship (do you remember the p1 variable?). There's an entity, though, that knows everything that is created inside the level, and it's the Level Blueprint , that we already met when creating a custom camera. The first part of the procedure is somewhat similar to that one, so it should sound familiar. While we have the MainMap ...