Author Topic: Saving our playstate  (Read 2034 times)

princewessy

  • New Member
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Saving our playstate
« on: Fri, Dec 16, 2011 »
Hi,

We are building our game and we use 2 state's, the player can go from the playstate to a minigame state.
If the player completes the mini game he gets money in the playstate, but the problem is that if u switch states u lose all the data in the current state. So we have to be able to save the data from the state, and load it when returning to the playstate.

What would be the best way to save the data, i am just a beginner in flixel but eager to learn.

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

c023-DeV

  • Game Artisan
  • Active Member
  • ***
  • Posts: 176
  • Karma: +0/-0
    • View Profile
    • DeV-ZoO
Re: Saving our playstate
« Reply #2 on: Sat, Dec 17, 2011 »
Yep +1 for Photon's great advice, I store all my cross level data in a Registry.as ...
If you aint got no dedication, you won't get no education!

mesene

  • Member
  • **
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Saving our playstate
« Reply #3 on: Sat, Dec 17, 2011 »
Hi!
Im having the same problem and i cant find a solution.
I have a Main Level(state) a room wich have some doors wich moves you to another level(state), theese states are randomly generated at the begin also the main level.
when I move to another level the previous one is deleted.

I want to keep the state of the main Level, also the other levels so I could go between them.
So i have a group of states wich are generated at main level, I store that group on registry, when I go back I create a new main level and I copy the registry group to the new level, but i got an error about null objects. I think the problem its about pointers, when I change level the group of registry points to same that level.

I cant find the way right now, I think I'll be late for the ludumdare :(

thanks for the patience and effort to understand my English

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Saving our playstate
« Reply #4 on: Sun, Dec 18, 2011 »
I don't think you can "keep" a state. The approach I would use is to keep variables that is essential to recreate such altered state in an outside class, then when I need that previous state, I would recreate it and apply those "configs".

Just my 2 cents.
blog, twitter, Check out my award winning game, Rot Gut:

Alextfish

  • Active Member
  • ***
  • Posts: 174
  • Karma: +0/-0
    • View Profile
Re: Saving our playstate
« Reply #5 on: Mon, Dec 19, 2011 »
Yeah, if you're wanting to keep hold of the contents of a level, then States isn't the right idea to use. Just have one PlayState, which has perhaps an Array or FlxGroup of "Level"s, and one property called something like currentLevel.

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Saving our playstate
« Reply #6 on: Mon, Dec 19, 2011 »
Yeah, if you're wanting to keep hold of the contents of a level, then States isn't the right idea to use. Just have one PlayState, which has perhaps an Array or FlxGroup of "Level"s, and one property called something like currentLevel.

If you wanted to use what alex described here, you can use built-in "FlxG.levels" which is an array and doesn't get destroyed between state resets and FlxG.level which holds an integer on which you can store level number that you currently in. Or hack it and store whatever else you want in it.
blog, twitter, Check out my award winning game, Rot Gut:

John Hutchinson (Johntron247)

  • Commodore 256
  • Contributor
  • ****
  • Posts: 392
  • Karma: +1/-0
  • I can has lazerz?
    • View Profile
    • Level X Games
Re: Saving our playstate
« Reply #7 on: Mon, Dec 19, 2011 »
I think he's wanting to keep the progress of the current game while he goes into a minigame, so that the game can resume where exactly where it was left off when he's done.  Am I right?  If so, then the solution may not be as simple as any suggested so far, depending on exactly what his needs are.

Definitely do the Registry thing that photonstorm suggested and blogged about.  That is essential in getting everything able to "talk" to each other, and should be used in pretty much any game.  That, plus what test84 suggested about reseting the one FlxState may be enough to do the trick. 

However, if you really, REALLY, need to preserve the game, exactly as it was, for whatever reason, then there's a third, untested, solution.  It's a bit complex but I think it's pretty clever and you'd never have to create another FlxState OR reset the current one:

