Page 1 of 1

Tip: Managing Spawnpoints

Posted: Wed Dec 13, 2017 2:00 pm
by Tony Li
Someone asked some good questions about spawnpoints in an email.

In the LoadLevel() sequencer command, you can specify where the player will spawn in the new level. For example:

Code: Select all

LoadLevel(New Level, Spawnpoint From Old Level)
will load New Level and place the player at the position of the GameObject named "Spawnpoint From Old Level".

The LoadLevel() command sets the actor named "Player"'s Spawnpoint field. In Lua, that field is:

Code: Select all

Actor["Player"].Spawnpoint = "Spawnpoint from Old Level"
(Spawnpoints can apply to any object. If the object has a Persistent Position Data component, it will check for an actor field named "Spawnpoint". Further down in this tip, I'll explain how to set spawnpoints for other characters, too.)

A few screenshots will help illustrate how this works. In the example, there are two scenes: MAINSCENE and ANOTHERSCENE_UMA.

The screenshot below is in the first scene (MAINSCENE). The "Portal to ANOTHERSCENE" GameObject has a Sequence Trigger with a LoadLevel() command to load ANOTHERSCENE and position the player at "Spawnpoint from MAINSCENE":

Image

The screenshot below is in ANOTHERSCENE_UMA. It shows where "Spawnpoint from MAINSCENE" is. This is where the player will appear:

Image

You can manually set spawnpoints for other objects before loading the level. For example:

Code: Select all

Actor["Second Player"].Spawnpoint = "Second Spawnpoint In New Level";
Actor["Companion"].Spawnpoint = "Companion Spawnpoint In New Level";
etc.
To do this, you may want to use a Dialogue System Trigger instead of a Sequence Trigger. This will allow you to run Lua code to set their Spawnpoint fields before loading the next scene:

Image

In the upcoming version 1.7.7, the order in the Action section (Run Lua Code, Play Sequence, etc.) will be changed to accurately reflect the order in which they're executed.

The Persistent Position Data component clears the Spawnpoint field after using it. However, if for some reason a spawnpoint isn't used, it won't be cleared. Before you save a game, you can clear the spawnpoints manually:

Code: Select all

Actor["Player"].Spawnpoint = "";
Actor["Second Player"].Spawnpoint = "";
Actor["Companion"].Spawnpoint = "";
Or automatically using a script like this:

ClearSpawnpointsInSavedGames.cs

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class ClearSpawnpointsInSavedGames : MonoBehaviour
{
    void Start()
    {
        PersistentDataManager.GetCustomSaveData = GetClearSpawnpointsCode;
    }

    string GetClearSpawnpointsCode()
    {
        string s = string.Empty;
        foreach (var actor in DialogueManager.MasterDatabase.actors)
        {
            var actorIndex = DialogueLua.StringToTableIndex(actor.Name);
            s += "Actor['" + actorIndex + "'].Spawnpoint = ''; ";
        }
        return s;
    }
}

Re: Tip: Managing Spawnpoints

Posted: Sat Jul 18, 2020 3:03 pm
by Strook
This was so helpful ! I had started implementing my whole scene load management, but I ditched everything to use this instead, so much better !

Re: Tip: Managing Spawnpoints

Posted: Sat Jul 18, 2020 4:52 pm
by Tony Li
Thanks! Glad you found it helpful.

Re: Tip: Managing Spawnpoints

Posted: Sat Feb 12, 2022 10:37 am
by fallingstarint
Hello,

I have been trying to implement this solution like this:

- Player plays the first scene
- Is sent to a "cinematic" scene containing an empty gameobject having a Dialogue System Trigger that triggers this sequence:

Code: Select all

required LoadLevel(City, SpawnPointFromOpener)@90;
(90 being the time of the cinematic)
- Player now is in a scene called "City" but spawns where the gameobject of the player is placed in the scene instead of the "SpawnPointFromOpener"' as written in the sequence.

The City scene has an empty gameobject called "SpawnPointFromOpener" and my player is called "Chika".

I feel like I am missing a point, could you enlight me? Thanks in advance!

Re: Tip: Managing Spawnpoints

Posted: Sat Feb 12, 2022 11:07 am
by Tony Li
Hi,

Does your player have a Position Saver component? Make sure the Position Saver has a unique Key value such as "PlayerPosition", and Use Player Spawnpoint is ticked.

Re: Tip: Managing Spawnpoints

Posted: Sat Feb 12, 2022 11:16 am
by fallingstarint
Tony, you are insane! Thank you, it solved my problem!

A last question about this topic though: Do I need to have a Persistent Position Data component to my Player's gameobject if I have a Position Saver component? Then, what gameobjects should have it too?

Re: Tip: Managing Spawnpoints

Posted: Sat Feb 12, 2022 2:29 pm
by Tony Li
No. Generally speaking, there's no need for any Persistent*** components. They've all been superceded by Saver components. (For example, PersistentPositionData is obsolete, replaced by PositionSaver.) The Persistent*** components are still available for older projects that already use them.