Hi, this is my first time using a professional game engine and I’d like some tips. I have C++ code that generates a randomized labyrinth, game items within the labyrinth and player positions. I can output it in any format that I want, but let’s imagine I decide to output a 2D array of unsigned integers where each number represents a type of room (that has been created by an artist). If I create my game in UE4 and I want to load a level based on this algorithm that I have done in C++, what is my best possible approach? Is there a way to somehow mix the visual scripting capabilities with the occasional C++ programming? I am a strong C++ programmer but some things are just done much faster with the visual scripting tools.
Hey!
I don’t know much about procedural level generation (even though I tried it once, there are some videos / threads about it, you’ll need some (more or less heavy) customization for your needs though), but you can mix c++ and Blueprints yes! You can expose custom c++ methods/classes to blueprints and use them as you wish in the code and/or in blueprints (Other than that, you can just do some things in blueprints, some in c++, just as you need to. I tend to do graphic/UI-related stuff in the UI and Gameplay related things in C++, but that’s just me)
as said, C++ and blueprints interact really smoothly, you just have to decorate your C++ classes/structs/variables with appropriate macros and that will make them visible in the editor.
As for the procedural part, you can easily spawn your imported or prepared assets from C++ code (or even blueprints for that matter). Take a look at these:
So you prepare your assets so that they are conveniently named, then go through your map, for each unsigned integer get the corresponding class by name and spawn it’s actor at desired location.
Thanks! That’s exactly what I wanted: to be able to use the blueprints to do a lot of game mechanics easily and fast without C++ and then create my own blueprint nodes with C++ code. This would allow me to create a node that, given a seed, generates a 2D map of unsigned ints. Then I could go into blueprints and iterate through this map’s cells and spawn a static mesh per cell depending on the cell’s value and all that. Is this something easy to do with blueprints or should I just program the whole level generation with C++ and wrap it as a black box node that just spawns the meshes at level loading?
I am working on a very similar project where I generate a maze in C++, stored as a 2D array and used that to spawn wall-blueprints to get the maze in the world. I can give a few suggestions based on what you want, the decisive factor being when do you want the maze to be generated? You could for example want the maze to be generated once while you’re editing so you can edit the generated maze afterwards, but I’m guessing since you’re procedurally generating you want a different maze each time the level is started?
Until now I’ve tried two things: I first created a C++ class that extends AActor, and in its constructor I generated the maze and spawned blueprint instances containing wall-geometry based on the generated maze. I then created a blueprint in the editor with the created class as parent. When you use this approach, the maze will be spawned in your editor as soon as you drag the blueprint in the level and you can move the pieces around afterwards. You have to be aware that the constructor is called multiple times (!), when dragging the blueprint into the level a preview actor is created also using your C++ class, and when you release your mouse the constructor is called again to create the real actor. You have to make sure that you only generated the maze once, I don’t recall the exact function but it was something with RF_Transient.
To generate the maze each time you start the game, you can override Actor’s PostInitialize (or something like that) and move your generation code to there. Sorry I can’t be more specific atm, I’m on the road. Finally I saw something useful in a blueprint tut, check out Blueprint Quickshot - Making procedural content. They show there how to make procedural content regenerate cleanly, every time after a property of the actor changes. I haven’t found yet how to achieve the same result in C++, but it should be doable and would allow you to rapidly test your generation algorithm by exposing parameters.
Whoa thank you a lot for your post, that is exactly what I want to do: generate a maze every time you initialise a level (by pressing New Game from the main menu state for example). And you’ve described how you mixed both blueprints and C++ so it’s great help. I will try all of this out when I can spare time from my research! (Unfortunately when you aren’t already rich you need to work on your own game ideas in the little free time you have :P)
Sorry for reviving this, but I finally managed to play with the engine and I’ve been having some trouble. I have created UCLASSes that inherit from AActor. One of these classes is an AMazeLevel that contains a TArray<AMazeNode*>, AMazeNode also inherits from AActor. I’ve managed to implement UFUNCTIONs already, however, if I make my UFUNCTION return a TArray of AMazeNode*, how can I access each of these nodes in the Blueprint interface and add a for each loop to spawn geometry for each AMazeNode? The classes I’ve created don’t seem to be recognized inside BP.