3. Implement Custom Command
Introduction
Let's continue to explore the customizability of Diarkis by implementing a custom command.
In this tutorial, we will implement the following command:
A command to attack enemies in a Room
Command version: 2 (versions 0 and 1 are used for built-in commands)
Command ID: 300
Request: Send a command specifying an attack type (type) (1: Melee, 2: Ranged)
Response: Return who dealt damage, the damage value, and the total damage value
Push notifications to all room members
The implementation flow is as follows:
Write code to generate a payload using puffer (a data serializer provided by Diarkis)
Implement the custom command on the server
Implement the command in the test client
Puffer Module (Data Serializer provided by Diarkis)
Puffer is a tool that generates code to serialize and deserialize data from JSON.
The output supports C++, C#, and Go, simplifying packet data implementation between clients developed in Unreal or Unity and servers developed in Go.
For more details on Puffer, please refer to the API reference.
https://docs.diarkis.io/docs/server/v1.0.0/diarkis/puffer/index.html
Creating a JSON File for Custom Command Data Definition
💾 Add ./puffer/json_definitions/custom/attack.json.
attack is the data used for the custom command's request.
type: The attack type can be stored as a uint8
attackResult is the data used for responses and push notifications to room members.
type: The attack type can be stored as a uint8
uid: Who dealt the damage. Stored as a string
damage: Damage value. Stored as uint16
totalDamage: Total damage value. Stored as uint16
Code Generation
Execute the following command to generate code. The code will be output to the directory of each language under puffer.
Implementing the Attack Command Handler on the Server
Let's implement the attack command on the server.
💾 Add ./cmds/custom/attack.go.
Comments within the code explain what is being implemented, so please read them as well.
Publishing the Attack Command
Once the command is implemented, it needs to be published externally.
Edit ./cmds/custom/main.go and add the following.
By adding diarkisexec.SetServerCommandHandler() to Expose(), publish the attack command.
Adding the Attack Command to the Test Client
To execute the server's attack command, add the command to the test client.
Add ./testcli/handson/attack.go.
This includes setup to execute the client, sending the command to the server with Attack(), handling the response with onResponse(), and handling push notifications with onPush().
Register the handson attack command to the test client.
Edit ./testcli/main.go.
You can register a new command to the test client with cli.RegisterCommands(string, []cli.Command).
Generate binary and restart the udp server
Run make build-local again to generate the binary.
The server and test client will be updated.
If no errors are found, stop and restart the udp server.
Verifying the attack command
After room create and room join, issue the handson attack command and confirm that the damage is accumulated.
Execute handson attack with uid: test1
handson attack with uid: test1Receive push notification of attack result with uid: test2
Last updated
Was this helpful?

