Add Command Prefabs to GenvidSession

A GenvidSession object with child streams added.

Commands let you tell your game to do specific things from a separate website, such as an administrator site.

Unlike events, commands go directly from the website to your game without a MapReduce system. This makes it possible to send individual instructions to the game with minimal delay.

Caution

We recommend limiting commands to an administrator or similarly access-controlled page.

Engine Integration

A GenvidCommands object.
  1. Drag and drop a Commands prefab into GenvidSession as a child of the GenvidSession.

  2. The GenvidSession has a Commands section. Drag and drop your current Commands prefab into this field.

  3. Change the Ids Size to the number of game-data streams that you want to create.

  4. Open one element and give it a unique name in the Id field.

  5. In the script receiving the command, create a public function that takes the following parameters in this order:

    • A string.
    • A second string.
    • An integer pointer.
    Events with ID set
  6. Drag and drop the object with your script into the On Command Triggered box.

    Events with ID set
  7. From the dropdown list displaying No Function, select the game script and function you created.

  8. In your public function you can use the information received from the command:

    • First String: Id of the command.

      Note: You’ll use this Id later when setting up your Commands configuration and website calls.

    • Second String: Results of the command.

    • IntPtr: Specific pointer used for the callback.

    The public function tells your game how to interpret commands sent from an external website. Commands are generally used for administrative functions and what they do is completely up to you. You can see examples of commands on the sample admin website.

Website Integration

  1. Open the adminUnity.ts file available in the webpublic folder.

  2. Create a function to send the command.

  3. In this function, create a variable that contains an ICommandRequest.

    1. Set the id to the name of the command.

    2. Set the value to the content of the command.

      let commandToSend: ICommandRequest = {
          id: "CommandName",
          value: "CommandContent"
      };
      
  4. Create a post web-request to /api/admin/commands/game and send the command you created.

    let promise = $.post("/api/admin/commands/game", commandToSend).then(() => {
        //web request done properly
    });
    
  5. Open the admin.html file available in the webpublic folder.

  6. Create a button in the page to use your function.

    <button class='myCommandButton'>Click to trigger command</button>
    
  7. Associate the function with the button. For example, this accesses the button and adds the function created previously to it.

    let commandButton = <HTMLButtonElement>document.querySelector("." + "myCommandButton");
     commandButton.addEventListener("click", (_event) => { this.sendCommandFunction(commandToSend); }, false);
    
  8. Open a command line prompt and go to the Unity sample folder.

  9. Run py unity.py build web to rebuild the website.

Now you should be able to use command from the website when the stream is running.