LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

How do you do save files?

Root / Programming Questions / [.]

GuzzlerCreated:
I want to know how you save things. I kinda know how to save a string, but how do you do numbers?

I have never actually tried to program a save file, however I have a very radical and bizarre idea to propose: Using the program editing functions, you can edit a specific line of a program (perhaps a DATA function to store a player's score and whatnot. The easiest way to do this would be to have a section at the beginning of the program that gets GOSUB'd whenever the save file needs to be referenced. There are many problems with this though, (such as the need to save the program whenever the player quits) but I just thought I'd dump the idea for future thought.

Or, just use a DAT file. It's an array in a file!
SAVE "DAT:FILENAME",ARRAY
LOAD "DAT:FILENAME",ARRAY,true/false(optional)
Keep in mind that if you're loading a DAT file, the destination array has to have the same dimensionality as the one you saved into the file (and string arrays can't be saved, but TXT exists.)

See SAVE (3) and corresponding LOAD (4): http://smilebasic.com/en/reference/#file

Keep in mind that there isn't any apparent distinction between int and real-type DATs, but the file DOES remember what type you put in. An int array will save an int DAT, and vice-versa.

Lol, I just learned how to do this for my game! What a coincidence. My game is not unlike the GBA Ace Attorney where it has a structure of @TITLE, @CH1, @CH2, @CH3, @CH4, @ENDING.
  • A dialog will prompt you to save at the end of Chapters 1, 2, and 3.
  • You cannot save elsewhere, and there's only one save.
  • I added a line in @TITLE so that the player can press Y to load from your save.
  • If there's a save file, the program will jump to the beginning of the next chapter. If there's no save, it does nothing.
With those in mind, here's my code:
@TITLE
(...)
DIM SAVEARRAY[1]
WHILE TRUE
IF BUTTON(2) AND #A THEN GOTO @CH1
IF BUTTON(2) AND #Y THEN
 IF CHKFILE("DAT:GAMESAVE")==TRUE THEN
 LOAD "DAT:GAMESAVE",SAVEARRAY,FALSE
 ON SAVEARRAY[0] GOTO @CH2, @CH3, @CH4
ENDIF:ENDIF
WEND

@CH1
(at the end)
SAVEARRAY[0]=0
SAVE "DAT:GAMESAVE", SAVEARRAY

@CH2
(at the end)
SAVEARRAY[0]=1
SAVE "DAT:GAMESAVE", SAVEARRAY

@CH3
(at the end)
SAVEARRAY[0]=2
SAVE "DAT:GAMESAVE", SAVEARRAY

Using the program editing functions, you can edit a specific line of a program (perhaps a DATA function to store a player's score and whatnot.
Already been done: http://smilebasicsource.com/page?pid=174 EDIT: Well, not really. That program will not edit the program itself. It will load the program as a text file, find the part of the text at the end where the data is, put new data at the end of the text, and save the text file again.

Ah, thanks for the explanation on DAT files. I hadn't looked up how to use them yet, and really needed to figure it out.

Lol, I just learned how to do this for my game! What a coincidence. My game is not unlike the GBA Ace Attorney where it has a structure of @TITLE, @CH1, @CH2, @CH3, @CH4, @ENDING.
  • A dialog will prompt you to save at the end of Chapters 1, 2, and 3.
  • You cannot save elsewhere, and there's only one save.
  • I added a line in @TITLE so that the player can press Y to load from your save.
  • If there's a save file, the program will jump to the beginning of the next chapter. If there's no save, it does nothing.
With those in mind, here's my code:
@TITLE
(...)
DIM SAVEARRAY[1]
WHILE TRUE
IF BUTTON(2) AND #A THEN GOTO @CH1
IF BUTTON(2) AND #Y THEN
 IF CHKFILE("DAT:GAMESAVE")==TRUE THEN
 LOAD "DAT:GAMESAVE",SAVEARRAY,FALSE
 ON SAVEARRAY[0] GOTO @CH2, @CH3, @CH4
ENDIF:ENDIF
WEND

@CH1
(at the end)
SAVEARRAY[0]=0
SAVE "DAT:GAMESAVE", SAVEARRAY

@CH2
(at the end)
SAVEARRAY[0]=1
SAVE "DAT:GAMESAVE", SAVEARRAY

@CH3
(at the end)
SAVEARRAY[0]=2
SAVE "DAT:GAMESAVE", SAVEARRAY
Thanks! I don't think the Pheonix Wright GBA game ever came out in America. I might be wrong. But I'll use this to learn.

Thanks! I don't think the Pheonix Wright GBA game ever came out in America. I might be wrong. But I'll use this to learn.
Didn't the GBA games got re-released in a trilogy on the DS?

yes