What are change scripts?

GC2

How do you use them? How do they differ from turn scripts?


Turnscripts are a type of element. The function RunTurnScripts at the end of every turn goes through and runs them all.

Changescripts aren't an object in their own right. They are attached to an existing object, as script attributes whose name starts with "changed".

For example, if you create a script attribute called player.changedhealth, it will be run whenever the attribute player.health changes. If you give an NPC a script attribute called changedmoney, it will be run every time their money attribute changes.

These scripts aren't run at the end of the turn; they are run immediately when the attribute changes. It's often better to use a changescript to handle something that changes regularly, so you know it always happens.

There are a few changescripts that are included by default. Every object has a changedparent script, which triggers whenever it is moved to a different room or container. This script just checks if that object is currently being used as the player, and if so displays the description of the new room.


GC2

Is there a way to get information on how attributes change? Like does hp increase or decrease and how much?


Yes. Within a changescript, you can use the special variable oldvalue to find the old value; and then you can subtract that from the current value to find out how much it's changed by.


an example:

<object name="room">

  <inherit name="editor_room" />

</object>

<object name="player">

  <inherit name="editor_object" />
  <inherit name="editor_player" />

  <attr name="parent" type="object">room</attr>

  <attr name="condition" type="string">normal</attr>

  <attr name="life" type="string">Life: 999/999</attr>

  <attr name="current_life" type="int">999</attr>
  <attr name="maximum_life" type="int">999</attr>

  <attr name="changedcurrent_life" type="script">

    <![CDATA[

      if (this.current_life > this.maximum_life) {
        this.current_life = this.maximum_life
      } else if (this.current_life < 0) {
        this.current_life = 0
      }

      if (this.current_life = 0) {
        this.condition = "dead"
      }

      this.life = "Life: " + this.current_life + "/" + this.maximum_life

    ]]>

  </attr>

  <statusattributes type="stringdictionary">

    <item>
      <key>life</key>
      <value>!</value>
    </item>

    <item>
      <key>condition</key>
      <value>Condition: !</value>
    </item>

  </statusattributes>

</object>

-------------------

example outputs as you take damage when fighting a monster:

// initially:
// Life: 999/999
// Condition: normal

Life: 900/999
Condition: normal

Life: 500/999
Condition: normal

Life: 100/999
Condition: normal

Life: 0/999
Condition: dead

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums