Real Software Forums
http://forums.realsoftware.com/

Saving a game.
http://forums.realsoftware.com/viewtopic.php?f=4&t=42486
Page 1 of 1

Author:  nilesmitchell [ Wed Feb 01, 2012 1:00 pm ]
Post subject:  Saving a game.

Any one have a template for creating a file that saves the game's state?

So, if the user wants to save the game, I'd like to write out a file that saves the current state of all the properties, and the current active window. This part doesn't seem to0 hard. Where I'm lost is how to load them again.

Author:  Bob Keeney [ Wed Feb 01, 2012 1:39 pm ]
Post subject:  Re: Saving a game.

Depends on how you want to save it. You can save as a pure text file, an xml file, or heck, even use an SQLite database. Find which one you want, or most comfortable with, and then implement the write and then the read functions.

Author:  nilesmitchell [ Wed Feb 01, 2012 2:35 pm ]
Post subject:  Re: Saving a game.

Text file is fine.

I'm just looking for help in saving all the properties, and the active window, and then being able to load them again.

Author:  markwalsh [ Wed Feb 01, 2012 2:39 pm ]
Post subject:  Re: Saving a game.

nilesmitchell wrote:
Text file is fine.

I'm just looking for help in saving all the properties, and the active window, and then being able to load them again.

Therre are several solutions for saving preferences in the boards; I use XPrefs which should accomplish what you are looking for, but you can do a search for others.

Author:  nilesmitchell [ Wed Feb 01, 2012 3:29 pm ]
Post subject:  Re: Saving a game.

It's been replaced by X database.

Look way more complicated than what I need.

Anybody else have a small example?

Author:  nilesmitchell [ Thu Feb 02, 2012 10:00 am ]
Post subject:  Re: Saving a game.

Ok, so I've got the basic code down to save a game to a text file. It writes down all the properties (inventory) and the window name (room).

// Gathers Inventory
dim pistoli as string
dim shotguni as string
dim crossbowi as string
dim inv as string


if pistol = 1 then
pistoli = "Pistol,"
else
pistoli=""
end

if shotgun = 1 then
shotguni="Shotgun,"
else
shotguni=""
end

if crossbow = 1 then
crossbowi = "Crossbow,"
else
crossbowi = ""
end


inv = pistoli + shotguni + crossbowi


// Gets Window Name (Current Room)
dim w as Introspection.TypeInfo
dim wname as string

w = Introspection.GetType(self)

wname = w.Name




// Outputs above information into a text file.
Dim t as TextOutputStream
dim n as FolderItem = SpecialFolder.ApplicationData.child("Zombie Game")
n.createasfolder
Dim f as FolderItem = SpecialFolder.ApplicationData.child("Zombie Game").child("Saved Game.txt")
t=TextOutputStream.Create(f)
t.WriteLine(inv)
t.WriteLine(wname)
t.Close




The question I have is, how do I load this file to set the properties and the window from a fresh launch?

Author:  markwalsh [ Thu Feb 02, 2012 10:49 am ]
Post subject:  Re: Saving a game.

nilesmitchell wrote:
Ok, so I've got the basic code down to save a game to a text file. It writes down all the properties (inventory) and the window name (room).

// Gathers Inventory
dim pistoli as string
dim shotguni as string
dim crossbowi as string
dim inv as string


if pistol = 1 then
pistoli = "Pistol,"
else
pistoli=""
end

if shotgun = 1 then
shotguni="Shotgun,"
else
shotguni=""
end

if crossbow = 1 then
crossbowi = "Crossbow,"
else
crossbowi = ""
end


inv = pistoli + shotguni + crossbowi


// Gets Window Name (Current Room)
dim w as Introspection.TypeInfo
dim wname as string

w = Introspection.GetType(self)

wname = w.Name




// Outputs above information into a text file.
Dim t as TextOutputStream
dim n as FolderItem = SpecialFolder.ApplicationData.child("Zombie Game")
n.createasfolder
Dim f as FolderItem = SpecialFolder.ApplicationData.child("Zombie Game").child("Saved Game.txt")
t=TextOutputStream.Create(f)
t.WriteLine(inv)
t.WriteLine(wname)
t.Close




