? Save and load variables? (Page 1) ● SmileBASIC Source Forums

Sign In

Register
*Usernames are case-sensitive
Forgot my password

Save and load variables?

  • #1 ✎ 24 Crazyblob Beginner Programmer I'm just starting out! I'm still trying to grasp the basics. Programming Strength Video Games I like to play video games! Hobbies Minecraft Is Awesome! I love Minecraft! Express Yourself Hey, I've tried looking around the forum but couldn't really find this question. How would I be able to save and load variables by saving a file? Let's say i let the user input a variable wert$ and i wanted to load what the user inputed.. how? Posted
  • #2 ✎ 1551 12Me21 Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website You can save/load strings as text files: SAVE "TXT:filename",string$ string$=LOAD("TXT:filename",FALSE) 'false disables the message box when loading files. Posted
  • #3 ✎ 24 Crazyblob Beginner Programmer I'm just starting out! I'm still trying to grasp the basics. Programming Strength Video Games I like to play video games! Hobbies Minecraft Is Awesome! I love Minecraft! Express Yourself Thank you! Posted
  • #4 ✎ 24 Crazyblob Beginner Programmer I'm just starting out! I'm still trying to grasp the basics. Programming Strength Video Games I like to play video games! Hobbies Minecraft Is Awesome! I love Minecraft! Express Yourself What about for a value instead of a string? (1,2,3) Posted
  • #5 ✎ 1551 12Me21 Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website For that you have to use an array. For example, to save/load the variables A, B, C: 'saving DIM ARRAY[3] ARRAY[0]=A ARRAY[1]=B ARRAY[2]=C SAVE "DAT:filename",ARRAY 'loading DIM ARRAY[0] '(if you haven't created the array already) (Also, LOAD automatically expands/shrinks the array to the the correct size) LOAD "DAT:filename",ARRAY,FALSE A=ARRAY[0] B=ARRAY[1] C=ARRAY[2] Posted
  • #6 ✎ 24 Crazyblob Beginner Programmer I'm just starting out! I'm still trying to grasp the basics. Programming Strength Video Games I like to play video games! Hobbies Minecraft Is Awesome! I love Minecraft! Express Yourself Sorry but I don't really get it. I want to save the current state of a variable but it says I can't have a duplicate when I rename the arrary. What's the letters (a,b,c) for? I'm probably being really dumb, sorry Posted
  • #7 ✎ 24 Crazyblob Beginner Programmer I'm just starting out! I'm still trying to grasp the basics. Programming Strength Video Games I like to play video games! Hobbies Minecraft Is Awesome! I love Minecraft! Express Yourself What if the user inputs the amount they want in the variable? How would I array that Posted
  • #8 ✎ 86 Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies DAT files are strictly only for numbers, it saves an array of numbers into a file. Then you can open that file later, and get all the values back. A,B, and C are your variables, you can save as many as you want, 12Me21 was giving an example of saving 3 variables named A,B, and C. So to elaborate on his answer, at any given time in your program, you can run the save code, and at any given time you can load that file. If you're going to store strings it gets a little more complicated 'A function that saves my current game variables DEF SAVEMYGAME DIM A[0] PUSH A,MY1STVARIABLE PUSH A,MY2NDVARIABLE PUSH A,MY3RDVARIABLE PUSH A,MY4THVARIABLE PUSH A,MY5THVARIABLE SAVE "DAT:MYSAVEFILE",A END 'A function that loads the game variables, setting all of their values DEF LOADMYGAME IF CHKFILE("DAT:MYSAVEFILE") THEN DIM A[0] LOAD "DAT:MYSAVEFILE",A,0 MY1STVARIABLE=SHIFT(A) MY2NDVARIABLE=SHIFT(A) MY3RDVARIABLE=SHIFT(A) MY4THVARIABLE=SHIFT(A) MY5THVARIABLE=SHIFT(A) ENDIF END So a test program would be something like this MY1STVARIABLE=1234 MY2NDVARIABLE=1.234 MY3RDVARIABLE=4321 MY4THVARIABLE=43.21 MY5THVARIABLE=432.1 SAVEMYGAME 'Save these values to a file Now say, you're opening up the game again and want to restore those values MY1STVARIABLE=0 MY2NDVARIABLE=0 MY3RDVARIABLE=0 MY4THVARIABLE=0 MY5THVARIABLE=0 LOADMYGAME 'Now: 'MY1STVARIABLE is equal to 1234 'MY2NDVARIABLE is equal to 1.234 'MY3RDVARIABLE is equal to 4321 'MY4THVARIABLE is equal to 43.21 'MY5THVARIABLE is equal to 432.1 Does that help? It's different for strings, for strings you'll want to use a TXT file instead of DAT file type. Posted Edited by Simeon
  • #9 ✎ 24 Crazyblob Beginner Programmer I'm just starting out! I'm still trying to grasp the basics. Programming Strength Video Games I like to play video games! Hobbies Minecraft Is Awesome! I love Minecraft! Express Yourself Alright thank you. Yeah that helped a lot. I managed to save and load the variables but there was a issue, when I tried to print the variable, it would always be 0. (followed 13Me21 example.) I guess I did something wrong, I fixed the issue with if c==array[2] then c=2 Am I doing something wrong? Posted