Temporarily cloning objects

I’m trying to create an RPG-esque game, complete with randomly selected enemies to fight, and I figured the best way to go about it would be to copy enemies into the player’s location, then remove them when I’m done with them. I’ve tried this in two ways, and both have some pretty big flaws.

First of all, I tried using Dynamic Objects to simply clone the enemy. However, the problem with this is that there seems to be no way to “delete” a cloned object. It can be removed from play, but it seems to remain in memory, as the interpreter freezes up after a number of clones roughly equal to the dynamic memory allocation have been created. While the dynamic memory allocation can be set very high, having this problem be at all possible in the first place seems like bad form.

The second way I tried was to have a mostly blank table defining some enemies and then populating the table during play, so that clearing an entry from the table would hopefully completely remove the enemy, but it seems defining something with a table only works when the source is interpreted, so I don’t think that would work.

I can think of a workaround, but it becomes significantly messier when applied to other uses, such as disposable items the player can carry multiple of. So I was wondering if someone could suggest a way to temporarily copy an object and then discard it when done with it, including reclaiming the dynamic memory it uses. Failing that, some suggestions on how to handle this sort of thing would be nice.

  • Create a special room called the holding area.

  • Whenever you need to allocate a new object:
    – Check whether there is an object of the appropriate class in the holding area.
    – If there is, decide on that object.
    – Otherwise, allocate one using Dynamic Objects and decide on that object.

  • Initialize all fields of the object.

  • Whenever you need to deallocate an object:
    – Set an “inactive” property on it, if appropriate. (So that monsters in the holding area don’t fight each other, and the like.)
    – Move it to the holding area
    – Deallocate anything on / inside it, if appropriate.

Basically, create a load of identical items and then move them on and off stage when needed.

Hope this helps.

So here’s the thing. If there is a convenient upper limit to the number of monsters that can be on-stage at any given time, you don’t really need Dynamic Objects. You create N “blank” monsters, and swap them in and out as climbingstars suggests.

If there isn’t such an upper limit, then the player could conceivably run out of dynamic memory no matter what, by invoking too many on-stage monsters at the same time. (But really, your game will become impractically complex long before that. What strategy is a player going to employ against 16 monsters at the same time? 32? A text interface is not good at that sort of situation.)

You could also use Dynamic Objects to create monsters but then swap them off-stage for reuse.

No matter what, this leaves you in a situation of manually setting (or resetting) monster objects to a new template. I suspect you’re already considering this, and it is messy, as you say. The Dynamic Objects extension goes through some low-level contortions to clone a property block. It’s simpler, if more tedious, to just set all the properties by hand.