Creating counter from prototype?

How would I go about creating a de novo counter from a prototype, within custom code?

For instance, assume I already have a map object and a tile on that map, and my custom code has established that there is a resource token of type A on the tile, and a worker piece. How could I activate the tile to make the current resource token disappear and, say, two new tokens of a completely different type B appear? (Or, for instance, imagine that a pawn has reached the far side of the chess board and needs to be queened - but I want this to happen programmatically, not via a user switching pieces.) The tokens I want are in the game’s piece palette, so I could possibly find that and clone what I need from there, but is there a simple one-line solution for creating a completely new game piece? And, similarly, what is the best way to delete a piece from the game?

Thanks for any help you can provide.

I would still be keen on any pointers to an easy solution here.

Ideally, I would be able to write functions like:

CreatePiece(Map m, String prototype, int x, int y)
CreatePiece(Map m, String prototype, Zone z)

and call them like:
CreatePiece(mainMap, “Infantry”, 500, 600);
CreatePiece(mainMap, “Infantry”, rebelBaseZone);

By far, the easiest way is to set up the replacement/new pieces as Place Marker or Replace with Other traits with appropriate Key Commands. Similarly, add a Delete trait. Then all your custom code has to do is issue the appropriate Key Commands to the counters in question. You can hide the commands from the players if you don’t want them to be able to interact with those pieces directly.

Okay, thanks, I’ll get started on that… How would I issue a Key Command from within custom code?

// Old Style KeyStroke
KeyStroke deleteKey = KeyStroke.getKeyStroke(‘D’, java.awt.event.InputEvent.CTRL_MASK);

Command c = unit.keyEvent(deleteKey);

// Named key Stroke
namedKeyStroke moveKey = new NamedKeyStroke (“move”);

c .append(unit.keyEvent(moveKey.getKeyStroke());


That approach worked quite well. I can now split a large token into 5 smaller ones with a single shortcut key, and I am about to tackle the creation of an arbitrary number of tokens of different types, based on the game’s economic logic.

One issue with this approach is that there does not seem to be an obvious way to get a pointer to the newly created objects, to position them. The workaround will be to search the map, find the new objects, and then rearrange them.

There is something odd about generating false keystrokes programatically to run bits of code that are hard to access in other ways, but I guess Vassal has grown organically, so it has a few gnarly bits. Presumably, there are plans to streamline this in Vassal 4.

I have all of this working well now, so thanks for your help.

One thing to note, if anyone is following a similar path, is that creating new tokens via the keystroke route does not immediately update the set of pieces available on studying the map programatically. The creation event is put in the event queue (I presume), so the code launching the creation event cannot do anything with the new token. To get hold of the new tokens and reposition them, I had to put the repositioning code in a function triggered by yet another keystroke, so that it was executed in the appropriate sequence, further down the queue.

I faced similar issues deleting tokens and creating change. For instance, if a map has a large token worth 5 and 2 small tokens worth 1 each (total 7), removing 4 of that resource needs to leave 3 small tokens, so there is a combination of creating and deleting. The code that works out what to delete and what to create cannot check on the progress of those tasks, directly, by counting what is on the map, because the creation and deletion is still in the event queue.