Player spawn with DontDestroy

got scripts? Need scripts?
Post Reply
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Player spawn with DontDestroy

Post by SpiritWebb »

I am having problems trying to figure this out. I am looking for a script (used for OuterNet) that when a player leaves the star system, they are spawned back on the outside of the star system in the main scene called "Sector 1" and also includes the DontDestroyOnLoad(this) feature. I have 15 star systems in the main scene that will require a spawn point for each star system, and a spawn point inside each star system after entering from the main grid.

Does anyone know how to do this? I'm in need of this. With this the game will have taken a huge step forward so I can start linking the scenes to the main grid and back.

Right now, my script to load the main grid looks like this:

Code: Select all

function OnTriggerEnter(){
  Application.LoadLevel("Sector 1");
}
And this is the code I am using for entering the system:

Code: Select all

var availableLevels : String[];
var windowWidth : int = 200;
var windowHeight : int = 100;
var target : ShipControlScript;
private var showMenu : boolean = false;

function OnTriggerEnter () {  //Replace TriggerAction with your desired means of triggering the menu.
     target.flyingSpeed = 0;  //stops ship in tracks and loads menu
     showMenu = true;
}

function OnGUI () {
    if (showMenu) {
        GUI.Window(0, 
                   Rect((Screen.width / 2) - (windowWidth / 2), 
                        (Screen.height / 2) - (windowHeight / 2), 
                        windowWidth, 
                        windowHeight), 
                   LevelSelect, 
                   "Enter Star System?");  //Creates a window with ID 0 in the center of the screen using the function LevelSelect with the title, "Select a level!"
    }
}

function LevelSelect (id : int) {
    for (var levelName : String in availableLevels) {
        if (GUILayout.Button(levelName)) {
            Application.LoadLevel(levelName);
        }
    }
    if (GUILayout.Button("Cancel")) {  //Gives the player the opportunity to back out of the menu.
        showMenu = false;
    }
}
Image

Image
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Player spawn with DontDestroy

Post by Callan S. »

15 star systems in the main scene that will require a spawn point for each star system, and a spawn point inside each star system after entering from the main grid.
Is that two spawn points or one? I'm trying to visualise what you mean and a SP for each star system and a SP inside each star system - sounds like the same thing?

To me it just sounds like you'd have some value with 1-15 being which star system they are in, and with 0 meaning they are in sector one. Then have X & Y co-ords for where they are in any particular star system?
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Player spawn with DontDestroy

Post by SpiritWebb »

Callan S. wrote:
15 star systems in the main scene that will require a spawn point for each star system, and a spawn point inside each star system after entering from the main grid.
Is that two spawn points or one? I'm trying to visualise what you mean and a SP for each star system and a SP inside each star system - sounds like the same thing?

To me it just sounds like you'd have some value with 1-15 being which star system they are in, and with 0 meaning they are in sector one. Then have X & Y co-ords for where they are in any particular star system?

Basically on the main grid are 15 star systems, linked to their own scene thats titled to the star system. I need a spawn point for the player near each star system on the main grid, and one within the star system. So for example, I am flying on the main grid to Sol (our solar system), I choose to enter it, now I am spawned without being destroyed when loading Sol. Now I have explored Sol and ready to leave. I fly to the outer edge of the system, and automatically enter back into the main grid near the Sol system without being destroyed. The DontDestroyOnLoad(this) function.

So would need to place 15 on the main grid and one in each star system so they can be placed back on the main grid next to the system they just left. If that makes sense.
Image

Image
User avatar
Sakar
Posts: 520
Joined: Thu Apr 23, 2009 2:59 am

Re: Player spawn with DontDestroy

Post by Sakar »

Just add the DontDestroyOnLoad in the Awake function.

Code: Select all

function Awake() {
     DontDestroyOnLoad(gameObject);
}
And for choosing the spawn point, just find the object with whatever name (like "SolSpawn" or something), and set that as the target object in the script. Then position the player at that object's position.

Code: Select all

// Pseudo code, kinda
function Start() {
     var levelName = Application.loadedLevelName;
     var spawnPoint = Find(levelName + "SpawnPoint");
     transform.position = spawnPoint.position;
}
That code will will look up the current scene's name (for example, Sol). It then finds the object with a name containing the level's name and SpawnPoint (in this example, SolSpawnPoint). Then it positions the object it is attached to to the spawn point's position. This only works as is for a single spawn point. For multiple points, store the the newly entered scene's name in a variable, then when you move to a new scene use the variable to find a spawn point.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Player spawn with DontDestroy

Post by SpiritWebb »

Sakar wrote:Just add the DontDestroyOnLoad in the Awake function.

Code: Select all

function Awake() {
     DontDestroyOnLoad(gameObject);
}
And for choosing the spawn point, just find the object with whatever name (like "SolSpawn" or something), and set that as the target object in the script. Then position the player at that object's position.

Code: Select all

// Pseudo code, kinda
function Start() {
     var levelName = Application.loadedLevelName;
     var spawnPoint = Find(levelName + "SpawnPoint");
     transform.position = spawnPoint.position;
}
That code will will look up the current scene's name (for example, Sol). It then finds the object with a name containing the level's name and SpawnPoint (in this example, SolSpawnPoint). Then it positions the object it is attached to to the spawn point's position. This only works as is for a single spawn point. For multiple points, store the the newly entered scene's name in a variable, then when you move to a new scene use the variable to find a spawn point.
Where do I place the dontdestroyonload(this) at? In the player object script, or on the spawn point script?
Image

Image
User avatar
Sakar
Posts: 520
Joined: Thu Apr 23, 2009 2:59 am

Re: Player spawn with DontDestroy

Post by Sakar »

Both scripts would go on the player object.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Player spawn with DontDestroy

Post by SpiritWebb »

Sakar wrote:Both scripts would go on the player object.
Thanks, will try that out when I get home from work and let you know the outcome!
Image

Image
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Player spawn with DontDestroy

Post by SpiritWebb »

Tried it out, and receive:
Unknown identifier: 'Find'.
Image

Image
Post Reply

Return to “Scripting/Coding”