Load random scene/level

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Caidran
Posts: 20
Joined: Wed Jan 03, 2018 5:02 pm

Load random scene/level

Post 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();
}
}
User avatar
Tony Li
Posts: 21678
Joined: Thu Jul 18, 2013 1:27 pm

Re: Load random scene/level

Post 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.
Caidran
Posts: 20
Joined: Wed Jan 03, 2018 5:02 pm

Re: Load random scene/level

Post 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
User avatar
Tony Li
Posts: 21678
Joined: Thu Jul 18, 2013 1:27 pm

Re: Load random scene/level

Post by Tony Li »

Happy to help!
Post Reply