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.

1 Like

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.

This is an extension question to the above problem.
I am trying to clone a adventurer, a subkind of person, that the PLAYER created, so I don’t know the properties beforehand. That eliminates the idea of creating a bunch of “blanks”. Once I create the adventurer, I cannot create another one because it overwrites my original.

I want to use Dynamic Objects, but can’t figure out how to use it. The example does not help too much.

I gave an example here of the cheap and easy (and fairly reliable) way to fake dynamic objects.

Here’s a more generic version with fewer distractions:

lab is a room.

an object can be actual or in-potentia.

Definition: an object is null rather than non-null if I6 condition "*1==0" says so (it is the null value).

To decide what K is a/-- new (name of kind of value K):
  let v be a random in-potentia K;
  if v is non-null, now v is actual;
  decide on v;

To recycle (v - value of kind K):
  now v is in-potentia;
  [ but really you'll want a separate recycle phrase per kind so you can reset 
    properties to their defaults, and, maybe, move v to nowhere ]

to recycle (c - coin):
  now c is in-potentia;
  now c is nowhere;

A coin is a kind of thing.
A coin can be actual.
There are 3 coins.

instead of jumping:
  recycle a random carried coin;

instead of thinking:
  let x be new coin;
  move x to lab;

test me with "think / think / think / look / think / get all / jump / inventory"

it produces:

>test me
(Testing.)
 
>[1] think
>[2] think
>[3] think
>[4] look
lab
You can see three coins here.
 
>[5] think
 
*** Run-time problem P33: Attempt to 'decide on nothing'.
 
>[6] get all
coin: Taken.
coin: Taken.
coin: Taken.
 
>[7] jump
>[8] inventory
You are carrying:
  two coins
 
> 

The drawback, of course, is that they’re not really dynamic and if you don’t choose the right maximum number in advance, you’ll end up with an error. In your code, you might want to test to verify something is non-null after each invocation of new. But, of course, there’s not a lot you can do about it at run-time, so you could also not worry about testing it… you’ll see a run-time error anyway, as the example shows.

[ Edited to add…]

It wouldn’t be a terrible strategy to test before calling new, though, if you had game-behavior in mind for what to do when there aren’t any more in-potentia objects of the relevant kind.

To decide if a/an/-- (name of kind of value K) is available:
  decide on whether or not the number of in-potentia K is not zero.

instead of thinking:
  if a coin is available begin;
    let x be new coin;
    move x to lab;
  else;
    say "No more coins.";
  end if;

As suggested (but I’m going to elaborate as I do) since you’re talking about legions of NPC/enemies, think about his this way:

Consider a theatrical performance. Frequently there are more characters than actors. An actor will go off stage, change costume and emerge as a different character.

You can do this in your game. Items and NPCs needn’t be one use. Have a troupe of 3-5 trolls or hobgoblins or whatever, and just have the player fight and kill the same ones that go off-stage, recover, and then emerge for the next encounter. You can even randomize bits about them to give the impression they are different like different clothing, one randomly has a helmet, one has a club instead of a sword…

You could have different kinds - like six skeletons, six trolls, six pirates - or you could just have generic limited cast of “enemies” so at one point one NPC “actor” is a troll, then re-emerges as a pirate, then an acidic ooze - just change the name and the hit-points/stats for each actor as appropriate for each encounter.

Or you could cast by type - 5 “humanoids” for when they need arms and legs and use weapons, 5 “creatures” for oozes and gelatinous cubes that don’t have limbs or weapons, 5 “flyers” for creatures who have wings…etc. You have basic kinds with similar abilities, and just change them cosmetically.

TL;DR: don’t discard, recycle

1 Like