Jump to content
SlipperyBrick

Procedural Level Generation (Lua)

Recommended Posts

I am looking at making a PCG script that will generate a level using prefabs of modular components.

 

I am trying to figure out the logic behind this of how the script is going to work but I am running short of ideas. I actually only have one method on how to do this which I read up on a forum.

 

You could create room templates that are prefabs. Add a short script to the prefab's main GameObject that lets you specify where the connection points are. If you want to keep it simple, you can restrict connections to the middle of each edge (top, bottom, left, right) and have a Boolean variable for each. Say you start with a prefab room where left and right are true. For the left connection, you want to find another prefab whose right connection is true, and then instantiate it next to the original room.

 

The thread can be found here http://forum.unity3d.com/threads/structuring-procedural-level-generation.279680/

 

This method was on the Unity forums. I am looking to see if anyone else has some idea's or methods they would want to share on how to do something like this?

 

I am struggling to get started sad.png

 

Cheers.

Share this post


Link to post

there where some good thoughts about that already in this thread here:

http://www.leadwerks.com/werkspace/topic/12063-procedural-generated-levels-using-prefabs/page__hl__procedural

 

and i guess i would not do it much differently.

 

put pivots to the doors where the parts can connect etc. i am just not sure how intersecting parts should be handled.

 

If you go the Binding Of Isaac route, i guess you would not even have to worry about that, since the scenes switch completely and you can unload the other part. (or move the old part out of the way if you don´t want to fiddle with loading/saving if you are able to return to the old part. but it would eat more memory i guess)

Share this post


Link to post

Make a 3D array. Imagine a 2D tiled game. You have x and y "tiles". You are now just adding z layers (vertical layers) to that. Now in a 2D game you have tile sizes. Normally like 32x32 or 64x64 which means 32 or 64 pixels in width or height. You normally want width and height to be the same. Now in 2D land you can make bigger objects by combining multiple tiles that makes up the bigger image. Like you can make a house that is 3x3 in tiles.

 

Now translate this all to 3D. Your prefabs are your tiles. Make them all the same size width width depth and pick a constant height value. Ideally you would make the width/height the smallest you can imagine will be needed, but for performance reasons you'll want to not have it so small. If you need bigger things in your scene then you can make prefabs that are meant to be combined together to make that bigger thing.

 

In the 3D array just store numbers. The numbers will relate to the prefab. 1 = hallway.pfb, 2 = hallway_end.pfb etc. Then you just have some code that fills in this 3D array with numbers. On load you loop over it and do a lookup on the numbers to load the correct prefab. Each "tile" has a position (center point) and so placing your models is easy since the width/height/depth are all known.

 

Let's say for example you pick 2cm height, 10cm depth, 10cm width for all your prefabs (just for example). Let's say 0,0,0 in your 3D "map" array is 0,0,0 in the world origin. map 1, 0, 0 means you are moving right on the x value in LE. So you just multiply 10 by that x value and that gives you the new position for whatever model is in that map.

 

If this seems confusing I suggest looking at how 2D tilemaps are drawn and it'll start to become more clear. You could even just use 2D array to start with and just have everything at 0 height. It might be easier to understand to start with.

Share this post


Link to post

Continuing the conversation from the status updates, you mentioned to you have assets already created. How are they constructed? Are they actual prefabs where walls, floors, and ceilings are already in place and you piece them together like a jigsaw puzzle? Or after you generating them at runtime?

Share this post


Link to post

Hey guys, I will put up some screenshots of the assets and prefabs. I haven't yet started the script, I have been reading through Flexman's blog of one method he uses to do this but it isn't what I am looking for.

 

I will start a blog and get some screenshots up of what I have so far though for you guys too see. I think I am gonna need the help to be honest :P

Share this post


Link to post

Good link NA, I have been following these threads as I considered having the later levels of my game procedurally generated,

Share this post


Link to post

Continuing the conversation from the status updates, you mentioned to you have assets already created. How are they constructed? Are they actual prefabs where walls, floors, and ceilings are already in place and you piece them together like a jigsaw puzzle? Or after you generating them at runtime?

 

Hey Einlander I totally missed your post. To answer your question, yes. All models are pretty much constructed with walls, floors, ceilings already. They all piece together as you say 'like a jigsaw'.

 

I will post screenshots later today so you guys can see the way I have made the models. Pivot points are all centred on the models at 0, 0, 0.

 

I unfortunately had to start all over again with this project, I bought a laptop about a week ago (girlfriend complains about me being glued to my desktop and not spending time with her lol.) I thought I had backed up the whole project to my Dropbox, so I removed the project from my desktop and then realized I didn't back up the project so I lost everything sad.png

 

