playMaker

Author Topic: (SOLVED BY ME) Setting a game object for spawning a player.  (Read 9128 times)

markfrancombe

  • Sr. Member
  • ****
  • Posts: 338
Im trying to make a simple system for spawning a player to the correct location when a scene loads.

Specifically: When a character clicks a door, the door loads a new scene that contains the buildings interior. As there are a number of different possible entrances to the building, I cant hard-code the players location when the new interior scene loads.

I want to use Create Object, and use a game object as the location for the spawn. (this is so I can simple move the game object to tweak the exact location.)
But I cant set a global game object variable in one scene when the game object exists in another scene.
I thought I might set a normal string variable, and somehow turn that into a game object in the new scene, but cant figure out how.
Or maybe somehow set a global event, so that first frame in the new scene that transition would fire and take me to the correct state for the correct location.. but cant figure it out.

How should I do this?

Mark




« Last Edit: July 24, 2013, 11:22:13 AM by markfrancombe »

markfrancombe

  • Sr. Member
  • ****
  • Posts: 338
THE SOLUTION!
« Reply #1 on: July 24, 2013, 11:21:36 AM »
In order to help others I hereby post my solution. Maybe its obvious, but I think its quite elegant, and modular!

Its simple setting ONE variable, a string (could be anything, and int, but I like to use "real names" where possible)

Firstly I have a very generic ITEM Fsm, that I Place on everything dynamic. Its showing some gui text about the thing, and has a state (after the mouse click to select the "thing) where I place actions relating to what needs to happen on click!

Secondly I have a Generic Level_Setup Gameobject and FSM, where I spawn things when the level loads.
The first state looks for a global variable called Spawn_Location (its a String Compare action) and transitions to a new state  which spawns the player to the location of a dummy Game Object.

So in the ITEM Fsm, Ive added an action that sets this Spawn_Location, for example hospital door. Then it transitions to an action that loads that scene (the one with the hospital in it)

That scene loads, the Level Setup FSM checks the current name of the String Variable (In this case hospital door) and dumps the player right on the dummy game object.

This is brilliant in so many ways. First, I can MOVE the dummy game objects without having to code new positions or coordinates. Secondly the variable is simply a statement of location, where is the player now? At the Front door!

Im sure this was very simple, but Im pretty happy with it.
I have a few more things to iron out, like somehow saving states in one scene so they persist on re entering a Scene. Like I have one door hidden behind a bunch of planks... you must first break your way thru (just physics, smash smash.. nothing special here) but the second time in I dont want these planks to have magically re-built themselves... but Im sure Ill get to that!

Mark



jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: (SOLVED BY ME) Setting a game object for spawning a player.
« Reply #2 on: July 29, 2013, 05:42:08 AM »
Hi,

 Very good, thanks for sharing!

bye,

 Jean

spacerobot

  • Playmaker Newbie
  • *
  • Posts: 17
Re: (SOLVED BY ME) Setting a game object for spawning a player.
« Reply #3 on: September 26, 2014, 01:34:23 PM »
The method mentioned by Marc above is exactly what I need to get working. I seem to be having some trouble though. I don't exactly know what the particulars should be for each of those steps.

Mark or anyone could you post an FSM that shows a little more detail on this?

markfrancombe

  • Sr. Member
  • ****
  • Posts: 338
Re: (SOLVED BY ME) Setting a game object for spawning a player.
« Reply #4 on: September 27, 2014, 04:02:06 PM »
Yes I can help with this!

So lets suppose you have a very simple "level based" game, where the player enters a level at a start position, does stuff, and then leaves the level by another location. THEN completes THAT level etc. No problems there really, BUT if you want your character to be able to "go back" to a previous level, then he will spawn from the start, right? Not where he exited from. So we need to make a system where the game knows where the player is, and load him to there instead.

First... Make your Player a prefab:
(drag to library) and disable your player character in your scene. We will be spawning him in from the start.

Add Spawn positions:
Can be anything, an empty game object, but I use a red sphere, but I will turn off the rendering of the sphere when my game is done, but its nice to have something visible, that you can select easily and move about.
Copy it to anywhere, in all levels, where your character might need to spawn to. (in our example start and end of the level. Name each one something sensible, and drag it to Library to make it a prefab too! (so you have lots of these, all differently named.)

Create a global String variable, and name it Spawn_Location

Make an empty game object and call it SPAWN_CONTROL:
 (or something) add an fsm to it. Also make it a prefab and add it to all your scenes. You will need to update (hit apply) every-time you add something to this.
In this we will find out what a variable is currently called, and depending on that set a Spawn Point, then spawn our player there.

An heres what it looks like:

Each String Compare looks to see what the current name of Spawn_Location is, and send it out to...

...where Spawn Point is one of the red spheres prefabs. The place you want the guy to spawn too, when the variable is set thus...

But how do you set the variable?

Make a collider object and call it SCENE_CHANGER:

Add an fsm. Make it the thing that the player must bump into or click when they need to change levels. (over a door way, or a teleport pad, or a hole in the ground, whatever you game is about)
In the image you can see something called Get message from vp switch, this is just a message coming from another bit of my game where I handle clicks on objects and stuff... you could do anything, walk into a collider, or click a button
In this fsm we will SET the above mentioned variable, to tell SPAWN_CONTROL where our player currently is... so that the SPAWN_CONTROL fsm in the next scene will know which Spawn Sphere to send you man at...

AND it will load that next scene.


Hope thats helps...


Mark

spacerobot

  • Playmaker Newbie
  • *
  • Posts: 17
Re: (SOLVED BY ME) Setting a game object for spawning a player.
« Reply #5 on: September 29, 2014, 08:35:11 AM »
Mark! Excellent. Thank you for this write up. I now have everything working correctly!

markfrancombe

  • Sr. Member
  • ****
  • Posts: 338
Re: (SOLVED BY ME) Setting a game object for spawning a player.
« Reply #6 on: September 29, 2014, 08:42:30 AM »
Glad to be of assistance!

marcustegtmeier

  • Playmaker Newbie
  • *
  • Posts: 13
Re: (SOLVED BY ME) Setting a game object for spawning a player.
« Reply #7 on: April 02, 2015, 03:02:38 AM »
I'm building a game that is something like Zelda meets Animal Crossing so I've been trying to implement this and it doesn't seem to work for me, the character always spawns at the location of whichever spawn sphere is first in the list regardless if the sphere is even in the scene.  This has been giving me a headache all day, any pointers?

marcustegtmeier

  • Playmaker Newbie
  • *
  • Posts: 13
Re: (SOLVED BY ME) Setting a game object for spawning a player.
« Reply #8 on: April 02, 2015, 03:39:24 AM »
Also in the Spawn_Control FSM where it says Compare To, what's supposed to go in there exactly?  The scene or a spawn sphere or something else?  I wish there was a video of this so I could see the full layout and how it's really set up.

markfrancombe

  • Sr. Member
  • ****
  • Posts: 338
Re: (SOLVED BY ME) Setting a game object for spawning a player.
« Reply #9 on: April 02, 2015, 06:17:52 AM »
Also in the Spawn_Control FSM where it says Compare To, what's supposed to go in there exactly?

Thats your global variable, you HAND WRITE that in that box. Its set by the last thing that happens in the previous scene, before this one is loaded. (see below)
Remember you made one called SpawnLocation?
So in the main Spawn_Control FSM, All the "string compares" check the name of that variable and if its the one it wants, if it is, it sends it out to the Create Object action.

Remember this whole thing happens the second a scene loads.
Note the default one on top, here I usually drag which ever spawn sphere i need to be near when testing in that level. So UNDEFINED happens there...

THEN... setting the variable.
however you do that (I have to CLICK on a door usually) but maybe you just walk THRU a door, so lets talk about THAT?

You cover a door with a collider (a primitive box with the renderer turned off)
this is the scene changer.
ON that you put whatever you like (I like to fade out the camera) but DEFINATELY
set string value (set the variable the next scene needs)
and load Scene (DO the spawn)


Final example.
Internal scene is a house with a font door and back door, next scene garden the garden has 2 exits, the house or the gate:

Collider over back door sets the global variable "WhereAmI" to "BackDoor"
and spawns garden scene
Garden loads SpawnControl checks what the variable is called, and discovers its "BackDoor"
The player is spawned to the spawn sphere thats just outside the back door.

He realises he needs to go back in, turns round bumps into the back door, its got a collider on it that sets the variable to Back Door (It already is I know, but it might be the Garden Gate now) and loads the scene, back in the house, the house check whats the variable? and puts him at teh backdoor spawn, not the front door spawn point..

Hope this helps



marcustegtmeier

  • Playmaker Newbie
  • *
  • Posts: 13
Re: (SOLVED BY ME) Setting a game object for spawning a player.
« Reply #10 on: April 03, 2015, 01:49:28 PM »
Thanks Mark, I fiddled with it a bit and got it working.  Looks like I wasn't doing the actual creation of the player prefab correct, I had the spawn location set as where they were coming from instead of where they were going so the compare to string was hitting a dead end.  Now it's working beautifully.

Any tips on getting the main camera to follow the player(clone) when they are spawned in the scene?  I'm thinking it has something to do with the main camera following game objects tagged with player.  I'll mess with it.

markfrancombe

  • Sr. Member
  • ****
  • Posts: 338
Re: (SOLVED BY ME) Setting a game object for spawning a player.
« Reply #11 on: April 03, 2015, 03:09:06 PM »
Quote
Any tips on getting the main camera to follow the player(clone) when they are spawned in the scene?

well that rather depends on how your are doing your camera, are you 1st person or 3rd person?

you can just somehow include your whole camera setup in the Player Prefab, so you spawn the whole thing...just add the cameras as a child? If you use physics/Spring joint, maybe it will bounce a little?

or add a FSM to the player, use Get Position that sets a variable called MyPosition THEN have your camera have an FSM with Set Position and read the global variable called MyPosition..

it rather depends on the game...

Mark


marcustegtmeier

  • Playmaker Newbie
  • *
  • Posts: 13
Re: (SOLVED BY ME) Setting a game object for spawning a player.
« Reply #12 on: April 08, 2015, 02:39:54 AM »
I tried to parent the camera to the player but I didn't want the camera to rotate at all so I ended up using this video to add a simple controller to my character and then created an empty game object with the same controller on it minus any rotation.  It works for now anyway, we'll see if that causes problems down the road.  This is only used for my "over world" area, any interiors will have a fixed camera.  I actually use the spawn controller to also spawn the camera controlling empty game object prefab to any spawn locations in the "over world" scene.  Thanks for all the help Mark.

ncindie

  • Playmaker Newbie
  • *
  • Posts: 4
Re: (SOLVED BY ME) Setting a game object for spawning a player.
« Reply #13 on: January 22, 2016, 04:18:30 AM »
I am truly, very sorry to necro a dead thread, but I just wanted to say a MASSIVE HUGE thank you for this thread to the OP.

I spent hours trying to set a similar solution up but things weren't setting as I wanted. Found this thread and BOOM, all is operational after 1 small tiny kink, 1 delete and 5hrs later :S (Forgive me for I am a new playmaker convert and barely know c#). When I transitioned from one scene to the next all worked fantastic - after I realised my duplicate player mistake etc. But then when transitioning back to my first scene it would load the second scene again!!

Important lesson kids, I fixed this by changing my trigger in scene 2 to make sure the next level being loaded was in fact scene 1, and not scene 2, like the original prefab had been set up for - if you get my meaning.

Thank you again OP if you are reading this. OP you were my only hope!! :P