# Taking a plain C++ Program into Unreal

**URL:** <https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [June 17, 2014, 4:46am UTC](https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187 "2014-06-17T04:46:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_8000980f1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8000980f1](https://forums.unrealengine.com/u/anonymous_user_8000980f1)\
**Post date:** [June 17, 2014, 4:46am UTC](https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187/1 "2014-06-17T04:46:32Z")

</div>

Alright its about time to get started!

In any case, I’ve got a fun little program capable of generating a tiled cave system. You feed it a width and a height(current maxes at 100), then you can feed it the max room size(3 is the min, 9 works great), then the max rooms you want(there’s a counter for failed rooms that keeps it from an infinite loop, so it might be slightly less than the max), and then its spits you out a nice map such as this:

(x:80, y:80, maxroom:40, maxroomsize:9)  
[http://i.imgur.com/RDvSa1al.png](http://i.imgur.com/RDvSa1a.png)

Which the space can then be translated into useful 2d array of tile code(tiles numbered 1-9 starting at upper left of a room, ending at lower right, then 10 and 11 are corridor codes), which can then be read out into text then to excel for easier viewing, or just printed right into the terminal(terribly, because 10 and 11 dont play nice)

So the big question: can I use this plain old average C++ file in UE4, or will I have to recreate it using other functions of unreals design. I’m using the includes:

#include \<iostream\>  
#include \<string\>  
#include \<cstdlib\>  
#include \<ctime\>  
#include \<math.h\>  
#include \<fstream\>//notreallynecessarybutniceanyways

and within codeblocks I get this flag on compile:  
non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

In reality the bulk of it is heavily nested if statements and checking consistencies in a 2D array, but there’s a LOT of potential off-by-1 errors I’d like to skip debugging again. Once I’ve got it in, I’ll use blueprints to build the level with 3D boxes(the 11 tile models already created in blender to fit perfectly together), then populate it, and start the fun work of coding AI and inventories and other things.

---

<div class="post-metadata">

**Author:** ![Andargor](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Andargor](https://forums.unrealengine.com/u/Andargor)\
**Post date:** [June 18, 2014, 11:51pm UTC](https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187/2 "2014-06-18T23:51:01Z")

</div>

UE4 uses its own types, so you will need to do some marshalling. Ideally, compile the generation logic (not the graphical part) as a static library, and use UE4 to draw.

Also, [this thread](https://forums.unrealengine.com/showthread.php?47-SaxonRahs-Tutorial-Thread-Random-Maze-Generation-amp-Solving) might interest you.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_8000980f1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8000980f1](https://forums.unrealengine.com/u/anonymous_user_8000980f1)\
**Post date:** [June 20, 2014, 2:13am UTC](https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187/3 "2014-06-20T02:13:48Z")

</div>

I never found that thread, and its pretty close to what I’ve got going so far. The way mine works is by creating the rooms then connecting them all, ensuring that each room has at least 2 exits…normally, I’m not sure what happened to map\_17, but the entry/exit(DARK GREEN) tile should never be in a situation like that due to formula that it uses. The variables can all be changed when I feel like it, but its currently set to 100x100 with 60 rooms, it creates a fairly interesting layout I think, and I’m not sure how many static meshes it would handle at once, the current variable settings produces around 4000 ±200 tiles(roughly 70km of walking area)

[http://i.imgur.com/XOf5Dn0l.png](http://imgur.com/XOf5Dn0)

Tiles will be 4m^2 on the inside, at max 24 faces with only the inside having texturing.  
[http://i.imgur.com/bHKgc6Jl.png](http://imgur.com/bHKgc6J)

This is the class file that contains all the code for the generation and exporting, would it be possible to just to put a copy of this file into the game folder and call the file use the class(I’m fuzzy on the including of files in other projects, didn’t do a lot of that in classes).  
[http://i.imgur.com/uG0tc5Rl.png](http://imgur.com/uG0tc5R)

All I want to do is create the data array using:  
worldMap var(maxX,maxY);  
var.floorbuild(room,size);  
var.cavebuild();  
var.objbuild(density);

And then call the data from the position in the array using the .getObj(x,y), and run a loop from (0,0) to (100,100) that will spawn the tiles(and enemies if the tile is of the right type)

END GOAL: spawn on island \> player has ability to chose size, dificulty, etc. \> player drops into dungeon \> load screen \> blueprint runs the generation code then begins constructing level \> blueprint then creates tiles, placing enemies and items on them \> player spawns(drops) on entrance tile \> player runs the gauntlet to the exit tile \> assuming he lives, the exit tile will roll for a new dungeon level. It will have the levels saved, but only the one you are on is active.

One thing I saw that saxon said in his level generation, was that he saved his data into a text file(I’ve already got that in place) and then just read the text file, and this boosted performance, would reading form the text file be the same as in regular C++ or is there another method to it in unreal(and to be honest reading from a .txt is not something I have tried yet, I’ve only worked on importing the data into excel)

and here is what the enemy/item spawn would look like, the one on the left is 318 spawns(20%chance), and the right is 172(10% chance). I dont know if unreal would handle that many actors at once…  
[http://i.imgur.com/s2q9kkEl.png](http://imgur.com/s2q9kkE)

---

<div class="post-metadata">

**Author:** ![Andargor](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Andargor](https://forums.unrealengine.com/u/Andargor)\
**Post date:** [June 20, 2014, 1:29pm UTC](https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187/4 "2014-06-20T13:29:49Z")

</div>

> [@Thunter124;79982](#):
>
> This is the class file that contains all the code for the generation and exporting, would it be possible to just to put a copy of this file into the game folder and call the file use the class(I’m fuzzy on the including of files in other projects, didn’t do a lot of that in classes).

Here is a [wiki article](https://wiki.unrealengine.com/Linking_Static_Libraries_Using_The_Build_System) on linking static libraries with your project. It is not extremely clear, but should get you started.

> [@Thunter124;79982](#):
>
> One thing I saw that saxon said in his level generation, was that he saved his data into a text file(I’ve already got that in place) and then just read the text file, and this boosted performance, would reading form the text file be the same as in regular C++ or is there another method to it in unreal(and to be honest reading from a .txt is not something I have tried yet, I’ve only worked on importing the data into excel)

There is another wiki article on [storing and using game data from Excel](https://wiki.unrealengine.com/Using_excel_to_store_gameplay_data_-_DataTables) using DataTables, if it helps.

---

<div class="post-metadata">

**Author:** ![SaxonRah](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaxonRah](https://forums.unrealengine.com/u/SaxonRah)\
**Post date:** [June 20, 2014, 3:36pm UTC](https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187/5 "2014-06-20T15:36:42Z")

</div>

Hi, I’m the guy from the other thread and I wanna say you are doing really great work!

I used to use just straight up .TXT files back when i first made the generator sometime ago. That approach sucks imo because you need to write your own importer for unreal engine 4. While I did that too and it works great, but unreal handles CSV naively, no reason to reinvent the wheel like i did, you can find that importer work [here](https://forums.unrealengine.com/showthread.php?7243-Tutorial-CSV-Files) if you are interested. But really you should look [here](https://docs.unrealengine.com/latest/INT/Gameplay/DataDriven/index.html) and the above link.

I would really look into forcing your map generator to generate a CSV file instead that being said, It could be simple as adding a delimiter parameter and changing the file extension to CSV when writing. But i have no idea how you do it there are many different ways to program a cat. If you need help I can try.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_8000980f1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8000980f1](https://forums.unrealengine.com/u/anonymous_user_8000980f1)\
**Post date:** [June 21, 2014, 4:12pm UTC](https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187/6 "2014-06-21T16:12:16Z")

</div>

That was… startlingly simple… I changed a space to a coma, and .txt to .csv, but for purposes of reading/writing to a csv file I would like to know weather its possible to go by column number and row number, or if I need to actually add in column and row names, because that in fact becomes quite more difficult.(I may have overthought that, and might have just made it simple)

(edit: it was indeed again, extraordinarily easy, and I was trying to over-complicate it again, unfortunately column 0 and row 0 were casualties, and now I only have 99x99. But they were a buffer anyways, filled with 0s for intial map generation to stop rooms from running out of bounds)

All I would truly want to do is say: mapfile.getValueAt(x,y), which would produce an integer, and then the blueprint generates the mesh/colision/anythingcontainedwithin based on that integer.

---

<div class="post-metadata">

**Author:** ![SaxonRah](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaxonRah](https://forums.unrealengine.com/u/SaxonRah)\
**Post date:** [June 21, 2014, 6:26pm UTC](https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187/7 "2014-06-21T18:26:01Z")

</div>

You can thank

> [@Thunter124;81095](#):
>
> That was… startlingly simple… I changed a space to a coma, and .txt to .csv, but for purposes of reading/writing to a csv file I would like to know weather its possible to go by column number and row number, or if I need to actually add in column and row names, because that in fact becomes quite more difficult.(I may have overthought that, and might have just made it simple)
> 
> (edit: it was indeed again, extraordinarily easy, and I was trying to over-complicate it again, unfortunately column 0 and row 0 were casualties, and now I only have 99x99. But they were a buffer anyways, filled with 0s for intial map generation to stop rooms from running out of bounds)
> 
> All I would truly want to do is say: mapfile.getValueAt(x,y), which would produce an integer, and then the blueprint generates the mesh/colision/anythingcontainedwithin based on that integer.

> [@;27139](#):
>
> **Code for You !**
> 
> and others!
> 
> I made my own USTRUCT system for having a multi-dimensional UPROPERTY() friendly dynamic array version of your maze data structure.
> 
> Here’s that code for you and others to enjoy!
> 
> .h
> 
> ```auto
> 
> USTRUCT()
> struct FMazeGridRow
> {
> GENERATED_USTRUCT_BODY()
> 
> UPROPERTY()
> TArray<AStaticMeshActor*> Columns;
>     
> void AddNewColumn()
> {
> Columns.Add(NULL);
> }
> //default properties
> FMazeGridRow()
> {
>         
> }
> };
> 
> ```
> 
> ```auto
> 
> USTRUCT()
> struct FMazeGrid
> {
> GENERATED_USTRUCT_BODY()
> 
> UPROPERTY()
> TArray<FMazeGridRow> Rows;
>     
> void AddNewRow()
> {
> Rows.Add(FMazeGridRow());
> }
>     
> void AddUninitialized(const int32 RowCount, const int32 ColCount)
> {
> //Add Rows
> for(int32 v = 0; v < RowCount; v++)
> {
> AddNewRow();
> }
>         
> //Add Columns
> for(int32 v = 0; v < RowCount; v++)
> {
> for(int32 v = 0; v < ColCount; v++)
> {
> Rows[v].AddNewColumn();
> }
> }
> }
>     
> void Clear()
> {
> if(Rows.Num() <= 0) return;
> // ~~~~~~~~~~~~~~~
>         
> //Destroy any walls
> const int32 RowTotal = Rows.Num();
> const int32 ColTotal = Rows[0].Columns.Num();
>         
> for(int32 v = 0; v < RowTotal; v++)
> {
> for(int32 b = 0; b < ColTotal; b++)
> {
> if(Rows[v].Columns**&& Rows[v].Columns**->IsValidLowLevel() )
> {
> Rows[v].Columns**->Destroy();
> }
> }
> }
>         
> //Empty
> for(int32 v = 0; v < Rows.Num(); v++)
> {
> Rows[v].Columns.Empty();
> }
> Rows.Empty();
> }
> //default properties
> FMazeGrid()
> {
>         
> }
> };
> 
> ```
> 
> ````auto
> 
> **Maze Grid Var Declaration for Persistence and Global Access**
> 
> ```
> 
> //Now you have dynamic array benefits and also UPROPERTY()!
> UPROPERTY()
> FMazeGrid JoyMazeGrid;
> 
> ```
> 
>     
> .Cpp
> 
> ```
> 
> //Init Maze
> JoyMazeGrid.Clear();
> JoyMazeGrid.AddUninitialized(tileX, tileY);
>     
> //Sample usage
> //JoyMazeGrid.Rows[x].Columns[y] = SpawnBP<AStaticMeshActor>(GetWorld(), ...
> 
> ```
> 
> ````
> 
> **Summary**
> 
> The final result of all of this is that you have
> 
> 1. dynamic array benefits (easily change the maze size and regenerate during runtime)
> 
> 2. global access to the MazeGrid Actors, even from other classes
> 
> 3. GC protection from the UPROPERTY() (if not using actors/objects for some reason)
> 
> 4. And yet the syntax for 2D array using UPROPERTY() and TArray is still clean and clear and precise
> 
> **JoyMazeGrid.Rows[x].Columns[y]**
> 
> Enjoy!
> 
> PS:
> 
> I also wrote a function to clear the maze / destroy any actors.
> 
> You have to pre-initialize the array to use the intuitive 2D array accessor method.

- Quoted from @ my thread.

This is how i get rows and columns. It’s quite the code!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_8000980f1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8000980f1](https://forums.unrealengine.com/u/anonymous_user_8000980f1)\
**Post date:** [June 21, 2014, 8:31pm UTC](https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187/8 "2014-06-21T20:31:57Z")

</div>

I encountered a problem when trying to import the table through the UE editor(not sure what exactly it didn’t like about it, 0,0 was blank and then I had the rows as 1-99, and columns as 1-99. So for right now I’m working on creating a static library so I can call the functions and just create the array in unreal and then access it. And theres another thing, how do I take my C++ class and use and abuse it with the blueprint functions?

Oof, the library is completed, but I’m still having quite a bit of trouble connecting it to the project, going to attempt just adding in the code itself(its only 600 lines) in the project, and then have it create an array of map objects to pull data from. At some point I found a place to just add in code to the project, but I’ve forgotten where, shouldn’t be too hard to find again however.

---

<div class="post-metadata">

**Author:** ![SaxonRah](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaxonRah](https://forums.unrealengine.com/u/SaxonRah)\
**Post date:** [June 22, 2014, 4:32am UTC](https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187/9 "2014-06-22T04:32:50Z")

</div>

Yeah I would add your generation to ue4 code instead of importing the csv file in the editor although it can be done. I’d extend actor personally, i like to have ‘brain’ bp actors in the world so i can edit it’s values at run-time and get dungeons where ever whenever.

i don’t use any names on any rows or columns i even use ‘a1’ i just plot all of my data into the file with no padding. and i use single and double letters for tiles so i can manage 676 different tiles in a simple 2 letter data even more like 2704 different tiles if i use upper and lower case. (idk maybe using numbers would be a smaller footprint idk)

Are you looking for the add code to project from file menu in ue editor ?

Hey i found some really great stuff for you to check out. It should help you allot.  
[A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums](https://wiki.unrealengine.com/Save_System),_Read_%26\_Write\_Any\_Data\_to\_Compressed\_Binary\_Files#Custom\_Save\_System\_To\_Binary\_Files

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_8000980f1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8000980f1](https://forums.unrealengine.com/u/anonymous_user_8000980f1)\
**Post date:** [June 22, 2014, 1:29pm UTC](https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187/10 "2014-06-22T13:29:20Z")

</div>

I’ve always had a penchant for over complicating things, what I should be doing is finding where the c++ fps adds in the actor/default terrain, nuke the terrain, drop in a delay for his spawn, gen the world, spawn the current floor, and then drop the actor in on an island where he can drop in a hole that will portal him to the entrance. I’ll still need to look into saving to the csv, assuming it is more efficient than having a vector of arrays of 10k integers floating around.

The save system should come in handy when I start working on the full thing. Currently I’m attempting to get the dungeon sprint working(single floor, survive to the end), then I’ll work on the “choose your own adventure”(player decides ALL the variables,still governed by sprint rules), and once I’m done with that I’ll start creating the full experience.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_8000980f1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8000980f1](https://forums.unrealengine.com/u/anonymous_user_8000980f1)\
**Post date:** [August 17, 2014, 5:04pm UTC](https://forums.unrealengine.com/t/taking-a-plain-c-program-into-unreal/6187/11 "2014-08-17T17:04:58Z")

</div>

Alright, I’ve taken the summer off to work on manufacturing techniques,built a Cnc, a 3d printer, and a ton if 3d modeling. I’m the intersection of engineering, computer science and art(specializing in ceramics, further specialization instrument crafting), kinda run out of time in the day to run all the experiments, code programs, and make all the things.

I’ve finaly built and tuned and run the machines for a couple hundred hours, and every major project in that realm finished. I’m about to get back inhibit CS studies, so I’m making the transition back into this project. SO, I could use a quick refresher on a few things:

1. I’m needing help in creating a new class within a project, one in which I can rewrite my worldgen code, that will create a 3D array
2. How to make this code where I’m capable of using it in a blueprint, passing it parameters then having the code generate the array, and then being able to pull data from an (x,y,z) from the data table(I’ll worry about saving data at a later point, will probably implement seeds instead).
