SaveState Function
Monkey Forums/Monkey Programming/SaveState Function
| ||
Hello to all I am using Monkey since a few months and I'm very comfortable with the language, but there are some things i don't understand yet. In our actual project, we are using save states to save the player characteristics, but i only know how to save one variable with "SaveState" Function. My question is, how can i save more than one variable using the SaveState Function? |
| ||
Save everything into one string where you place a special character between each variable. Then when you load it, split the string you get with that special character you have choosen and read the aray of stirngs back into your variables. Attention, don't make your string bigger than 1024 byte. |
| ||
Why the 1024-byte limit? |
| ||
Never heard of it too... my savestate has a few Kbs, so this 1024 string limit doesn't make much sense. |
| ||
Sorry, not byte. I ment 1 MB. That is what I read somewhere. But hey, maybe I am wrong. |
| ||
Thanks for your quick reply. :) So i have to parse the array to extract the values, is'n it? There is any example in the forums? |
| ||
Yes, my understanding is that the maximum length of a saved string is platform dependent, but some platforms may be as low as 1 MB. I was afraid you meant there was smoothing I should know about slicing strings! [Personally I don't slice the string into substrings, I just serialise all saved data sequentially. Slicing at a certain character means your random data can't use that character, which is not a good thing.] |
| ||
Oniric, there are various ways of converting all data into strings which can then be concatenated (in practice you will use string buffers for large files) into a single string. I think diddy has some serialisation (saving and loading) code if you want to look at that. |
| ||
Oniric you will have to parse it to extract the values. How you do it depends on what you want to store. |
| ||
Thanks a lot Dawlane, the "Split" Method is very usefull to Parse data! Only a Question... why "step 2" in this loop?: For Local i:Int = 0 To(parse.Length() -2) Step 2 |
| ||
Thanks a lot Dawlane, the "Split" Method is very usefull to Parse data! Only a Question about the code... why "step 2" in this loop? i made a test using "Step 1" and some "NaN" texts appears: For Local i:Int = 0 To(parse.Length() -2) Step 2 |
| ||
Only a Question about the code... why "step 2" in this loop? i made a test using "Step 1" and some "NaN" texts appears: When the string is split you have 2 types of data in the parse array both of which will still be strings.As we're looping through 'parse' array we need to get the first item which is the name (first index 0) and the second which is the score (first index + 1). This would mean that the next hi score would be the third index (first index + 2) so to get the next hi score with name you have to iterate parse in steps of 2 to go through all the data. I could have written For Local i:Int = 0 To(parse.Length() -2) Step 2as For Local i:Int = 0 To(parse.Length() -1) Step 2 and it would still work as the last item in the array would never be reached. For an experiment try and change the for loop into a while/wend and a repeat/until. This bit of code int(parse[i + 1]) is just to let the compiler and anyone reading the code know that we are enforcing the numerical string in to the data type that we want. It shouldn't be necessary if your using Strict mode to do this for default data types. It's also the one that would throw the error when the parse array is iterated in steps of 1 as you would be trying to read past the length of the array. Edit: I've updated the code in post #9 to explain better what I did with a little showing of using string slicing. |
| ||
dawlane, what the dots in the Array index does? str_display = str_zeros[ .. (str_zeros.Length - str_hi.Length)] + str_hi |
| ||
http://www.monkey-x.com/docs/html/Programming_Language%20reference.html Like strings, arrays can also be sliced. The syntax for slicing an array is: ArrayExpression [ StartExpression .. EndExpression ]. Slicing an array returns 'sub array' of ArrayExpression starting at index StartExpression and ending at index EndExpression. Both StartExpression and EndExpression are optional. If StartExpression is omitted, it defaults to 0. If EndExpression is omitted, it defaults to the length of the array. StartExpression and EndExpression can also be negative, in which case they refer to offsets from the end of the array. |
| ||
You know I can't remember posting that. @APC; Monkey doesn't have the traditional string functions that you would find in the standard BASIC dialect and a few other languages. As strings in Monkey are basically an array of characters. The double period (..) is use to slice string arrays. Any number before the double period is the start character and any number after would be the end character. A small example of slicing and some of the String Class functions Function Main() Local myVar:String="abcdefgh" Local myArray:Int[]=[65,98,67,100,69,102,71,104] ' AbCdEfGh Local myToArray:Int[]="MONKEY-X".ToChars() ' Convert a string to an array Print "myVar is 'abcdefgh'" Print "myArray is [65,98,67,100,69,102,71,104] ' AbCdEfGh" Print "myToArray is MONKEY-X" Print "String are zero index arrays [0=a,1=b,etc]" Print "Print the fourth letter ('d') using String.FromChar(myVar[3]) this would be equivalent to Chr$(numeric value) -> " + String.FromChar(myVar[3]) Print "Print the ascii character code for the fourth letter ('d') with myVar[3] this would be equivalent the Asc(character_at_variable_index) -> " + myVar[3] Print String.FromChar(34)+"A"+String.FromChar(34)+ "[0] would be the same as doing Asc(" + String.FromChar(34) + "A" + String.FromChar(34) + ") -> " + "A"[0] Print "Print the letters 'abcde' using what would be the equivalent of Left(myVar, length) with myVar[..5] -> " + myVar[..5] Print "Print the middle letters 'def' using what would be the equivalent of Mid(myVar, start, length) with myVar[3..((myVar.Length+1) - 3)] -> " + myVar[3..((myVar.Length+1) - 3)] Print "Print the last letters 'fgh' using what would be the equivalent of Right(myVar, length from right) with myVar[((myVar.Length) - 3)..] -> " + myVar[((myVar.Length) - 3)..] Print "Print the letters 'defgh'. Some languages would use Mid(myVar,start) with myVar[3..] -> " + myVar[3..] Print "Convert an array of integer character codes to a string with String.FromChars(myArray) -> " + String.FromChars(myArray) Print "'MONKEY-X' that was converted to an array" For Local i:Int = 0 To myToArray.Length - 1 Print "Index = " + i + ": code: " + myToArray[i] + " -> " + String.FromChar(myToArray[i]) Next End Function |