Post

Visualizzazione dei post con l'etichetta class

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

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