This is my code to enable buttons that allows the player to play the next level.
Can the code be made better? So that I don’t need to make a variable for each level. Or some how bind the level button to the level name?
Hey @ddave104!
So what you can do first is make an array of bools and name each bool for each level. Keep this in the save game. When a level is complete, mark that bool as true.
When you go to load, you can do a “ForEachWithBreak”. Input the list of level bools and the first one that returns true, run the break so it stops iterating over the array and continues your code! You can even make another array of Names for the levels, and then compare the index of the boolean entry that returned true, and use that name for Load Level (meaning you only need one button)! ![]()
Hope that helps, let us know if you need more clarification once you give it a shot!
What?
the image shows a variable with name and boolean already.
im trying to find how I can connect the button to those levels
Hi, i think the modular way would be to -
create a widget with only button and a boolean variable instance editable and expose on spawn
have a logic to spawn this new widget with button in runtime like add child to scroll box or uniform grid panel as per your design.
As per picture, save till how many levels are done in a int variable. while looping and adding, you can check the for loop index with this levels done integer and till the index is greater, you can put that bool to false (bool gets shown when you create widget as we put that variable to instance editable and expose on spawn) and for false condition, you set the bool to true.
and on event construct of the widget with button thats created on 2nd step, maybe enable/disable button accordingly. This way, it can be modular irrespective of number of buttons i think