i was wondering how to create player stats that stay there and are available for when the program is restarted. i know i cant use saferam for this because it gets deleted when program is done running. and than i can also do something like


Code:

stats:
  .db 5, 5, 5


but if i did that how would i access each different element of it.
Looks like you have an array set up. In that case, use hl to index the array, and load from offsets of hl. For instance:


Code:

  ld b,3
load:
  ld hl,statssave
  ld de,stats
  ld a,(hl)
  ld (de),a
  inc hl
  inc de
  djnz load


I know the above could have been done better with ldir, but it's important that you understand the concept of loading from (hl).

As for saving it after the program quits: you can either have an area of the program set off for it and use SMC, or you can create an appvar, list, or other object to house it. I prefer to use appvars, but that's just me.
can you please comment the above code so that i can understand what exactly is going on.
Most shells, including Ion, DCS, and MOS, use some form of writeback. Therefore SMC would be the way to go.
how could i do it with ion since that is what the game is going to be made with
Just have your .db whatever somewhere in the program, and directly read and write to it. No need to do anything else fancy...
Sure thing lafferjm


Code:
 
;ok, let's assume that we have 3 stats saved at statssave and let's give them the values of 5 each (normally this will have been saved in a previous run by the program)
  ld b,3
load:
  ld hl,statssave ; index save location with hl (essentially we have loaded some address into hl)
  ld de,stats  ;index runtime location with de
  ld a,(hl)   ;load the byte at the memory location that hl is pointing to (the first loop this will be the first byte of statssave)
  ld (de),a  ;store this to the address de is indexing
  inc hl  ;make hl address the next byte of memory in statssave
  inc de  ;make de address the next byte of memory in stats
  djnz load  ;loop until b = 0


statssave:
  .db 5,5,5

stats:
  .db 0,0,0
Good stuff @ Chip. Shock That's an impressive amount of commenting...
KermMartian wrote:
Just have your .db whatever somewhere in the program, and directly read and write to it. No need to do anything else fancy...



how would i do that. and thanks for the commenting chipmaster it helped a lot.
heh, thanks. I just wish I could have written it fast enough to be the next post after lafferjm's request. Drat, I couldn't even type up this response fast enough to be right after you, Kerm. Laughing
lafferjm wrote:
KermMartian wrote:
Just have your .db whatever somewhere in the program, and directly read and write to it. No need to do anything else fancy...



how would i do that.


That's what I was demonstrating in my code segment. Well, at least the reading part. To write, you essentially do the reverse of that process.
lafferjm wrote:
how would i do that. and thanks for the commenting chipmaster it helped a lot.


By reading a tutorial like "Learn Z80 ASM in 28 Days" Rolling Eyes
is this possible and if so i think i might do it because it would be a lot easier.


Code:

ld hl,atk
inc hl
ld atk,hl

atk:
 .db 4


and do the same for the other stats.
Incorrect use of hl. You need:


Code:
ld hl,atk
inc (hl)
Or, alternatively:
Code:
ld a,(atk)
inc a
ld (atk),a
kinda. Its actually "inc (hl)" because you want increase the number stored at the location HL points to, not HL itself

here's what your code is doing


Code:
ld hl,atk ;HL = memory location 'atk'
inc hl ;hl = memory location 'atk' + 1
ld atk,hl ;!INVALID!

atk:
   .db 4


here's what it should be


Code:
ld hl,atk ;HL = memory location 'atk'
inc (hl) ;inc the value at the address in HL
           ;no need to 'ld' the value, because you directly edited it

atk:
   .db 4


EDIT: You beat me Kerm Razz
thanks kllrnohj and Kerm. i think i might use that way since it is a lot easier.
lafferjm wrote:
thanks kllrnohj and Kerm. i think i might use that way since it is a lot easier.


But not as reliable. There is always the possibility that the writeback routine is buggy (i know its a slim chance - but a chance nonetheless), or more likely that the user has turned writebacks off
My way = Kerm and Kllnohj's way, except that mine is dynamic, meaning you can have variable amounts of stats instead of loading them individually. In any case, ldir is the way to go with moving sequentially located memory.
if i load the stats individually could i display them with the code that i thought would increase atk.
If you're loading stats individually, you are programming very inefficiently. Beyond that, how do you expect that code segment to display the stats. That's only going to copy them somewhere.

To display a number you have a multitude of options. You can use _DispHL which will display HL in big text. You can use _dispOp1a which will display a digits of Op1 in small text. You can convert the number to a string and use _puts or _vputs. You can also write your own display routine.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement