How to add a hit count (health points) to individual instances?

The name of the game is Custom Primitive Data (check docs at the bottom).

The material for a brick:

  • each instance stores a float which is used to Lerp between 2 colours (to visualise hits remaining)
  • this can (and probably should) get prettified

Making a brick wall:

  • create a bunch of instances and store their data in an int | int Map
  • instead of an integer, you could have a struct and hold all kind of data here; perhaps this brick is worth some coins, or explodes when destroyed and takes their neighbours with it (I’m thinking you’re making an Arkanoid but you might be about to Minecraft 2.0)
  • we need to feed the material a 0-1 Alpha range for the Lerp so we math it out
  • if you’re creating thousands+ of those, it might be worth looking into how Dirty State works

Brick getting hit:

  • a Custom Event pipes in an index of the hit instance
  • we find its data in the Map and adjust it, removing 1 hit from Remaining, and shoving the value back into the Map
  • we update the material in a very similar fashion as before
  • we need to get rid of an Instance if no more Hits remain…
  • …if you were to remove an instance of an Instanced Static Mesh, the indexes would get jumbled and we’d need to rebuild the Map; so we don’t remove the Instance, we move it below ground instead (or where the player cannot interact with / see it). This way the indices remain intact.

Hitting bricks:

  • this detects what’s under the cursor and calls the above-mentioned Custom Event, providing the hit instance index
  • you’ll probably have a more elaborate interaction method

The end result

So, 1 material and 1 Instanced Static Mesh, no iteration, no ticking. So this should be good for all kind of fun stuff:


2 Likes