kingkirb |
Posted - May 13 2006 : 09:38:12 AM Ok ive just about finished making my game but i want to be able to add a feature to save it and re load it. how do i do this? if anyone knows PLZ email me at kingkirb@hotmail.co.uk |
Walrus |
Posted - May 19 2006 : 2:38:15 PM I thought that might be the problem, but I wanted to wait for a confirmation. And now you've solved the problem yourself.
The WriteLine method expects a string, but my code saves two numeric values. Therefore, these have to be converted into strings. That's what Str$ does; you give it a number(apparently, it accepts Booleans as well), it gives it back to you in the form of a string ("123", for example). But a string is already, well, a string, so Str$ doesn't accept it, which is what the "Type mismatch" message tells you.
It's the other way around with ReadLine. It gives you a string, but if you know that it is in fact a number, you use Val.
Actually, when possible, VB converts values into appropriate data types itself. So the following two lines are ok: txst.WriteLine 100 iX = txst.ReadLine (iX is an integer)
Personally, I prefer to convert the values explicitly.
|
kingkirb |
Posted - May 19 2006 : 08:38:12 AM ah ha fixed!! ll yuo do is
fname = txst.ReadLine when loading and
txst.WriteLine (fname) when writing and ignore the STR$ thingy. seems to work. Thanks again for your help guys. J(kingkirb) |
kingkirb |
Posted - May 19 2006 : 07:26:26 AM just had a thought is it cuase of the VAL where when it reads the line it is looking for a value and not letters.(but saying this i havnt encountered a problem with booleans and the document saves them as "true" or "false" |
kingkirb |
Posted - May 19 2006 : 07:23:55 AM Basicly ive practised using this code and ive started sending most of my variables. i But as soon as i send a string it says. "Type mismatch" and when i hit debug it highlights the line that sends the name of my player. I thought at first summate was wrong with the way i typed it but its fine so i tried another string that contains the name of the class of the player (e.g "Mage") and it does exactly the same. Though this only happens when im saving but then if i cant save it it wont load it either. thanks again J(kingkirb) |
Walrus |
Posted - May 19 2006 : 03:50:23 AM I'm not quite sure I understand what you mean. Could you describe the problem again or post the code that's causing it?
|
PW7962 |
Posted - May 18 2006 : 5:09:09 PM Well that doesn't seem right seeing how in his code he uses the Str$ function, which converts a variable to a string. I'm not sure how the FileSystemObject works but strings should be usable. |
kingkirb |
Posted - May 18 2006 : 3:35:13 PM actually summate aint right. this code doesnt want to accept any string values or leters of anykind. 1.why? 2. is there a way to make it accept letters? sorry i must be a pest but i cant figure out why. Thanks again J(kingkirb) |
kingkirb |
Posted - May 18 2006 : 3:11:26 PM thank you thank you thank you this is perfect!!!! Nope your instructions where perfect to. i like easy t follow ones. this is exactly what i wanted thank you very much again. J(kingkirb) Though ,although my problem is solved, seeing VB work this way does anyone know a function that saves every variable in the program automatically save typing out a lot of lines of code for each variable. cuase if the code existists it would save a lot of time :) thanks soooooo much agian walrus. |
Walrus |
Posted - May 18 2006 : 05:40:36 AM PW7962's method is good, but I find using VB's statements or objects easier than calling Windows API functions. Here is a very basic program that saves some info in a file, then loads it again at start-up. This is probably not the smartest method available, but it does the trick(hopefully for you too). This is for VB6, which is what I presume you're using.
This is what to do to get it running. 1.Start a new project. 2.Add an Image to the form and keep the default name Image1 3.Load a picture into the Image, preferably a small one 4.Add a menu to Form1 and call it mnuSave 5.Go to Project->References and add "Microsoft Scripting RunTime" 6.Paste this code into Form1 module:
Dim iX As Integer, iY As Integer Private Sub Form_Load() On Error GoTo NoFileYet Dim fso As FileSystemObject Set fso = New FileSystemObject Dim txst As TextStream Set txst = fso.OpenTextFile(App.Path & "\Save.txt", ForReading) iX = Val(txst.ReadLine) iY = Val(txst.ReadLine) NoFileYet: Me.PaintPicture Image1.Picture, iX, iY End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then Me.Cls Me.PaintPicture Image1.Picture, X, Y iX = X iY = Y End If Me.Caption = iX & ": " & iY End Sub Private Sub mnuSave_Click() Dim fso As FileSystemObject Set fso = New FileSystemObject Dim txst As TextStream Set txst = fso.CreateTextFile(App.Path & "\Save.txt", True, False) txst.WriteLine Str$(iX) txst.WriteLine Str$(iY) End Sub
When you run the app, you can drag the picture from Image1 around the form by holding down the left button. Place it somewhere, and click the menu. Exit the app. When you run the app again, the picture should be where you left it.
Once again, this is very simple, but hopefully it helps. Also, I apologize for giving such detailed(and possibly redundant) instructions for such a simple program, I just wanted to make sure it is understandable. |
kingkirb |
Posted - May 17 2006 : 11:26:17 AM Thanks very much for your help guys. Id gladly give you some of my gaem code but it is massive. As for the function of savin and loadin i think i understand basicly what your getting at but im not positive. ill try your code and c if i can get it to work. If i cant id probs drop you a line. Thanks again. J(kingkirb) |
PW7962 |
Posted - May 14 2006 : 8:18:04 PM Well if this in VB6 then an easy way to do this is using INI files, but for a little protection give the save files a different ending (maybe 3 initials of the game). Then you would save any information to the file that you would need for reloading later. Then when you need to load you just read the information from the file. To do this add this code into a module:
Public Declare Function WritePrivateProfileString& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal AppName$, ByVal KeyName$, ByVal keydefault$, ByVal FileName$) Public Declare Function GetPrivateProfileString& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal AppName$, ByVal KeyName$, ByVal keydefault$, ByVal ReturnedString$, ByVal RSSize&, ByVal FileName$) Public Sub WriteINI(INISection As String, INIKey As String, INIValue As String, INIFile As String) Call WritePrivateProfileString(INISection, INIKey, INIValue, INIFile) End Sub Public Function ReadINI(INISection As String, INIKey As String, INIFile As String) As String Dim StringBuffer As String Dim StringBufferSize As Long StringBuffer = Space$(255) StringBufferSize = Len(StringBuffer) StringBufferSize = GetPrivateProfileString(INISection, INIKey, "", StringBuffer, StringBufferSize, INIFile) If StringBufferSize > 0 Then ReadINI = Left$(StringBuffer, StringBufferSize) Else ReadINI = "" End If End Function Function FileExist(ByVal FileName As String) As Boolean If Dir(App.Path & "\" & FileName) = "" Then FileExist = False Else FileExist = True End If End Function
Then to use this code:
call writeini("SAVE1", "Name", playername.txt, app.path & "\saved.ini") dim playername as string playername = readini("SAVE1", "Name", app.path & "\saved.ini")
Now the location of the ini file & the name (4th parameter in writeini and 3rd in readini) will change depending on the location and name of the file. duh. The first parameter of each is the Main Section in the file. Main sections would look like this: "[SomeSectionTitle]" and the brackets don't count in the parameters. The second parameter is the Key, the specific thing you are looking for under the Section, which would look like this: "Key = Info". In writeini it would put information there and in readini it returns that information. This probably isn't the best way of doing a save/load but it's pretty easy. (If you still don't get this then I can make a dummy program) |
Walrus |
Posted - May 13 2006 : 5:54:31 PM Welcome to the forum.
It would be very helpful if you let us have a look at the code. If you don't want to do this, at least tell us a little bit about the game and which version of VB you are using, so we can give you more general instructions. |
|
|