I started making the assets again they are pretty much the same as what I had, I have about 4 of them so far, I am trying to plan ahead before making more. The map is going to be a train station very similar to the 'London Underground' so corridors and platforms and big spaces for ticket machines and benches etc are going to be done but I am thinking how I can break things up so the procedural generation stuff brings back interesting results for different variations of the map and also works everytime, predictability is my objective with this smile.png

 

I am researching into the 'preparation' of PGC and the thinking behind how the models should be constructed, the way I am currently doing it from what I have seen seems to be the usual way it is done (if that makes any sense lol).

 

Screenshots will defo come later today though, I am probably going to need a lot of help on this so expect some nagging from me guys tongue.png

Share this post


Link to post

Ok here is a screenshot from Leadwerks of the modular components I have so far.

 

Not a lot right now and the UV's need a lot of work so the textures are all seamless sad.png

 

I am currently working on more. Just trying to plan how to build certain parts.

 

This is how the majority of the models are going to be made, so each one can join up nicely to make a seamless level.

 

vQeC7nn.png

 

Right now the pivot points are set at 0, 0, 0. I think this is probably how I am going to keep it.

 

I was thinking of using a boolean type operation on the models so both ends of the model will have a 'true' or 'false' value so only 'true' will match up with 'true' and vice versa, the more I think about this though the more problems I think it may cause.

 

I am probably going to work up some artwork for promotion and get my project a 'Greenlight' concept page on Steam so I can add it to the game database here on Leadwerks. I am thinking of starting a blog here also, everything is still heavily WIP but I am hoping to have the majority if not all of the models done pretty soon smile.png

Share this post


Link to post

In my opinion, you are making things way too hard on yourself. You could use brushes to create any rooms like that, and then just populate them with model props for details (like the light fixtures). It would be faster and you'd have a lot more flexibility with your design.

 

post-1-0-93937400-1431288378_thumb.png

Share this post


Link to post

In my opinion, you are making things way too hard on yourself. You could use brushes to create any rooms like that, and then just populate them with model props for details (like the light fixtures). It would be faster and you'd have a lot more flexibility with your design.

 

post-1-0-93937400-1431288378_thumb.png

 

Defo sounds much more easier, the main reason I am modelling is really for practice but I do want to get things going with this game. Maby this way would be better to get the main elements of the game done visually so I can focus on gameplay. I could get a prototype done much quicker and get some feedback on it, after that I can focus on the visuals.

Share this post


Link to post

I think the idea was to get procedural levels which comes with the benefits of having unlimited level designs since it's put together in code with some randomness, making replayability much higher and more interesting.

Share this post


Link to post

I've been looking into procedural generation to, and I was wondering with this game engine how would I create and size a GSG with lua. I dont know what api I should look for, And if I switch and use rooms that are prefabs, how would I detect where a door way would be?

Share this post


Link to post

I personally wouldn't use CSG's, they have little gotchas that appear in the oddest of places. Also texturing in the editor and in game is completely different.

Share this post


Link to post

Okay, so say if I went with a room prefab, how would I detect where a door way is, would I have to create a pivot for each entrances, so that for each differnt prefab, check and make sure that the pivots are right and move the prefab to that pivot?

Share this post


Link to post

That's probably a good way to do it. Pivots are pretty powerful for methods like that. If they are grid-based rooms, then you might not even have to do that. Usually, grid-based approaches are easiest for procedural generation since they are easy to line up.

 

If you are talking about adding doors after the level is generated, then the method you described would probably work best.

Share this post


Link to post

Just note that you may run into performance issues if you have a ton of entities. LE doesn't like a ton of entities so you may have to lower the view distance to be fairly low to help out the performance pending on how many entities you end up with. I know we ran into this when we were trying to do something similar with our game and making houses in a very modular way.

Share this post


Link to post

Yes, the modular thing in LE didn't work for us with the size of the project we are looking at. You can keep it modular, but I recommend build your structures in your modeling program and export as much as you can as one piece. We did a lot of testing in the initial stages of our project to see what is possible.

Share this post


Link to post

I personally wouldn't use CSG's, they have little gotchas that appear in the oddest of places. Also texturing in the editor and in game is completely different.

What do you mean?

Share this post


Link to post

For me, since you can't instance csg's I would only use csg's to block out a level for the concept phase of game creation. And then move on to regular mesh models that can be instanced or use geometry shaders to create or delete models to help cut back on the entity count.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...