Alright, I spent some time on this one as it seemed like a fun morning brainhunt and it’s directly useful for many other projects.
I’ve included in the example an implementation of loading from a save file as well since that seems like an important thing to do in this case. If you have further questions feel free to ask!
So! time to explain
I wanted to start with a struct to keep everything level-oriented grouped together nicely. So create a struct in your content browser that contains the following:
Level unlocked image array
Level locked image array
Is Level Unlocked Boolean array
Populate the arrays with your desired content and default values as shown in my example here:
Next, on construct we need create an array of all the level buttons or images and save that off as a variable ‘level button array’ for the next few steps. Then we need to make sure we set all the images to their correct ‘islevelunlocked?’ state for each index of the ‘level button array’.
I added the optional branch “does save file exist” in order to give you a chance to load save data to the struct before the initial foreach reads from the struct on construct.
Next I made an example of how you would unlock the next level. To do that we need to scan for the next locked level and return the index of that in order to unlock it and set its widget style to unlocked. Optionally, and not shown, you can save off the last unlocked level as a variable and use index +1 instead of a foreach whenever you unlock a level. This would be useful if you had a large amount of levels.