How to make a Grid of Enums in the Inspector

I have a puzzle game, at the moment I’m using different scenes for different levels, but since this is A really hard to debug and B not very efficient, I want to make scriptable objects that I can edit in the inspector.

Is there any way to make an editor script that does this? So, basically my levels are laid out like this,


Don’t mind the random errors lol

I have these gizmos that represent puzzle pieces, white means empty. I was just editing them manually this way, I want to set up a way to have them automatically assign from a grid of enums that I set up in the inspector. So it’ll be like, level 1, this level, the 3 brown spots will be set to “Brown” and the rest will be “Empty”

Of course, the way I’m doing it now, with a different scene as a different setup means, if i want to change something in the way the game works, with the gamemanager or something, I have to go through each scene and manually change it, or change the prefab, but of course, there’s some things which can’t be solved from changing a prefab.

Does anyone know the best way to go about this?
Thanks

You can do pretty much anything in an editor.

I personally would just use a TextAsset of a text file, or even just a text area for a string, that looks like:
0000
0000
0BB0

Instead of writing a custom level editor for what seems like a fairly straight forward layout.

3 Likes

I’ve used @GroZZleR 's text grid to level definition approach many times, probably dating back to hacking in BASIC on my Vic-20 computer in 1982 or so. Definitely tried and true, works all day long.

Another handy way is to define the grid with a texture of colored pixels, then produce the scene from that.

I made an example and added it to my MakeGeo project. It’s called Bitmap2Grid and has a configuration scriptable object that gives any number of color mappings to map to things that are spawned.

You can chop it up and use the basic GenerateGrid function at runtime if you prefer, or at scene design time.

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

https://github.com/kurtdekker/makegeo

3 Likes

How would you go about converting the text to values? :Just run through each line and run through the string with a for loop, and have an array of “keys” that tells which letter corresponds to which enum?

That might be fine, but also keep in mind, the grid has a set width/height, 11wide and 11 tall for each scene. That means I’d need
00000000000
00000000000
00000000000
etc until I have 11 rows lol

Thanks, this might work, if I made the texture 11x11 pixels exactly, I’ll try it out.

Grozz’ suggestion just needs a switch statement, which you can see in my example code. Just change it from those banded color quantity strings to instead look for a “0” or “1”

And string.Substring() will let you quickly chop strings up.

1 Like

Okay, thanks. I’m dumb and have never used the Substring method before haha, how does it work exactly? I did some googling, and found it takes in two ints but couldn’t really find what these ints do… Start character and finish character index?

If you are unable to get anything out of the C# documentation then my feeble scribblings won’t help you either.

Go read the docs, make some code to try it out, learn how it works.

Don’t know if you’re willing to spend money on the asset, but Odin Inspector has an attribute that can do it. I’ve used it to make Level Editor for the Bricks Breaker type game…

1 Like

Can’t remember if I found the docs or some other page, but it didn’t give much explanation lol, it didn’t even really say what the 2 ints were for.

That looks really cool, I might consider buying it when i’m not super broke lol, I hear Odin has all kinds of useful features…

The first int parameter in Substring is the character index of a string value, and the second int parameter is how many characters to get starting from that index.

Say you have the string value, "Good morning world", and you want to isolate the word “morning” from it.
You’d want to Substring it starting from the index of “m”, and counting how many characters it takes to get from “m” to “g”.

You can think of a string as an array of characters, because that is essentially what strings are anyway:

char[] x = new char[] { 'G', 'o', 'o', 'd', ' ', 'm', 'o', 'r' , 'n', 'i', 'n', 'g', ' ', 'w', 'o', 'r', 'l', 'd'};

Like arrays, the character indexes of strings start at 0, so the index of “m” is 5.
Then, we count how many characters ahead of “m” there are until we reach “g”, which is 7 (“m” is included in the count):

string myString = "Good morning world";

//When we substring "myString", start at index 5, count 7 characters ahead, and get all the characters in between.
//mySubstring = "morning".
string mySubstring = myString.Substring(5, 7);

Here are the official docs for Substring as well:
https://docs.microsoft.com/en-us/dotnet/api/system.string.substring?view=net-6.0

2 Likes

Ohh, I get it now. I thought the first int was the index but i thought the second int was the SECOND index haha, so i thought it got the characters from index 1 to index 2. Thanks for the explanation, i understand it now!

1 Like

It can be confusing like that at times because this actually is how Substring functions are implemented in some other programming languages.
The solution to the same example in JavaScript, for instance, would be this:

let myString = "Good morning world";
let mySubString = myString.substring(5, 12);

Where 5 is the index of character “m”, and 12 is the index of the character after “g”.

I’m not sure which version I prefer.

1 Like