This project has moved and is read-only. For the latest updates, please go here.

Using methods in a script

Jun 24, 2016 at 9:56 AM
Hey everyone.

I'm working on a game server for a game client and I'm using ClearScript as my scripting interface.

I have an abstract class called 'ScriptBase' and classes that inherit from it: 'NpcScript', 'PortalScript' and 'ReactorScript'.

'ScriptBase' contains all the necessary fields and methods like the script's engine, the 'Run' method and everything else.

'NpcScript', 'PortalScript' and 'ReactorScript' are basically the implementation of each game object's script. So, when I interact with a NPC for example, I run a new instance of NpcScript like so:
new NpcScript(104000).Run();
The 'ScriptBase' class does all the work - it looks for the appropriate file and executes it. However, I want to be able to include methods to expose to the engine from within each class. In 'NpcScript' class, for example, I have a few methods: 'SendNext(string text)', 'SendYesNo(string text)' - how would I expose them to the engine if 'ScriptBase' is abstract so I can use them like so (in a script file):
Jun 24, 2016 at 4:29 PM
Hello, Fraysa!

There are many ways to do this. Here's one possibility:
public abstract class ScriptBase {
    protected ScriptEngine _engine { get; private set; }
    private string _code;
    public virtual void Run() {
    /* ... */
public class NpcScript : ScriptBase {
    private void SendNext(string text) { /* ... */ }
    private void SendYesNo(string text) { /* ... */ }
    public override void Run() {
        _engine.Script.SendNext = new Action<string>(SendNext);
        _engine.Script.SendYesNo = new Action<string>(SendYesNo);
    /* ... */
This is just one possibility, but hopefully it gives you an idea of what's possible.

Good luck!