-
September 3rd, 2009, 06:37 AM
#16
Re: Save game/Load game
Save code:
Dim namefile As String
namefile = Text3
If Text3.Text = "" Then
MsgBox "Fill in a Username!"
Else
Open "D:\Projects\GR_Manager\Data\" + namefile + ".txt" For Output As #1
Print #1, Label5 + ";" + Label1 + ";";
Print #1, Label10 + ";" + Label11 + ";";
Print #1, Label38 + ";" + Label39 + ";";
Print #1, Label31 + ";" + Label33 + ";";
Print #1, Label32 + ";" + Label34 + ";";
Print #1, Label42 + ";" + Label43 + ";";
Print #1, Label19 + ";" + Label24 + ";";
Print #1, Label17 + ";" + Label25 + ";";
Print #1, Label16 + ";" + Label26 + ";";
Print #1, Label20 + ";" + Label27 + ";";
Print #1, Label21 + ";" + Label28 + ";";
Print #1, Label22 + ";" + Label29 + ";";
Print #1, Label23 + ";" + Label30 + ";";
Close #1
End If
Load code:
Dim a$, b$()
Dim namefile As String
namefile = Text2
Open App.Path + "\Data\" + namefile + ".txt" For Input As #1
Line Input #1, a$
b = Split(a$, ";")
Label5.Caption = b(0)
Label1.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label10.Caption = b(0)
Label11.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label38.Caption = b(0)
Label39.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label31.Caption = b(0)
Label33.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label32.Caption = b(0)
Label34.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label42.Caption = b(0)
Label43.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label19.Caption = b(0)
Label24.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label17.Caption = b(0)
Label25.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label16.Caption = b(0)
Label26.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label20.Caption = b(0)
Label27.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label21.Caption = b(0)
Label28.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label22.Caption = b(0)
Label29.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label23.Caption = b(0)
Label30.Caption = b(1)
Line Input #1, a$
b = Split(a$, ";")
Label10.Caption = b(0)
Label11.Caption = b(1)
-
September 3rd, 2009, 08:16 AM
#17
Re: Save game/Load game
Originally Posted by Equx
I'm noob on codes and VB atm, trying to get better and better.
Seeing the fact that you are a noob, and trying to learn, I'll give this advice ( not really applicable to your situation, but still very important )
Names like Label1, Label2, Label3 etc. is extremely difficult to remember. Why do I say that ¿
Imagine how difficult it is to remember each label's caption, for example :
On Label 62 you might have text about a certain topic, 2 months from now, how would you remember what Label62 represented ¿
Give them decent names, each should have a name which should easily identify its purpose
Another thing here, is that a Form can only hold 255 controls, and is only so big. All your controls on the forms should be nicely organised, you must try to find ways to save form space, and design proper professional forms
-
September 3rd, 2009, 08:26 AM
#18
Re: Save game/Load game
Originally Posted by HanneSThEGreaT
Seeing the fact that you are a noob, and trying to learn, I'll give this advice ( not really applicable to your situation, but still very important )
Names like Label1, Label2, Label3 etc. is extremely difficult to remember. Why do I say that ¿
Imagine how difficult it is to remember each label's caption, for example :
On Label 62 you might have text about a certain topic, 2 months from now, how would you remember what Label62 represented ¿
Give them decent names, each should have a name which should easily identify its purpose
Another thing here, is that a Form can only hold 255 controls, and is only so big. All your controls on the forms should be nicely organised, you must try to find ways to save form space, and design proper professional forms
OK thanx for you advice.
This is my first real project so I need advices.
The labels are in an special order. I know exactly what label1 is and so on...
But good advice when I make better programs
-
September 3rd, 2009, 10:53 AM
#19
Re: Save game/Load game
I doubt, that the aboce code works.
You do Print #1, Label5 + ";" + Label1 + ";";
where the trailing ; does not print a complete line.
For the above reading code to work you must omit the trailing semicolon
The proper print statement should look like that:
Print #1, Label5 + ";" + Label1
All print statements should look like that. With this you get one line printed for each pair of labels.
Only then you can read them back in line by line with the Line Input statement.
-
September 3rd, 2009, 11:35 AM
#20
Re: Save game/Load game
WOF is correct, the sample where I showed the use of the semi colon would work with the way you were reading the file before but not the way you are reading it in the most recent post.
Remember the ; at the end of the print statement tells it not to send the new line characters
-
September 3rd, 2009, 01:21 PM
#21
Re: Save game/Load game
yes but I still get the error '62: Input past end of file
-
September 3rd, 2009, 01:39 PM
#22
Re: Save game/Load game
That's the reason why.
Write your Print statements like I have shown you and your reading routine would work.
Also, examine your file. It should contain individual lines which look like:
nameofvalue;10000
It should be one pair in each line.
Please show us the file if you are in doubt.
-
September 3rd, 2009, 02:17 PM
#23
Re: Save game/Load game
Originally Posted by Equx
yes but I still get the error '62: Input past end of file
In your most recent code posted you are writing 1 line but reading more than one line therefore you get input past end when you try to read line 2 as there is no line 2 generated by the code posted.
To use the read routine in your last post yuou need to remove the trailing semicolons from each print statement. You also should count the print # vs line input statements to make sure you have the same number of Print as Input otherwise you will be reading past end.
-
September 3rd, 2009, 11:37 PM
#24
Re: Save game/Load game
Originally Posted by WoF
That's the reason why.
Write your Print statements like I have shown you and your reading routine would work.
Also, examine your file. It should contain individual lines which look like:
nameofvalue;10000
It should be one pair in each line.
Please show us the file if you are in doubt.
Ok I have done that now.
nameofvalue;10000 ?
Can you explain exactly how it shall be?
-
September 4th, 2009, 12:31 AM
#25
Re: Save game/Load game
Delete the last + ";"; in each line, so you get one ; between the parms, and one line of two parms
Code:
Open "D:\Projects\GR_Manager\Data\" + namefile + ".txt" For Output As #1
Print #1, Label5 + ";" + Label1 + ";";
Print #1, Label10 + ";" + Label11 + ";";
Print #1, Label38 + ";" + Label39 + ";";
Print #1, Label31 + ";" + Label33 + ";";
Print #1, Label32 + ";" + Label34 + ";";
Print #1, Label42 + ";" + Label43 + ";";
Print #1, Label19 + ";" + Label24 + ";";
Print #1, Label17 + ";" + Label25 + ";";
Print #1, Label16 + ";" + Label26 + ";";
Print #1, Label20 + ";" + Label27 + ";";
Print #1, Label21 + ";" + Label28 + ";";
Print #1, Label22 + ";" + Label29 + ";";
Print #1, Label23 + ";" + Label30 + ";";
-
September 4th, 2009, 06:24 AM
#26
Re: Save game/Load game
Exactly.
That's how a Print statement should look like:
Print #1, Label5 + ";" + Label1
Change all yhe print statements like that, then write the file and open it with a text editor.
Look at the lines it contains.
Then you will understand what I wanted to say with nameofvalue;10000 because the lines will all look similar.
I presume that in the first label is always some description and in the second one the current value,
like Label5 says "Points" and Label1 says "10000", reflecting the actual points count.
So your line 1 in the file would be "Points;10000"
Last edited by WoF; September 4th, 2009 at 06:27 AM.
-
September 4th, 2009, 07:01 AM
#27
Re: Save game/Load game
Originally Posted by WoF
Exactly.
That's how a Print statement should look like:
Print #1, Label5 + ";" + Label1
Change all yhe print statements like that, then write the file and open it with a text editor.
Look at the lines it contains.
Then you will understand what I wanted to say with nameofvalue;10000 because the lines will all look similar.
I presume that in the first label is always some description and in the second one the current value,
like Label5 says "Points" and Label1 says "10000", reflecting the actual points count.
So your line 1 in the file would be "Points;10000"
Ok.
My print code looks like this:
....
Print #1, Label5 + ";" + Label1
....
And when I print and open the printed txt file it looks like this:
Current Money:;535000
Current Loan:;12000
.....
-
September 4th, 2009, 09:07 AM
#28
Re: Save game/Load game
Thats what I expected.
Now you try your Load code with the blocks:
Code:
Line Input #1, a$
b = Split(a$, ";")
Label5.Caption = b(0)
Label1.Caption = b(1)
Watch that there are only as many reads as there are lines in the file.
Then it should work as expected without reading past end of file.
-
September 4th, 2009, 11:35 AM
#29
Re: Save game/Load game
Originally Posted by WoF
Thats what I expected.
Now you try your Load code with the blocks:
Code:
Line Input #1, a$
b = Split(a$, ";")
Label5.Caption = b(0)
Label1.Caption = b(1)
Watch that there are only as many reads as there are lines in the file.
Then it should work as expected without reading past end of file.
Thank you guys!!!
What would I have done without you?
Once more, thank you all!
But one question more:
I want to open an file by pressing a button.
The location of the file is for example: D:\Projects\GR_Manager\TrackEditor.exe
And the code?
Last edited by Equx; September 4th, 2009 at 11:47 AM.
-
September 4th, 2009, 04:52 PM
#30
Re: Save game/Load game
You can Shell() a program to open it.
Code:
Shell("C:\temp\notepad.exe")
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
|