Post

Visualizzazione dei post con l'etichetta input

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

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