Skip to content

Create a Script

Kiki edited this page Dec 25, 2024 · 27 revisions

To create a new script, you only need to instantiate the Scriptor class:

var _script = new Scriptor(_name, _script_source_code, [_owner]);

Each script can be bound to an owner at the time of creation (or rebound later during execution, see below). The owner can be any struct, class, or game object. Within the script, the owner is accessible as self., meaning the script behaves as if it were "a part of the owner".

Tip

Assign a meaningful name to your script to make it easier to identify where compile-time or runtime errors occur.

By default, a script is not automatically compiled when created. However, you can compile it immediately by calling .compile() on the constructor. This is a builder function and returns self, allowing for create-and-compile in one step:

var _script = new Scriptor(_name, _script_source_code, [_owner]).compile();

Best Practice

It is recommended to bulk-create and compile your scripts during game startup, such as on the initial loading screen.

  • Reason: Scriptor is optimized for runtime performance, meaning the complex calculations and interpretation occur during the compile step. This requires more resources during compilation but ensures lightning-fast execution later.

  • Implementation: Compile your scripts during game startup and simply call .run() when needed during gameplay.

Rebinding the Owner

In games with many similar objects (e.g., bullets, cards, NPCs), creating and compiling hundreds of identical scripts can be inefficient. Scripts execute synchronously within a single frame, like regular functions. Therefore, you can rebind a script to a new owner dynamically by specifying the owner when calling the run() method:

_script.run([_owner]);
  • If the script hasn’t been compiled yet, it will be compiled before execution.

  • Calling run() without arguments re-executes the script on the current owner.\

  • To rebind the script to a new owner, call run(_owner).\

Note

If your script doesn’t reference self. (the owner), no rebinding is necessary

Running a script without an owner

You can choose to ignore the owner of a script entirely, but in doing so, you lose access to self..

This is suitable for scripts that act globally or perform tasks not tied to a specific object. Just keep in mind that any references to self. in such scripts will not work.

Update/Change the Source Code of a Script

In certain scenarios, you might need to modify the source code of an already compiled script (e.g., based on player actions or dynamic game situations).

You don’t need to create a new scriptor instance; instead, update the code in the existing instance (so your pointers to the script won't break).

_script.update_source_code(_new_source);

Note

  • This method triggers a recompile the next time the script is executed with .run().\
  • The update_source_code method is chainable, so you can call it along with .update_source_code(...).compile() immediately:
Clone this wiki locally