The question I have is, how do I load this file to set the properties and the window from a fresh launch?

Do the reverse of what you have done to save the file. Read the text file using TextInputStream (You can either read each line separately, or readAll and parse the results yourself), then split the first line on your delimiter (',') to get each value saved.

Author:  nilesmitchell [ Thu Feb 02, 2012 2:19 pm ]
Post subject:  Re: Saving a game.

I'm getting closer. I took out the commas and list each item on it's own line. I have this so far...

dim invsaved as FolderItem = SpecialFolder.ApplicationData.child("Zombie Game").child("Saved Game.txt")

Dim textInput As TextInputStream

Dim invsavedlist As String

textInput =TextInputStream.Open(invsaved)

invsavedlist=textInput.Readall



So how do I do some thing like.......

if invsavedlist contains "Pistol" then
pistol = 1
else
pistol = 0
end


because that doesn't work! LOL

Author:  markwalsh [ Thu Feb 02, 2012 2:24 pm ]
Post subject:  Re: Saving a game.

nilesmitchell wrote:
I'm getting closer. I took out the commas and list each item on it's own line. I have this so far...

dim invsaved as FolderItem = SpecialFolder.ApplicationData.child("Zombie Game").child("Saved Game.txt")

Dim textInput As TextInputStream

Dim invsavedlist As String

textInput =TextInputStream.Open(invsaved)

invsavedlist=textInput.Readall



So how do I do some thing like.......

if invsavedlist contains "Pistol" then
pistol = 1
else
pistol = 0
end


because that doesn't work! LOL


if insavedlist.instr("Pistol") > 0 then


Note that if you put each item on it's own line, you need to make sure that each item either has a blank line if it is not present, or you will need to make sure you know how to parse out any other information (such as your window name which could be on a different line depending on how many items exist)

It would be best to structure your text file a little more consistently - i.e - you could use a named pair for each line, then parse each line for the name and value

Quote:
Pistol:1
Crossbow:0
Shotgun:1
WindowName:Entry


This way, if you need to add/remove values in the future, you are less likely to run into problems.

Writing to an XML file would also be an option.

Author:  nilesmitchell [ Thu Feb 02, 2012 3:43 pm ]
Post subject:  Re: Saving a game.

Awesome, just awesome!

I guess there is just one last thing!

I've got the name of the room as the string roomsavedlist. Looks great in msgbox, etc...

I can't seem to open the window using that variable.

I've tried:

roomsavedlist.open
Window.open(roomsavedlist)

Among many other combinations.....

I really though I could just come back here and say thanks!

Any ideas?

Author:  markwalsh [ Thu Feb 02, 2012 3:47 pm ]
Post subject:  Re: Saving a game.

nilesmitchell wrote:
Awesome, just awesome!

I guess there is just one last thing!

I've got the name of the room as the string roomsavedlist. Looks great in msgbox, etc...

I can't seem to open the window using that variable.

I've tried:

roomsavedlist.open
Window.open(roomsavedlist)

Among many other combinations.....

I really though I could just come back here and say thanks!

Any ideas?

You're probably going to have to check for each value separately. You can't open a class via it's name in a variable (at least, I am pretty sure that's the case). Something like this:

Select case windowName
case "Window1"
Window1.Open
case "AnotherWindow"
AnotherWindow.Open
Case "YetAnotherWindow"
YetAnotherWindow.Open

'... repeat for each window

Case Else
' Open default window?
End Select

Author:  nilesmitchell [ Thu Feb 02, 2012 4:13 pm ]
Post subject:  Re: Saving a game.

Not following you at all on that one.....

There has to be a way to translate a one word string into the window name. I can't imagine something so simple wouldn't work.

Author:  nilesmitchell [ Fri Feb 03, 2012 9:44 am ]
Post subject:  Re: Saving a game.

Thanks Markwalsh. Got it solved in another post. You were on the right track, I just had to have someone hit me over the head with the sledgehammer!

viewtopic.php?f=1&t=42517

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/