How To

Store arrays into persistent memory ?

0

G-9 2021-04-07 08:44

Still for my RPG 😂
Actually I was thinking about storing user’s name into persistent memory but it wasn’t working. Now I did a basic 6 char limit, with CHR$() and ASC() converting to store numbers instead of letters, then to convert them into letters, and finally matching them to recover the name. Phew.
Does anyone knows a better and simpler way to do that ?


Timo 2021-04-07 16:38

The idea is correct. You can put the code in nice SUBs, so it's easy to use.

This is from https://lowresnx.inutilis.com/topic.php?id=592, but I cleaned the test:

The code:

SUB POKEPS(A,S$)
L=MIN(255,LEN(S$))
POKE A,L
FOR I=1 TO L
POKE A+I,ASC(MID$(S$,I,1))
NEXT I
END SUB

SUB PEEKPS(A,S$)
L=PEEK(A)
S$=""
FOR I=1 TO L
S$=S$+CHR$(PEEK(A+I))
NEXT I
END SUB

The test:

DIM I$,O$
I$="TEST 123"
A=$E000
CALL POKEPS(A,I$)
CALL PEEKPS(A,O$)
PRINT I$
PRINT O$


Timo 2021-04-07 16:40 (Edited)

If you want to save more than one string, make sure the addresses have enough room between them. In theory these SUBs support strings with up to 256 bytes. So you could use these memory addresses:
$E000
$E100
$E200
$E300
...


was8bit 2021-04-07 20:59 (Edited)

One trick i enjoy trying to use is that PERSIST memory is the exact same size as both BG 1 and BG 0 memory... for example, in my Farm Life game, i use the background as game data (no arrays) so i save the game via saving the background memory to /from persist memory, i also store game data on screen, and manually extract them to reload some key variables (like cash, game level, etc)

But for normal games, Timo's approach is best :)


Log in to reply.