|
-
Apr 27th, 2011, 11:29 PM
#1
Thread Starter
Lively Member
[RESOLVED] Save progress in Text Adventure?
I've set up buttons with the Save Dialog and Open Dialog in Visual Basic 2010, but It doesn't really do anything when I save, and there's nothing there when I try to open. I didn't actually put any code in the dialogs or anything. Basically, I just want to beable to have an option in my VB2010 text adventure game for the user to save his/her progress. Please help!
-
Apr 28th, 2011, 02:13 AM
#2
Hyperactive Member
Re: Save progress in Text Adventure?
im also new at this programming language so beware that my advices may be just senseless or wrong, but what i can think of right now is that you should save your "progress" into a .txt file, namely everytime u type a message in a text for example
"You are now in front of the mill, at your left side there is a wheel, at the right side there is a key laying on the floor and behind there is a dark forest, which way do u want to go?
A:Left
B:Right
C:Front
D: Back
imagine this example, in the program itself, everytime u change the text for the player try saving it as a variable, example like this:
in public class you add a variable:
Public progress as integer = 0
Now an example
progress = 1
Richtextbox1.text = "welcome to a text game would u like to read the tutorial?"
progress = 10
Richtextbox1.text = "You are now in front of the mill, at your left side there is a wheel, at the right side there is a key laying on the floor and behind there is a dark forest, which way do u want to go?
A:Left
B:Right
C:Front
D: Back
and imagine the user saves the game, once he does that you write the variable value into a .txt file,
and when you click load if progress = 10 then
Richtextbox1.text = "You are now in front of the mill, at your left side there is a wheel, at the right side there is a key laying on the floor and behind there is a dark forest, which way do u want to go?
A:Left
B:Right
C:Front
D: Back
end if
i suggest that you make richtextboxes text as functions, so you wont have to do my mistake of copying and pasting the same code over and over again. Invoke it with 1 word only,
if there are more aspects of the game like, hp, gold, weapons, you could also save those as variable and load them up the same way.
-
Apr 28th, 2011, 02:34 AM
#3
Re: Save progress in Text Adventure?
The OpenFileDialog and SaveFileDialog components don't actually open and save files. How could they? There is basically an unlimited number of file formats out there so they can't know about them all. They simply provide an interface for the user to select a location to open from or save to. It's up to you to provide the appropriate code to read or write the file the user selects in the appropriate format.
http://www.google.com.au/search?q=re...ient=firefox-a
-
Apr 28th, 2011, 06:41 AM
#4
Hyperactive Member
Re: Save progress in Text Adventure?
oh and here it is what u need to properly save a file and to read a file:
Save:
Dim filesave As System.IO.StreamWriter
Example:
filesave = My.Computer.FileSystem.OpenTextFileWriter("../../../AdventureProgress.txt", False)
filesave.Write(currentprogress) 'this can be anything you want, a variable, a string, an array, whatever....
filesave.Close() 'everytime you open and save a file you always need to close it because if u do not there will be issues while trying to save to a file which is in use.
Load:
Dim fileload as System.IO.StreamReader
Example:
fileload = My.Computer.FileSystem.OpenTextFileReader(""../../../AdventureProgress.txt")
Dim progressload As String = fileload.ReadToEnd 'This is used to save the details from the adventure progress into a string, you should try with an array instead.
fileload.Close()
Note1: if you want to check for an existent file here is the code for it
if My.Computer.FileSystem.FileExists("../../../AdventureProgress.txt") Then
'Execute your code here
End if
Note2: when you save to a file, it is irrelevant if the file exists or not, it will automatically create it.
-
Apr 28th, 2011, 04:24 PM
#5
Thread Starter
Lively Member
Re: Save progress in Text Adventure?
Appreciate the answer, Legendary agent. This is pretty much exactly what I'm looking for - wanting to save variables.
I must be doing something wrong though, because while I'm not getting any errors my open and save buttons don't appear to do anything.
This is what I've got:
Here's my open button:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileload As System.IO.StreamReader
fileload = My.Computer.FileSystem.OpenTextFileReader("../../../AdventureProgress.txt")
Dim progressload As String = fileload.ReadToEnd 'This is used to save the details from the adventure progress into a string, you should try with an array instead.
fileload.Close()
End Sub
And here's my save button:
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim filesave As System.IO.StreamWriter
filesave = My.Computer.FileSystem.OpenTextFileWriter("../../../AdventureProgress.txt", False)
filesave.Write(doonce, Clock, HungerBar, SleepBar, XpBar, Intl, Chr, Spd, Stl, Dex, HealthBar) 'this can be anything you want, a variable, a string, an array, whatever....
filesave.Close() 'everytime you open and save a file you always need to close it because if u do not there will be issues while trying to save to a file which is in use.
End Sub
-
Apr 28th, 2011, 04:54 PM
#6
Hyperactive Member
Re: Save progress in Text Adventure?
Ah ic, well you need to split them before you add them to the file, for example lets use the ' symbol to split the variables between each others
Modified load:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileload As System.IO.StreamReader
fileload = My.Computer.FileSystem.OpenTextFileReader("../../../AdventureProgress.txt")
Dim progressload As String = fileload.ReadToEnd
fileload.Close()
Dim datasplit As String() = progressload.Split(New Char() {"'"c})
doonce = datasplit(1)
Clock = datasplit(2)
HungerBar = datasplit(3)
SleepBar = datasplit(4)
Xpbar = datasplit(5)
Intl = datasplit(6)
Chr = datasplit(7)
Spd = datasplit(8)
Stl = datasplit(9)
Dex = datasplit(10)
HealthBar = datasplit(11)
End Sub
Now to save:
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim filesave As System.IO.StreamWriter
filesave = My.Computer.FileSystem.OpenTextFileWriter("../../../AdventureProgress.txt", False)
filesave.Write("'" & doonce & "'" & Clock & "'" & HungerBar & "'" & SleepBar & "'" & XpBar & "'" & Intl & "'" & Chr & "'" & Spd & "'" & Stl & "'" & Dex & "'" & HealthBar)
filesave.Close()
End Sub
[/QUOTE]
Last edited by Legendary_Agent; Apr 28th, 2011 at 05:02 PM.
-
Apr 28th, 2011, 05:14 PM
#7
Hyperactive Member
Re: Save progress in Text Adventure?
Hi Lucien,
It looks like your trying to save several variables into your text file. There is nothing wrong with this, but it makes things a bit more complicated when it comes to loading your file back into your variables.
You could save each of your variables into an application setting instead.
In Visual Studio click: 'Projects' --> 'YourApplicationName Properties...'. On the properties Menu click 'Settings'. You can add a Setting for each Variable you want to save and give it the same data type.
Heres a quick example. I added a setting called "HungerBar" and set its type to Integer. This is the code that would save/load your variable to this setting:
VB Code:
Dim HungerBar As Integer = 0 'Your Global Variable
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
HungerBar += 100 'This is just to show you that any changes are being saved
My.Settings.HungerBar = HungerBar 'Save your variable into your setting
End Sub
Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click
HungerBar = My.Settings.HungerBar 'Load your hunger setting back into its variable
MsgBox(HungerBar) 'This is the value of the loaded setting
End Sub
-
Apr 28th, 2011, 05:22 PM
#8
Thread Starter
Lively Member
Re: Save progress in Text Adventure?
Works like a charm! I really appreciate it! 
I left out a few details about the variables I was wanting to save, so a few changes were necessary. Going to go ahead and post them in case the information is of use to anyone:
Load:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileload As System.IO.StreamReader
fileload = My.Computer.FileSystem.OpenTextFileReader("../../../AdventureProgress.txt")
Dim progressload As String = fileload.ReadToEnd
fileload.Close()
Dim datasplit As String() = progressload.Split(New Char() {"'"c})
doonce = datasplit(1)
XpBar.Value = datasplit(2)
HungerBar.Value = datasplit(3)
SleepBar.Value = datasplit(4)
HealthBar.Value = datasplit(5)
Intl.Text = datasplit(6)
Chr.Text = datasplit(7)
Spd.Text = datasplit(8)
Stl.Text = datasplit(9)
Dex.Text = datasplit(10)
Clockweek = datasplit(11)
Clocksec = datasplit(12)
Clockmin = datasplit(13)
Clockhour = datasplit(14)
End Sub
Save:
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim filesave As System.IO.StreamWriter
filesave = My.Computer.FileSystem.OpenTextFileWriter("../../../AdventureProgress.txt", False)
filesave.Write("'" & doonce & "'" & XpBar.Value & "'" & HungerBar.Value & "'" & SleepBar.Value & "'" & HealthBar.Value & "'" & Intl.Text & "'" & Chr.Text & "'" & Spd.Text & "'" & Stl.Text & "'" & Dex.Text & "'" & Clockweek & "'" & Clocksec & "'" & Clockmin & "'" & Clockhour & "'")
filesave.Close()
End Sub
-
Apr 28th, 2011, 05:25 PM
#9
Thread Starter
Lively Member
Re: Save progress in Text Adventure?
 Originally Posted by jay20aiii
Hi Lucien,
It looks like your trying to save several variables into your text file. There is nothing wrong with this, but it makes things a bit more complicated when it comes to loading your file back into your variables.
You could save each of your variables into an application setting instead.
In Visual Studio click: 'Projects' --> 'YourApplicationName Properties...'. On the properties Menu click 'Settings'. You can add a Setting for each Variable you want to save and give it the same data type.
Heres a quick example. I added a setting called "HungerBar" and set its type to Integer. This is the code that would save/load your variable to this setting:
VB Code:
Dim HungerBar As Integer = 0 'Your Global Variable
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
HungerBar += 100 'This is just to show you that any changes are being saved
My.Settings.HungerBar = HungerBar 'Save your variable into your setting
End Sub
Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click
HungerBar = My.Settings.HungerBar 'Load your hunger setting back into its variable
MsgBox(HungerBar) 'This is the value of the loaded setting
End Sub
This looks promising as well, I think I'll give it a try. Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|