playMaker

Author Topic: Random Levels in Array  (Read 784 times)

rizwanash

  • Junior Playmaker
  • **
  • Posts: 61
Random Levels in Array
« on: June 28, 2019, 05:59:35 AM »
Hey guys,

I have some random level prefabs in an array variable. I am using Simple Array Variable with Get random array and then creating object. Is there any way i can keep the randomly picked level until player completes the level ? i mean right now player dies and a random level is generated. which looks weird. can i keep the first randomly picked level until level is completed ?

Thank you very much.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Random Levels in Array
« Reply #1 on: June 28, 2019, 07:21:54 AM »
Hi.
how is your setup?

when die you go to menu scene and level is chosen there?

if so :

You could use a player prefs int to save the index and also a player prefs int to check if level completed. (normally this can be done with a bool, but player prefs does not save bool)

To use player prefs, 1st you need to use 'Player Prefs Has Key' to see if a save exists.

if it does not exist use 'Player Prefs Set Int' 2x

one for index (name key "LevelIndex" for example)
and on for level complete (name key "LevelCompleted" for example)
Set the LevelCompleted value to 2

if it does exist do not save but continue to next state

Then use 'Player Prefs Get Int' and get the LevelCompleted value.

The use a int switch.
Set count to 3 and place 0,1 and 2
(0 is Level not finished, 1 is Level finished, 2 is 1st game started)

if 2 you should transition to the "get random array state" directly.

if 1 you can get the LevelIndex and use 'Array Delete At' if you want to remove that level. and then go to the "get random array state"

if 0 you can get the LevelIndex and use 'Array Get' with the LevelIndex as index.
and skip the "get random array state".

When you do the 'Array Get Random' you should store the index in a variable and then use 'Player Prefs Set Int' with LevelIndex key to save the index nr.