Game Actors

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
DedGzus
Posts: 11
Joined: Sat Jan 07, 2023 10:11 am

Game Actors

Post by DedGzus »

I'm trying to wrap my head around how to define an array of game actors in 6502 asm. I believe there is a way to define a struct and then allocate ram for an array of that struct. Say, 8 byte struct, 16 total game actors maximum, so lets say I wanna use addresses $0400 through $047F to store that data. How do I define this structure and repeat it 16 times over that area of memory? Hope this makes sense.
Fiskbit
Posts: 891
Joined: Sat Nov 18, 2017 9:15 pm

Re: Game Actors

Post by Fiskbit »

You probably don't want to define an array of structs because 6502 isn't very good at accessing this kind of data structure. Normally, games will instead define individual arrays of each byte, so you have an array of x positions, an array of y positions, etc. Then you can simply have the actor index in X and get the desired property from any of the arrays with ease.

If you're using an array of structures, you either have to have actors with indices that are arraysize apart, or if your array is larger than 256 bytes, you have to use a zero page pointer and (indirect),Y addressing, which is limiting and expensive.
DedGzus
Posts: 11
Joined: Sat Jan 07, 2023 10:11 am

Re: Game Actors

Post by DedGzus »

That makes a lot more sense. Thank you.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Game Actors

Post by tokumaru »

Yeah, a structure of arrays is how most games do it. When processing an actor, it's customary to have the X register control which actor is being manipulated. If you need to use X for anything else during this period, you can simply save its value to RAM and restore later, but in my own experience, this is hardly ever necessary.

For things like collisions between actors, where you need to compare properties from both actors, you can just use the Y register to point to the secondary object.

Most types of games will need way more than 8 bytes per actor, though. Even if the gameplay takes place in a single screen, in order to produce smooth movement you're gonna need to keep track of velocities and sub-pixel positions. Each actor often needs to keep track of the animation frame it's in and how long to wait before switching to the next frame. There's also health, logic states, and a bunch of other things that you're only gonna realize you need once you start designing and coding their logic, so try to plan ahead and be generous with the amount of RAM you're setting aside for this.
Post Reply