Page 1 of 1

Load random scene/level

Posted: Tue Feb 06, 2018 10:32 am
by Caidran
Hello again,

I'm looking for a method to implement a sequence that loads a scene at random from a list of possible scenes to allow for random events to trigger at certain transitions between conversations. Is this currently doable within DS or am I looking at some custom scripting?

I've had a look at the loadlevel sequencer script and I believe it should be possible to add some code that accepts a list of scene names and loads one at random, resulting in a sequence command similar to;
LoadRandomLevel(Level1, Level2, Level3)
I've not had a lot of time today to look into creating said script but I plan to attempt this tonight but if it is possible already or if anyone has any suggestions with regards to fashioning the script, please let me know :)

Cheers

LoadLevel sequencer for reference;

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class SequencerCommandLoadLevel : SequencerCommand {

public void Start() {
string levelName = GetParameter(0);
if (!string.IsNullOrEmpty(levelName)) {
PersistentDataManager.Record(); // Let current persistent objects save themselves.
Application.LoadLevel(levelName);
}
Stop();
}
}

Re: Load random scene/level

Posted: Tue Feb 06, 2018 11:55 am
by Tony Li
Hi,

Try this:

SequencerCommandLoadRandomLevel.cs

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class SequencerCommandLoadRandomLevel : SequencerCommand 
{

    public void Start() 
    {
        string levelName = Parameters[Random.Range(0, Parameters.Length)];
        var levelManager = FindObjectOfType<LevelManager>();
        if (levelManager != null)
        {
            levelManager.LoadLevel(levelName);
        }
        else
        {
            PersistentDataManager.Record();
            PersistentDataManager.LevelWillBeUnloaded();
            Tools.LoadLevel(levelName);
            PersistentDataManager.Apply();
        }
        Stop();
    }
}
It uses the SequencerCommand class's Parameters property.

It also uses the scene's Level Manager if present; otherwise it uses Tools.LoadLevel along with updating the scenes' persistent data.

Re: Load random scene/level

Posted: Tue Feb 06, 2018 12:29 pm
by Caidran
Fantastic, that works exactly as I was hoping for!

I had a couple of hours set aside to try and get it working but I can cross that off now and move on to something else, thanks a lot Tony. :D :D :D

Re: Load random scene/level

Posted: Tue Feb 06, 2018 1:11 pm
by Tony Li
Happy to help!