while (randomly cloning (to a string list (from which all clones can be destroyed)))

K.V.
08 Oct 2017, 08:40

I stole half of this from mrangel and the other from HK

<!--Saved by Quest 5.7.6404.15496-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="while cloning and destroying clones from a string list">
    <gameid>4ec73234-0bcd-4738-8432-7d8b5c45a7ea</gameid>
    <version>1.0</version>
    <firstpublished>2017</firstpublished>
    <appendobjectdescription type="boolean">false</appendobjectdescription>
  </game>
  <object name="room">
    <inherit name="editor_room" />
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
    </object>
    <object name="KV">
      <inherit name="editor_object" />
      <inherit name="namedmale" />
      <clones type="stringlist" />
      <slap type="script"><![CDATA[
        msg ("You can't slap KV!")
        if (ListCount(KV.clones)>0) {
          msg ("...but you CAN slap a clone!<br/><br/>You do just that, and KV is now one again!<br/>")
          foreach (o, KV.clones) {
            destroy (o)
          }
          KV.clones = NewStringList()
        }
      ]]></slap>
      <speak>"Just try to slap me," says KV.</speak>
      <look><![CDATA[KV is a 40-year-old nerd.<br/><br/>(You should probably just {command:slap KV}.<br/>]]></look>
      <inroomdescription type="string"></inroomdescription>
    </object>
    <object name="coin">
      <inherit name="editor_object" />
      <take />
      <usestandardverblist />
      <inventoryverbs type="stringlist">
        <value>Look at</value>
        <value>Flip</value>
      </inventoryverbs>
      <look>Looks like a good coin to {command:flip coin:flip}.</look>
      <flip type="script"><![CDATA[
        msg ("You flip it.")
        coin.x = 0
        while (RandomChance(50)) {
          coin.x = coin.x + 1
          if (coin.x > 1) {
            msg ("<br/>The coin hits the ground and bounces{if coin.x>2: again}!<br/>")
          }
          msg ("Another KV appears!")
          kvClone = CloneObjectAndMoveHere(KV)
          list add (KV.clones, kvClone.name)
        }
        on ready {
          msg ("<br/>You catch the coin.<br/>")
          AddToInventory (coin)
        }
      ]]></flip>
    </object>
  </object>
  <verb>
    <property>flip</property>
    <pattern>flip</pattern>
    <defaultexpression>"You can't flip " + object.article + "."</defaultexpression>
  </verb>
  <verb>
    <property>slap</property>
    <pattern>slap</pattern>
    <defaultexpression>"You can't slap " + object.article + "."</defaultexpression>
  </verb>
</asl>

mrangel
08 Oct 2017, 09:13

I never made a list of clones, because keeping it up to date ends up being a pain. Instead I put this.prototype = this in an object's init script. (I could do it in the start script, foreach (o, AllObjects()) { o.prototype = o }, but I seem to have ended up using the prototype in various places to check if an object is something my scripts can expect to see clones of.

So when I want to deal with them all, I can do something like:

<function name="FindClones" parameters="object" type="list">
  // Find all objects with the same prototype as the one passed in
  if (HasAttribute (object, "prototype")) {
    object = object.prototype
  }
  result = FilterByAttribute (AllObjects(), "prototype", object)
  return (result)
</function>

So then…

foreach (o, FindClones(KV)) {
  msg ("Here's one…")
  if (o = KV) {
    msg ("... but that's the original. Can't slap KV, that would be rude.")
  }
  else {
    DisposeItem (o.name)
    msg ("Clone slapped!")
  }
}

K.V.
08 Oct 2017, 10:12

Aha!

And, yeah... I only stole the while (RandomChance(50)){ spawning clones bit from you.

HK showed me you could use x = CloneObject(KV) to clone KV and get the name of the clone, and I adapted that to the string list.

I like your responses better than mine, too. Hehehe.


jmnevil54
08 Oct 2017, 12:43

Is this supposed to go to Enpherdaen?

What is up with that title, and those parentheses? Lol