Post

Visualizzazione dei post da maggio, 2017

Start your engines!

Immagine
Let's move our player ship around the map! I imported the mesh I created in Blender into Unreal; then, as usual, I created a  PlayerShipClass  based on AActor, to manage the behaviour of my ship.  Instead of dragging it in the editor to create my Static Mesh Actor, I'm going to spawn it from code, inside the BeginPlay() method of my PlayerController class: Here I can specify the world position of my mesh, and this will become necessary when we have multiple levels, each with its own map dimensions. Then I set the gameState to 0, meaning that the game will start inside the Player turn, waiting for his input. Let's take a look at the PlayerShipClass constructor, where I'm specifying what mesh to use: As we did with Hexagon tiles, I'm telling the constructor where to find the mesh, assigning it to the Actor and setting its location and scale. Let's compile, and take a look at our glorious spaceship, awaiting for orders at the center of the m...

Tracking the mouse position

Immagine
For the purpose of this game, the only input device we need to manage is mouse. Basically, we just need a way to know which tile it's hovering on, and then take some action if we click while we're on it (Let's not worry about any buttons for now, we're going to handle user interface later). The steps to follow are: get the position of the mouse in screen space (x and y coordinates on our monitor) transform those coordinates to world position. This is based on where the camera is positioned. create a line perpendicular to our screen, starting from the mouse cursor. See if this line intersects any object. If that object is a hexagon, then that's the tile we're hovering on. Since I need to constantly check for the mouse position, I'm going to implement this in the Tick() method of my class, so that a check will be performed every frame: As you can see, I gave my PlayerController class a variable for the game state: player turn, player move...

Creating a custom camera

Immagine
For the purpose of this game, I don't need a camera following the player pawn, as it would do in a 3rd person game: I'm going to have a fixed camera that will show the whole game map from a static point of view. Let's see how to create it: First, go to Modes > All classes > Camera and drag a new camera into your scene. I positioned it manually, moving and rotating it until it looked at the whole map from a satisfying angle. As you can see, when it's selected, a small box on the lower  right corner of the editor shows the camera's point of view. When I'm ok with the position, I type in the round values for Location and Rotation in the camera properties: Now, what we have to do is tell Unreal that we want the game to start from that particular camera (we could have multiple cameras in our scene), and this can be done inside the Level Blueprint . With the camera selected, open the Level Blueprint. If you right-click inside th...

The Player Controller

Immagine
In Unreal Engine, the Player Controller class is used to manage the player's Pawn (a pawn is simply a specialized Actor that can be "possessed" by the player). Player Controller takes care of handling the input, in our case from the mouse, moving the player ship accordingly, managing the turns, and so on. As always, the first step is to create a new C++ class, derived from PlayerController. I'm going to extend it to a Blueprint and call it MyPlayerControllerBP . It's always a good idea to use Blueprints for classes that will handle input, because binding it to custom methods, or giving the player the possibility to customize it, is much easier than having to do it from code. There's no need to drag the MyPlayerControllerBP icon inside the level to instanciate it, as we did with the Main Map; we just need to set it in the Options. The first thing to do is to create a custom Game Mode. I did it directly in Blueprint because I'm not going to c...

Instancing tiles

Immagine
The first thing we need in our game is a grid of hexagons to be used as tiled map for the ships movements. In my previous post I created a MainMap class, and here I'll put the code for the dynamic creation of our grid. Maybe you'll wonder why I didn't simply create an Actor instance for every tile in the map: the reason is that an Actor class is a bit heavy to handle for the Unreal Engine, so having hundreds of Actors in the scene only to render a static map isn't a good practice; especially when we have a nice and clean way to do it: components. An Actor can have many components, or sub-objects. They can be meshes, but also other types of sub-classes that handle a particular task for the main object (movement, aiming, etc.)  This is the code to create a component from C++: With the CreateDefaultSubobject() method we can create a new empty component ( tile ) for our Actor. In order to make it visible, we have to assign it a StaticMesh. ...

First Class

Immagine
This is not a post about X-Men: First Class, even though I love the franchise... today I'm going to explore the creation of C++ classes directly inside the Unreal Editor. Creating a C++ class in Unreal Engine is really simple: just click on the "Create new C++ class" button, select on what existing class you want it to be derived from (an Actor in my case), and give it a name:  This will create all the needed files and open Visual Studio editor. Here we can see that both the .cpp and .h files have been created for us, along with some useful methods: AMainMap():  This is obviously a constructor for the class. Here we can initialize all the variables, and do all the stuff we need to perform when the instance of the Actor is created. The variable bCanEverTick tells the compler if this object will need to do something every frame (like check its own psition, change animations, etc.). If not needed, it's better to set it to false to sa...

Unreal Engine 4 introduction

Immagine
Unreal Engine is a powerful framework for game developement, suitable for projects of all sizes, styles and complexity. For a simple project like mine Unity could have been the first choice, but since I'm attending an UE4 course I'll try to build it with this engine instead. First of all, every entity in Unreal (Player, Enemies, Ammo, Particle Systems, etc.) is an Actor. It has some basic functions like position, rotation and scale, and if needed it can be extended for more specialized tasks (i.e. Static Mesh Actor can simulate Physics, Pawn can be possessed by the Player, and so on). There are 2 main ways to create scripts for Actors in Unreal: Blueprints and C++. While blueprints are internally structured like C++ classes, they offer a visual representation of the behaviour of that class. A simple Event Graph of a Blueprint Each level of the game has its own Blueprint, taking care of  all the Actors, Cameras and every entity in the scene. Then every A...

Prototype gameplay

Immagine
Hello! I'm back to programming after my short holiday in the 3D modeling world with Blender. Today, I'll show you a gameplay video from my Java prototype: This is a sample level with random spawned enemies of all kind. It shows how you can approach enemies in different ways, trying to avoid them, moving to their same cell to destroy them, or deploying a mine for the most annoying ones. Also, resources management can be done with different strategies: you can repair as soon as you get hit, or decide to continue playing damaged, hoping to reach the end of the level. In advanced levels, you could be asked to complete it after collecting at least N resources, or killing X enemies, and so on. These constraints will constantly change the way you play, and (hopefully) keep you interested in the game. Next: I'll finally move to Unreal Engine and see how hard it is to implement all these ideas in that environment. I expect many challenges, but I'm sure it will be a...

3D Game assets

Immagine
While working on the basic game concept I decided to take a break and start doing some 3D assets for the final version of the game. The style I'm looking for is not a super realistic, highly detailed one: The game will have a static camera to show all the grid, so the ships will be very small on the screen. So I decided for a low-poly, a bit cartoonish style. First of all, the base shape of the game: the hexagon. Doing this is very simple in Blender: create a cylinder and lower the number of sides to 6. Scale down its height, and you have a perfect hexagon! For the player ship model I started with a simple box; I subdivided and extruded it until I got the basic shape of the ship. Then I added laser weapons on the wings and thrusters on the back (basically modified cylinders). To apply textures to the model, it needs to be UV unwrapped. This means that all the polys have to be "flattened" on a 2D sheet so that the software knows where to apply the...