LoginLogin
Might make SBS readonly: thread

Need help with save"txt

Root / Programming Questions / [.]

ArrowCreated:
Still trying TO make a save function how to use save"txt with array to create save function so players can save progress

I got it to save using save"txt for each variable but that would have me saving a bunch of times every time I want to save so I need an array

Are the things you are saving numbers?

I'm creating a rpg the variables I'm trying to save are for the players stats Atk,Def,gold,hp,sp

Put this code near the beginning:
VAR SAVE[5]
It will make an array (essentially a bunch of variables put into one) that will store your save data. When you want to save, use this:
SAVE[0]=ATK
SAVE[1]=DEF
SAVE[2]=GOLD
SAVE[3]=HP
SAVE[4]=SP
SAVE "DAT:SAVEDATA",SAVE
The first 5 lines put your variables into the array, and the final line saves the array as a DAT file.
Side NoteArrays start with 0 for some reason... If you have an array with 5 things in it, the array will go from 0-4. Odd.
When you want to load, use this:
LOAD "DAT:SAVEDATA",SAVE,0
ATK=SAVE[0]
DEF=SAVE[1]
GOLD=SAVE[2]
HP=SAVE[3]
SP=SAVE[4]
If I remember correctly, this will only work with integers. You can't save real numbers as DAT files. (Integers are variables that can't have decimal points and real numbers are the ones that can) If your numbers ARE real numbers, then you can use this method, but it's a bit more complex. It's like your original method, but it puts multiple numbers into one string. To save, use these lines of code:
SAVE$=STR$(ATK)+"*"+STR$(DEF)+"*"+STR$(GOLD)+"*"+STR$(HP)+"*"+STR$(SP)
SAVE "TXT:SAVEDATA",SAVE$
The first line puts all the variables together, separated by *s using the STR$ function, which converts numbers to strings. Here's where it gets complex. To load, use these lines of code:
LOAD "TXT:SAVEDATA",0 OUT SAVE$
ATK=VAL(MID$(SAVE$,0,INSTR(SAVE$,"*")))
SAVE$=MID$(SAVE$,INSTR(SAVE$,"*")+1,LEN(SAVE$)-INSTR(SAVE$,"*"))
DEF=VAL(MID$(SAVE$,0,INSTR(SAVE$,"*")))
SAVE$=MID$(SAVE$,INSTR(SAVE$,"*")+1,LEN(SAVE$)-INSTR(SAVE$,"*"))
GOLD=VAL(MID$(SAVE$,0,INSTR(SAVE$,"*")))
SAVE$=MID$(SAVE$,INSTR(SAVE$,"*")+1,LEN(SAVE$)-INSTR(SAVE$,"*"))
HP=VAL(MID$(SAVE$,0,INSTR(SAVE$,"*")))
SAVE$=MID$(SAVE$,INSTR(SAVE$,"*")+1,LEN(SAVE$)-INSTR(SAVE$,"*"))
SP=VAL(SAVE$)
Oh boy... This is a mess. This chunk of code takes each value out of SAVE$, one by one. As you can see, there are two core copy-and-pasted lines in this: the VAL(...) and the MID$(...). The VAL() line pretty much says "find the first value I see out of the SAVE$ variable and put it into ATK, DEF, etc." The MID$() line takes what it just found out of the SAVE$ variable. The last VAL() line is different because there are no *s left in the SAVE$ variable, so it just takes the last value and puts it into SP. Edit: how did i not see the mistake

It gave me syntax error

Syntax error on what line? Also, LEFT$ and RIGHT$ are your friends.

for saving numbers, I strongly reccommend that you use DAT files
VAR SAVEARRAY[5]
...
SAVEARRAY[0]=ATK
SAVEARRAY[1]=DEF
SAVEARRAY[2]=GOLD
SAVEARRAY[3]=HP
SAVEARRAY[4]=SP
SAVE "DAT:SAVEFILE",SAVEARRAY
and then
LOAD "DAT:SAVEFILE",SAVEARRAY
ATK=SAVEARRAY[0]
etc. to load

I tried it and it gave me type mismatch. DAT didn't work need to figure out txt I know txt works I just don't how to save multiple variables at once. Have you tested the DAT code because last time I tried it didn't work here's the key for the rpg I'm working on see if you can get save function to work D4X2E4D
VAR Stats[9]

Stats[0] = atk
Stats[1] = dfn
Stats[2] = hpv
Stats[3] = hpm
Stats[4] = coin
Stats[5] = lvl
Stats[6] = c
Stats[7] = armor
Stats[8] = eq
SAVE "STATS",Stats

Try putting DAT: at the beginning of the filename (inside "").

Arrow, I would appreciate it if you learned how to use a forum. This isn't like text messaging: in a forum, you usually make a single post as a response and edit it when you need to add stuff. The only time you should add a new post is if you're replying or if what you're posting has absolutely NOTHING to do with your previous post. I'm fixing your posts; please keep this in mind in the future.

I did in the code I forgot that part when I was making the post Also, here's more code: LOAD "STATS"Stats ATK=Stats[0] DFN=Stats[1] HPV=Stats[2] HPM=Stats[3] COIN=Stats[4] LVL=Stats[5] C=Stats[6] ARMOR=Stats[7] EQ=Stats[8]

It makes it very difficult to help you when you 'forget' things like that. If you want help, I suggest you 'remember' to give accurate information. What line gives the type mismatch?

Sorry about that I messed because I'm half asleep my cousin kept me up late last night Type mismatch in 1350

(count to 3) And what's on line 1350?

LOAD "DAT:STATS",STATS

The variable STATS: is there one or more DIM or VAR lines declaring it? Where are they (specifically, inside or outside DEF blocks)?

Not that I can find the key is D4X2E4D can you check it

I don't think DAT will work I've gotten TXT to work but not the way I need

If the variable STATS is not declared, that's a problem. At or near the beginning of your code (but after CLEAR, if you use it), include
DIM STATS[9]
(or 5 or whatever the proper number is).