To preserve your FlxState and still have a menu, minigame, or whatever, without ever losing main-game progress then consider creating a "gamestate" variable, which controls program flow in your one FlxState's update (research "finite state machines").  Throw everything in you "main game" into a FlxGroup instead of adding it directly to the FlxState. Everything in your "menu" goes into another FlxGroup, and everything in a "minigame" goes into yet another FlxGroup.  Add all three of those to you FlxState.  In your FlxState's update, check the current state of the game and revive()/kill() those groups as appropriate for whatever state you're in.  This will obviously complicate your game architecture but it'll do what you want.  If you need more explaination about how/why that would work then let me know.
« Last Edit: Mon, Dec 19, 2011 by JohnTron247 »

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Saving our playstate
« Reply #8 on: Mon, Dec 19, 2011 »
To preserve your FlxState and still have a menu, minigame, or whatever, without ever losing main-game progress then consider creating a "gamestate" variable, which controls program flow in your one FlxState's update (research "finite state machines").  Throw everything in you "main game" into a FlxGroup instead of adding it directly to the FlxState. Everything in your "menu" goes into another FlxGroup, and everything in a "minigame" goes into yet another FlxGroup.  Add all three of those to you FlxState.  In your FlxState's update, check the current state of the game and revive()/kill() those groups as appropriate for whatever state you're in.  This will obviously complicate your game architecture but it'll do what you want.  If you need more explaination about how/why that would work then let me know.

Is it possible to have a copy of that in the registry and on comeback, reset the state from the registry?
blog, twitter, Check out my award winning game, Rot Gut:

John Hutchinson (Johntron247)

  • Commodore 256
  • Contributor
  • ****
  • Posts: 392
  • Karma: +1/-0
  • I can has lazerz?
    • View Profile
    • Level X Games
Re: Saving our playstate
« Reply #9 on: Mon, Dec 19, 2011 »
I don't think so.  I tried that before and couldn't get it to work at least.  I think when you reset or switch states it destroys it anyway.

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Saving our playstate
« Reply #10 on: Mon, Dec 19, 2011 »
I don't think so.  I tried that before and couldn't get it to work at least.  I think when you reset or switch states it destroys it anyway.

Yeah, that's why I said having a copy of the current FlxState, not just it's address in memory. It would help serialization later on too.
blog, twitter, Check out my award winning game, Rot Gut:

John Hutchinson (Johntron247)

  • Commodore 256
  • Contributor
  • ****
  • Posts: 392
  • Karma: +1/-0
  • I can has lazerz?
    • View Profile
    • Level X Games
Re: Saving our playstate
« Reply #11 on: Mon, Dec 19, 2011 »
Saving a copy instead of a reference to the FlxState would probably be more trouble than it's worth... too much exposure to error / oversight, and possibly too expensive in terms of memory as well.

Of course, if you went with my state machine + state groups idea then you could save a reference to the mainGameState, menuState, and miniGameState FlxGroups in the regestry, reset the FlxState, and then readd those groups - not sure why you really need to do that though.  It seems like all of that can be avoided by simplying killing/reviving the appropriate groups with the state machine.  That would effectively "pause" the appropriate "states" as well, since the updates/draws wouldn't be called on the disabled groups.

mesene

  • Member
  • **
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Saving our playstate
« Reply #12 on: Thu, Dec 22, 2011 »
Hi!  I'm sorry, I solved my problem and I didn't  remember come here.

first of all here is my entry of LudumDare 22 ;D
http://www.ludumdare.com/compo/ludum-dare-22/?action=preview&uid=6870

I think he's wanting to keep the progress of the current game while he goes into a minigame

@JhonTron247 you were right, this is what I wanted to do.
I'm ashamed of the way I solved this... I was hurry and had no time to program something to export all my data and reload later...  so I just overrided the function destroy() of my levels so the "stateController" would not delete them when I swap.
As the games where so light... they are never deleted, I know... this goes againt the laws of programming, I should be executed.
For my redemption I will try to correct the code in the next few days and show you my work.
thanks you.

John Hutchinson (Johntron247)

  • Commodore 256
  • Contributor
  • ****
  • Posts: 392
  • Karma: +1/-0
  • I can has lazerz?
    • View Profile
    • Level X Games
Re: Saving our playstate
« Reply #13 on: Thu, Dec 22, 2011 »
Don't feel bad man.  If it works, it works.  For what it's worth, I did the same thing - you'll have company when you go up on that chopping block.  ???