cleolibrary / CLEO-Redux

Experimental JavaScript runtime for GTA 3D era games/GTA IV/Bully
https://re.cleo.li
Other
214 stars 20 forks source link

Type "label" in opcodes input #94

Closed TheCaJlaT closed 1 year ago

TheCaJlaT commented 1 year ago

Faced with the problem of registering commands in SA:MP, by calling the SAMPFUNCS opcode, you need to pass a label to it, but there are no labels in JS, as in cleo. I tried to pass the function there, but I got an error: [ERROR] Unknown argument type function in command sampRegisterChatCommand Please tell me what needs to be passed to the opcode so that everything works? Opcode: https://wiki.blast.hk/gta-sa/opcodes/0B34 My code:

import { Samp } from "./includes/Samp";
while(true) {
    wait(0);
    if(!Samp.isAvailable()) continue;
    Samp.registerChatCommand('test', () => {
        Samp.addChatMessage('TEST', 0xFFFFFF);
    });
    Samp.addChatMessage("SA:MP CLEO-Redux loaded", 0xFF0000);
    while(true) {
        wait(0);
    };
};

Opcode in sa.json: image Opcode in ./includes/Samp.js: image

x87 commented 1 year ago

Labels (or offsets within compiled script) simply don't exist in JS. It will never work, no matter what you put in sa.json.

To provide this functionality in JavaScript one should convert those commands into CLEO Redux using its own SDK. You can look here for the first step https://re.cleo.li/docs/en/events.html#creating-your-own-events

In short, registerChatCommand should use CLEO Redux SDK, and when the command 'test' is called in the game it should call TriggerEvent with some predefined name, e.g. "OnChatCommand".

cleo_redux_sdk::trigger_event("OnChatCommand", '{"chatCommand": "test"}';

On JS side, a script should subscribe to such event using built-in addEventListener function:

addEventListener("OnChatCommand", (event) => {
  const chatCommand= event.data.chatCommand;
  log(`Command ${chatCommand} is called`); // logs "Command test is called"
})