Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sat Dec 16, 2017 3:47 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Saving a game.
PostPosted: Wed Feb 01, 2012 1:00 pm 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving a game.
PostPosted: Wed Feb 01, 2012 1:39 pm 
Offline
User avatar

Joined: Fri Sep 30, 2005 11:48 am
Posts: 3554
Location: Lenexa, KS
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.

_________________
Bob K.

A blog about being a Real Studio/Xojo developer at http://www.bkeeneybriefs.com


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving a game.
PostPosted: Wed Feb 01, 2012 2:35 pm 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving a game.
PostPosted: Wed Feb 01, 2012 2:39 pm 
Offline

Joined: Wed May 20, 2009 4:43 pm
Posts: 958
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.

_________________
RB 2009r4 Windows XP


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving a game.
PostPosted: Wed Feb 01, 2012 3:29 pm 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
It's been replaced by X database.

Look way more complicated than what I need.

Anybody else have a small example?


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving a game.
PostPosted: Thu Feb 02, 2012 10:00 am 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving a game.
PostPosted: Thu Feb 02, 2012 10:49 am 
Offline

Joined: Wed May 20, 2009 4:43 pm
Posts: 958
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.

_________________
RB 2009r4 Windows XP


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving a game.
PostPosted: Thu Feb 02, 2012 2:19 pm 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving a game.
PostPosted: Thu Feb 02, 2012 2:24 pm 
Offline

Joined: Wed May 20, 2009 4:43 pm
Posts: 958
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.

_________________
RB 2009r4 Windows XP


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving a game.
PostPosted: Thu Feb 02, 2012 3:43 pm 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving a game.
PostPosted: Thu Feb 02, 2012 3:47 pm 
Offline

Joined: Wed May 20, 2009 4:43 pm
Posts: 958
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

_________________
RB 2009r4 Windows XP


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving a game.
PostPosted: Thu Feb 02, 2012 4:13 pm 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Saving a game.
PostPosted: Fri Feb 03, 2012 9:44 am 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group