Hi all I’m working on 2d platformer game unfortunately it seams like c++ is not a c++ anymore in unreal and it got me a bit confused. I’m trying to create a tile map manager with multiple tile layers and normally I would do it this way
struct Layer
{
// "ATileSprite" is a an object that inherits from APaperSpriteActor
class ATileSprite** tiles;
} tileLayer;
tileLayer = new Layer[nrOfLayers];
// for each layer create a tile map
for (int layer = 0; layer < nrOfLayers; layer++)
{
// Create Dynamic 2d array of Tiles
tileLayer[layer].tiles = new ATileSprite*[nrOfTilesY];
for (int y = 0; y < nrOfTilesY; y++)
{
tileLayer[layer].tiles[y] = new ATileSprite[nrOfTilesX];
}
}
but this doesn’t work in unreal, can anyone provide a link to proper c++ documentation, what can and what can’t be done in unreal, especially on pointers, pointers to pointers, memory management, object instantiation, deletig objects, are there any proper c++ tutorials ? I have 3 years c++ experience and I thought it will be straight forward to make stuff in unreal but it seams like I’ll be better of making games in plain c++ and directX API
My answer the C++ is not C++ line, to me is that your code snippet looks like C and not C++. This is my opinion and doesn’t really matter though.
Use a TArray instead of the raw pointer to pointer struct.
You don’t need to call new to store a class, just store it, then use it to spawn your Actor. I’m not sure if you want an array of classes, or an array of Actor References. I normally don’t specify Array’s of classes from C++, because that’s Data, and either fill them from a DataTable, or let them be set in the Editor Details panel of the class that has the Array of them. If it is Actor References that you want, then spawn them, from the class.
C uses malloc(size), free(void*), and realloc(void*,newSize) so I don’t know where you see a “C” code there, doesn’t matter, I’m using new because as you can see I have a pointer to a pointer in layer structure witch means first I have to allocate a block of memory big enough to store "nrOfTilesY " of pointers and than for each one of them I allocate a block of memory big enough to store “nrOfTilesX” elements so at the end I can just call tileLayer[layer].tiles[y][x] to get appropriate class, I see your point with TArray but this is 1d array witch means I will have to calculate 1d index whenever I will want to get to appropriate element, anyway I will give it a go, I can see I can normally allocate “standard” c++ objects and classes but when it comes to stuff provided by unreal it is not that easy as it seams they are heavily “memory” managed thanks for reply anyway !
Hello Mlody and welcome to the community of Unreal Engine,
Unreal Engine does not modify your C++ compiler and as such all specifications of the ISO C++ Standard apply. However, before your code is compiled by your compiler, it is scanned by the Unreal Header Tool. The purpose of this tool is to parse meta data about your classes but sometimes when it finds some code it finds “smelly” and complains; I will provide an example down below as it is not important for your answer.
I assume this line does not work:
tileLayer[layer].tiles = new ATileSprite*[nrOfTilesY];
It does not work because all classes that inherit from UObject must have their instances constructed by some factory methods as they are managed by the garbage collector that the engine provides; the constructor in UObject is private and only the factory methods, which are declared as friends, can access them. Here you can find a way of constructing UObjects that do not inherit from AActor: UObject Instance Creation | Unreal Engine Documentation.
Furthermore, the type ATileSprite inherits from AActor so different construction rules apply: Actors are spawned in a level. As such, there are different factory functions to use which can be found here: Spawning Actors | Unreal Engine Documentation. These functions are called on instances of the type UWorld; this is obtained by calling GetWorld() on an actor; more about this below.
Donw below you find an example for a design that would work in Unreal Engine though first you should follow the links below. You must understand the reflection system (specifiers as UFUNCTION and UPROPERTY) if you want to be developing in C++ with Unreal engine (First link). It is beneficial to know about construction scripts for my design suggestion (second link).
I recommend you create a class ALayer which inherits from AActor. It will have a property of TArray which stores all sprites. Note you cannot make something like TArray> because the Unreal Header tool will complain but you can create a ustruct, such as UWrapperStruct, which contains an array; then you can have a property like this in ALayer: UPROPERTY(EditAnywhere, Category = Layer) TArray.
In the level designer, you can simply drag an ALayer into the level and it will spawn the actors for you. The question is when the tiles are spawned. In Unreal blueprints there is something called construction scripts which are like constructors only that they are called every time your object is changed in the level editor. I recommend overriding the method OnConstruction () in your ALayer class and spawning your tiles there.
You said your code does not work but could you provide more details as to what does not work, please?
Just a side note. The construction script for C++ is OnConstruct so you do not need to call into BP and back into C++ just for the construction script.
The code compiles but when I hit play the editor is crashing,
this line of code still works tileLayer[layer].tiles = new ATileSprite*[nrOfTilesY];
as it only allocates memory to store pointers to ATileSprite objects, however the code goes on and in the loop the actual objects are created and this is the line that crashes the editor tileLayer[layer].tiles[y] = new ATileSprite[nrOfTilesX];
as it allocates memory block of size (ATileSprite*nrOfTilesX) and calls constructors for all created objects. I won’t use blueprints with tile map class, I’m planing to parse the tile maps from the file as I already have this platformer game done in c++ and directX and I have all maps created in *.txt file. I’ve realized what I’m doing wrong when I found this topic
[link text][1]
[1]: How do you instantiate a new UObject? - C++ - Epic Developer Community Forums
My plan is to develop a simple LevelManager that will inherit from AActor class so it can be placed in world it will miantain a TileMap instance witch will load a text file and create tiles based on values found in the file, the level manager will only test if the level is completed and if it will call “LoadTileLevel” method defined in tile map and this is the method witch will create a tile map and spawn tiles.
Thanks for the links going to read them before I dive in to code and end up with pile of errors.