Triggering on piece instantiation

I’ve just started experimenting with module creation but I’ve hit something that has me scratching my head.

It’s just a learning exercise, but I’m trying to figure out how to give game pieces a unique name by appending a unique id number to an individual piece’s property. I’d like to do it automatically as new pieces are created. I’ve got a global property working as a counter to provide a source of unique numbers. Here are some other bits I’ve gotten working:

  • use a Set Global Property on a piece to increment the global counter
  • use a Dynamic Property to define the local piece’s uid property, either through a user input or a ‘hidden’ key command used by the trigger.
  • use a Trigger on the piece to update the global counter by calling the Set Global Property and then calling the Dynamic property to change the local piece property.

So I think I have most of the components I need. What I can’t seem to find is a way to trigger events when a piece is created. Something akin to a constructor method or an “on creation” handler.

I was able to tie the Trigger to the built in Clone command and Ctrl+C, but that’s only one way a piece can be spawned. Is there a more abstracted approach that would work regardless of what created the particular instance of the piece?

Thanks

Every Map Window has a property, “Key Command to Apply to All Units Ending Movement on This Map”. I’m not certain if this triggers when a piece is created, but if so, that should do the trick (although you’ll need to differentiate between the first time it’s triggered (on creation) and later triggers when the piece is just moved).

Edit: Also, in case you weren’t aware, if you create the new piece with the “Place Marker” or “Replace with Other” traits on an existing piece, those have an option to immediately trigger a command, also.

Thanks for the quick reply.

I tested with the “Key Command to Apply to All Units Ending Movement on This Map” and it works exactly as you described and did require a variable/property so it only triggers once.

In case someone needs this someday what I did specifically was:

  • added a key command (e.g. Ctrl+Shift+NumPad_*) to the Map’s “Key Command to Apply…”
  • gave the piece a Dynamic Property (isSpawned = false) to only allow the command to take effect once. The property also has a setSpawned command to change the value to true.
  • added a Report action as a test to show it was working
  • created a Trigger Action to fire on Ctrl+Shift+NumPad_* that:
    – checks if isSpawned = false
    – executes the setSpawned and report action

I was able to extend that to handle all the additional actions I’d worked out (update a global counter, rename the piece, etc.) though I’ve not worked out the bugs.

The “Key Command to Apply…” approach seems to work when a piece is dragged from the palette and when a piece is dragged between maps–the isSpawned flag stays set to true.

I was even kinda-sorta able to get the behavior I wanted during a Clone command too. I added a Clone component to the piece, changed the key to privateClone, and added a Trigger Action (MyClone) that

  • changes the isSpawned to false (by adding another command to the Dynamic Property)
  • calls privateClone
  • changes the isSpawned back to true (for the original piece)

The new piece doesn’t immediately register the “spawn” logic. It needs to be moved at least once. But honestly this is close enough to what I was trying to do to call it success. Thanks again for the assist.

If the fact that the cloned piece doesn’t register the “spawn” logic immediately becomes a problem, you may be able to use “Place Marker” instead of “Clone”: for the Marker Definition, you would either “Select” the same piece from your Game Piece Palette or At-Start Stack, or (if you ensure all the traits that need to be copied are defined in a Prototype) “Define marker” and just add the same Prototype used to define the original piece (this would allow for the new unit to have slightly different traits, if necessary); then, for the “Key Command to apply after placement”, put the command that triggers your spawn logic.

Note that this only really works if you don’t need the copy to inherit the current state of the spawning piece (there is an option to “Match rotation”, but otherwise the new piece will have all default values).