I’ve got a module loaded with extensions, which define a bunch of SinglePieces.
Now I’m trying to write a Java method to instantiate specific SinglePieces from these extensions and add them to a game board.
I’ve got pretty much everything figured out: I can get the correct map from iterating over Map.getMapList(), I can add a GamePiece to that map with map.AddPiece(gp) - I’m just missing the most important thing: The GamePiece object.
I’ve managed to create a GamePiece based on an image file with GameModule.getGameModule().createPiece(String), but I need one with prototypes and everything, as designed in the Extension Editor. I thought I’d be able to do this by using the ID of the SinglePiece, but haven’t got that to work.
Any guidance would be greatly appreciated,
I don’t know how to load them, but I solved it with a hidden board where each required piece was placed.
When I’m requiring a piece, I simple create a clone (see clone.java) and use it.
That’s a good thought, but I’ll be needing hundreds of different GamePieces, all loaded from extensions that may well change without my code changing. I need a purely runtime solution, unfortunately.
I finally found a (somewhat roundabout) way of doing this. Here it is, for the benefit of future generations:
First, you need to find the GamePiece Palette you wish to load Pieces from. In my case, I could do this with the following:
List piecePalettes = GameModule.getGameModule().getAllDescendantComponentsOf(PieceWindow.class);
You can iterate over them, asking them for “getConfigureName()” if you know what you’re looking for, to get the right one.
Once you have the right palette (let’s say we saved it in a variable called palette), follow the same logic to get its PieceSlots:
List slots = palette.getAllDescendantComponentsOf(PieceSlot.class);
Now you can iterate over all your slots, asking each one for its “getPiece()” and then querying the GamePieces for the correct one.
Once you have the Piece, you can add it to a map with maps.get(?).addPiece(…).
Thanks for your reply. That’s a better method.
But while testing I found a little error in my try out:
If you want to get one piece multiple times, this won’t work. You need to get a clone of the item.
I’ve added a small class to my reply which can be added via “Imported Class” to a Game Piece Palette and then allows to retrieve all content of that palette via its name.
Be aware that the class doesn’t check if the game piece does exist and that it should be a singleton and shouldn’t be added to another palette.