Skip to content

3. Extensions

libertylocked edited this page Dec 3, 2021 · 10 revisions

Writing extensions for WebScriptHook

Porting a ScriptHookV .NET script to a WebScriptHook extension

  • Instead of inheriting from GTA.Script, inherit from WebScriptHook.Extensions.Extension
  • Override public override object HandleCall(object[] args). This method will be called whenever client sends an input to your extension
  • Implement ITickable interface. Replace your Tick event handle void OnTick(object sender, EventArgs e) with a public void Tick()
  • If your plugin handles KeyDown event, you won't be able to do so as a WSH extension
  • Check for key presses in Tick, with Game.IsKeyPressed
  • Use remote inputs with HandleCall instead of keyboard inputs

Calling another extension

  • You can call another extension within your extension
  • Call this.CallExtension(string targetID, object[] args) within your extension
  • Example for a simple relay extension that relays the messages from and to another extension
public override object HandleCall(object[] args)
{
    return CallExtension("MyAssembly.MyNamespace.MyExtension", args);
}

The front-end for WebScriptHook extensions

Send inputs and receive responses

  • You can POST to /input with the following JavaScript code
  • In this example, my extension assembly is extension-examples.dll and my extension class is ExtensionExamples.ClockPlugin
  • {"on"} array will be sent to the extension
// Turn the clock display on
$("#btnOn").click(function() {
  sendInput("extension-examples.ExtensionExamples.ClockPlugin", "on", function(data) { window.alert(data); }, null);
});

function sendInput(extensionId, args, callback, errorCallback) {
  var stringified = JSON.stringify({ "Cmd": "extension", "Arg" : extensionId, "Args" : args });
  $.ajax({
    url: '/input',
    type: 'post',
    dataType: 'json',
    success: null,
    data: stringified,
    timeout: 2000,
    success: callback,
    error: errorCallback,
  });
}