How to: Create Indexed Actors from Prototype

adam36021adam36021 Member, PRO Posts: 45
edited February 2022 in Community Tutorials

Hi all, sharing this method in case it helps someone.

One of the common restraints with Gamesalad is that you cannot easily index your spawned actors. This makes logic and pathfinding difficult for a lot of genres of games, and people are using some pre-setup actors and instances which limits the scope of what you can do.

For example, take a strategy game (tower defense, turn-based grid, RTS, whatever). In Gamesalad, most approach this by creating 10+ tower actors, and each actor is unlocked, with logic running. This gets difficult if you want to do things with other "spawned" logic actors, like monsters, units, etc. Something as straight forward as showing individual unit's health can become messy.

The solution:

I have made a few "complex" strategy game systems using a workaround method. It's fairly straight forward.

  1. Your prototype unit is the master "template" for all units created. let's call it "Unit_Prototype".
  2. Spawn your "Unit_Prototype" using another actor (a level controller, a button that spawns units, whatever you want). Important: The X attribute where you spawn this actor will become it's ID.
  3. In your Unit_Prototype, set up a rule that makes this happen: On spawn (i like rule: self.time = 0), set a [self.selfID (you can call this whatever you want)] attribute to self.position.x
  4. This is also where you could load values to this unit. For example, reference table values that control stats, images, movement speed, etc.
  5. This is where you can add rows to a Table that can handle all unique stats for this unit. [Self.selfID] would point to a table row that holds all stats you care about -- Health, position, whatever.
  6. Add some code to move this unit to wherever it should be (adjust the x/y, etc)
  7. You now have an indexed unit with unique stats.

That's basically it. I can upload a demo if it would help.


In this project, a grid turn-based strategy game, I use this concept to: spawn a bunch of peasents who pathfind to the nearest tree, and then start "harvesting" the tree.

Each peasant has its own X,Y, pathfinding logic, health, power, resources gathered, etc.

Each tree has its own "supply" that is drained when peasents attack it. And so on.

I can spawn 10 peasents or 100 peasents, it all works just fine. I can spawn 1 tree or 50 trees, they are distributed at random. Doesn't matter. All peasents pathfind to the nearest spawned tree.


Some things to note:

  • You have to rely on tables heavily to do anything meaningful with this, and specifically functions like tableSearch and tableCellValue
  • table functions are a bit weird. In particular, inserting or deleting rows can cause bugs. So I would suggest you instead create a table with your "maximum" number of units you want to allow, and 0 it out. Instead of adding rows, just change values at the first row with a 0.
  • your prototype actor for units is going to be big. You have to have all the logic you want the unit to use in there, for each variation, so be aware.
  • There's tons of other uses for this type of thing, the immediate use for me is strategy game, but I am sure there are lots of ways to use table-linked spawned actors
  • Performance will drop the more things you spawn, but that's an issue for most engines and games -- that's why RTS games have unit caps. It depends on how much logic you put in to each actor, and how heavy they are. Probably best for turn-based games, but I have made a few real-time games that work well until I get in the hundreds of actors. So worth considering, you would want to make sure your code can "recycle" previously used IDs if necessary.


Sign In or Register to comment.