Code

From Seltanikor
Jump to navigationJump to search

Code is a type of property that stores a block of TworldPy code, which is mainly the same as Python code. Code properties may not be focused by players.

Any properties in the code that are given "standalone" are executed - text will be focused, code executed, move will move the player.

When the code property is not called but interpolated, any action properties being called or properties being assigned will cause an error. All text properties and variables will be concatenated and appear as the interpolation result. Note that string values are not treated as markup text - for that to happen they have to be passed through the text() function first.

Fields

Code properties are actually Python dictionaries with the following fields:

  • "type": "code" - Determines that this is a code property. Can be set in the build interface by selecting Code from the type drop-down menu.
  • "text": string - The code. Can be set in the build interface using the Code text field.

Special names

If a code property is named after one of these names, it will automatically be used for a specific purpose. It can still be called or interpolated manually.

on_init (realm property)
Called once when a new instance of the realm is created.
on_wake (realm property)
Called when the instance is awoken, i. e. if a player accesses it while it was asleep. Also called immediately after on_init at instance creation. The temporary variable _slept is available and contains a timestamp of when the instance went asleep, or None if it was never asleep before.
on_sleep (realm property)
Called when the instance properly goes asleep. This hook is somewhat unreliable, as it is not called after a server crash, where all instances are put to sleep. Thus it may be possible that two consecutive on_wake calls occur.
on_enter (realm or instance property)
Called when a player leaves the location the property is in, or any location in the instance if it is a realm property. The two temporary variables _from and _to are available, which contain the player's old and new location, respectively. _from will be None if the player moved in using a portal or opened the browser page.
on_leave (realm or instance property)
Similar to on_enter, but called when a player leaves the location the property is in, or any location in the instance if it is a realm property. The two temporary variables _from and _to are available, which contain the player's old and new location, respectively. _to will be None if the player moved out using a portal or closed the browser page.