Carrying player stats from level to level
by Craig Perko · in Torque Game Builder · 03/13/2008 (2:25 pm) · 9 replies
I wasn't ever able to find a tutorial on level control, so all I do is use "sceneWindow2D.loadLevel(%level);"
This works fine, except that I'm using behaviors to control the player's capabilities. If I put an avatar in each level, then the behaviors all reset. I've tried checking "persistent', but then all the behaviors mysteriously stop working and the avatar is just an immobile sprite.
How do people get around this? As the player has a variety of sprites associated with him - armor, weapon, etc - it would be a bit of a pain to re-initialize them each time. I just want to carry the avatar, his behaviors, and his mounted stuff from level to level.
-Craig
This works fine, except that I'm using behaviors to control the player's capabilities. If I put an avatar in each level, then the behaviors all reset. I've tried checking "persistent', but then all the behaviors mysteriously stop working and the avatar is just an immobile sprite.
How do people get around this? As the player has a variety of sprites associated with him - armor, weapon, etc - it would be a bit of a pain to re-initialize them each time. I just want to carry the avatar, his behaviors, and his mounted stuff from level to level.
-Craig
About the author
#2
Unless you mean spawning the character manually when the level is loaded. Then I have the same problem: the character has a dozen attachments and behaviors, each of which has very specific stats.
-Craig
03/13/2008 (2:44 pm)
No, I have no idea how to do that. Is there an example hidden somewhere?Unless you mean spawning the character manually when the level is loaded. Then I have the same problem: the character has a dozen attachments and behaviors, each of which has very specific stats.
-Craig
#3
Create the player in script by making a new function such as:
Then, open the level file, where you customized your player's behaviors and whatnot, with a text editor. (I assume that you did create the player in a scene in the level builder?) Find your player (You may want to name it in the level builder.) Then copy all the fields you find there into your createPlayer function.
You may want to pass some arguments that define a custom position.
Cheers,
Kevin
03/13/2008 (5:04 pm)
Craig,Create the player in script by making a new function such as:
function createPlayer()
{
%player = new t2dAnimatedSprite(player)
{
//all your fields here
};
}Then, open the level file, where you customized your player's behaviors and whatnot, with a text editor. (I assume that you did create the player in a scene in the level builder?) Find your player (You may want to name it in the level builder.) Then copy all the fields you find there into your createPlayer function.
You may want to pass some arguments that define a custom position.
Cheers,
Kevin
#4
Passing dozens of statistical parameters to the function is inefficient and fragile, which is why I wasn't doing it that way. There's no benefit to doing it this way over manually placing the player and then moving him.
So, the solution is probably to stop using behaviors for everything. Store the player's statistics globally.
-Craig
03/13/2008 (5:11 pm)
I think my problem is that I decided to do everything with behaviors, which are very tightly linked to the sprite. So I don't have a global "player" object with things like XP, weapon, so forth. But these things are persistent across levels.Passing dozens of statistical parameters to the function is inefficient and fragile, which is why I wasn't doing it that way. There's no benefit to doing it this way over manually placing the player and then moving him.
So, the solution is probably to stop using behaviors for everything. Store the player's statistics globally.
-Craig
#5
I don't think global variables are persistent across level loads, but I could be wrong.
03/13/2008 (6:03 pm)
Generally, using behaviors is much better than using a combination of global variables. But I don't think its a good idea to use behaviors as global variables. Actually, for statistical attributes, I don't know why you would use behaviors, just store the information in a dynamic field on the player. You can save the player to an object file then load the file.I don't think global variables are persistent across level loads, but I could be wrong.
#6
03/14/2008 (7:31 am)
I would actually make the entire player a global object, then place a visual representation of the player into the level.
#7
03/14/2008 (7:34 am)
That's pretty much what I'm converting it over to, at the moment.
#8
You could do that for any sprite that is part of your sceneGraph. Note that in my case my sceneGraph is named "gameSceneGraph" -- you'd have to change this to whatever yours is named. And of course your sprite's name instead of $gPlayerShip.
The effect of these calls is that loadLevel() won't delete $gPlayerShip when it loads the new level, since my ship is no longer in the sceneGraph.
There are probably better solutions, but this worked for me.
-Vern
03/26/2008 (12:34 pm)
I recently ran into this myself. For me, I didn't want loadLevel() to delete the player's sprite, which also had certain variables I didn't want to lose. So I did this before calling loadLevel():if (isObject($gPlayerShip) ) gameSceneGraph.removeFromScene($gPlayerShip); // load level here... if (isObject($gPlayerShip) ) gameSceneGraph.addToScene($gPlayerShip);
You could do that for any sprite that is part of your sceneGraph. Note that in my case my sceneGraph is named "gameSceneGraph" -- you'd have to change this to whatever yours is named. And of course your sprite's name instead of $gPlayerShip.
The effect of these calls is that loadLevel() won't delete $gPlayerShip when it loads the new level, since my ship is no longer in the sceneGraph.
There are probably better solutions, but this worked for me.
-Vern
#9
03/26/2008 (7:07 pm)
Huh, I'm surprised that worked. But that's cool; I'll have to remember that!
Torque 3D Owner Jason Ravencroft