This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Objects with different number of variables

Objects with different number of variables
by on (#184723)
I'll be starting another NES game soon and am trying to come up with a different way to handle objects. In my current game, I just have arrays of variables that are accessed by using X or Y:
Code:
MAX OBJECTS = 6

X .dsb 6
Y .dsb 6
Health .dsb 6
State .dsb 6
Animation .dsb 6

I can access individual object's variables like so:
Code:
   ; Change object 0's health to 5
   LDX #$00
   LDA #$05
   STA Health, x
   
   ; Add 8 to object 2's X position
   LDY #$02
   LDA X, y
   CLC
   ADC #$08
   STA X, y

This system works fine for objects as long as they have the same number of variables. But what if I want to have an object that doesn't have any health? I could just use the same system but then I would be wasting a variable for an object that doesn't need it.

How have you guys implemented objects into your project? Does it handle cases such as mine?
Re: Objects with different number of variables
by on (#184724)
On the Atari 2600, you have only 128 bytes, so you must make each count. But on the NES, you have 2048 bytes, of which about 512 are spoken for by the structure of an NES program (VRAM transfer buffer, stack, OAM transfer buffer). If your object table has 16 variables for each of 16 actors, that's only one-eighth of RAM. Even if none of them have health at any given moment, you're wasting less than one percent. So some waste is probably acceptable.

If you do end up running out of RAM, and you have some actors that are far more complex than others, such as enemies and their bullets, reserving the highest numbered actor slots for "simple" actors might help you save memory.
Re: Objects with different number of variables
by on (#184725)
You can, of course, reuse the health byte for some other purpose in the object that doesn't require health. You can make an alias for the Health symbol within the object code to make the code clearer (or, you can change the symbol to something more generic, like ObjData0, and alias it to Health for the objects which need it).
Re: Objects with different number of variables
by on (#184741)
thefox wrote:
You can, of course, reuse the health byte for some other purpose in the object that doesn't require health. You can make an alias for the Health symbol within the object code to make the code clearer (or, you can change the symbol to something more generic, like ObjData0, and alias it to Health for the objects which need it).

I use this approach myself, but even then there's still a lot of wasted space.

I have considered other methods, such having 2 sizes of object slots, or having each object allocate only the memory it needs and use pointers instead of structures of arrays, but I don't think the memory savings are worth the increased complexity of the object system caused by these options.
Re: Objects with different number of variables
by on (#184840)
For objects integral to the game, or other "special-case" objects that I expect to spawn and despawn frequently, I sometimes give them their own lists. For example, if I'm using particle effects, they get their own small buffer, same with projectiles. Things that are varied and change state less, like enemies or other objects on the map get placed in a general purpose "objects" list.
Re: Objects with different number of variables
by on (#185107)
Thanks guys. Seems like the most practical way is to just have those extra variables and maybe try to order the less complex objects first.