Tip: Managing Spawnpoints

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Tony Li
Posts: 21721
Joined: Thu Jul 18, 2013 1:27 pm

Tip: Managing Spawnpoints

Post 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;
    }
}
User avatar
Strook
Posts: 70
Joined: Fri Nov 08, 2019 10:51 am

Re: Tip: Managing Spawnpoints

Post 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 !
Currently working on ->Squeakross: Home Squeak Home<- Using Dialogue System Save System and other features.
Previous game made with Dialogue system ->The Spirit and The mouse<-
User avatar
Tony Li
Posts: 21721
Joined: Thu Jul 18, 2013 1:27 pm

Re: Tip: Managing Spawnpoints

Post by Tony Li »

Thanks! Glad you found it helpful.
fallingstarint
Posts: 22
Joined: Fri Oct 01, 2021 11:50 am

Re: Tip: Managing Spawnpoints

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

Re: Tip: Managing Spawnpoints

Post 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.
fallingstarint
Posts: 22
Joined: Fri Oct 01, 2021 11:50 am

Re: Tip: Managing Spawnpoints

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

Re: Tip: Managing Spawnpoints

Post 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.
Post Reply