- Home /
Server-side game "plugins"
Hello, I am currently making a multiplayer server-authoritative survival game. I was wondering how I would go about making the game so server owners/developers could make their own scripts to hook into the server's build (think uMod for Rust). I would include a compiler so the users would only need to drop source code into a folder and it would compile it and everything. I am more confused, however, on how I would let these scripts communicate with the base game. I would want it to be like the Spigot API for Minecraft, where the base game has events that the scripts could hook into, ex. PlayerInteractEvent when interacting with a door, this event would include the player, door, stuff like that, when the script hooks into this it would be able to access all of this but could also cancel this event from happening or call its own events. I'm sorry if this doesn't make sense and I would be more than happy to clarify, but I was wondering if anyone could point me in the right direction or direct me to an example of this being done in Unity/C#. Thank you for your time.
Answer by Bunny83 · Jun 22, 2019 at 03:21 PM
Well, you have to design your own Plugin API. How it should look like, what features it should support is completely up to you and your game. Minecraft is already built upon various events. Most "framework mods" (like Forge) will actually alter the code an put special handling code in between the default code.
Spigot seems to just cancel the default behaviour but still pass the event to all plugins. What behaviour you want if multiple plugins want to handle the same event is also up to you.
In general you would have to route all your relevant calls through a construct like this:
public class ModEvent
{
public System.Action<Parameters> defaultAction;
public List<System.Func<Parameters>> plugins = new List<System.Func<Parameters>>();
public ModEvent(System.Action<Parameters> action)
{
defaultAction = action;
}
public void Execute(Parameters aParams)
{
bool executeDefault = true;
foreach(var action in plugins)
{
if (!action(aParams))
executeDefault = false;
}
if (executeDefault)
defaultAction(aParams);
}
}
This is just a rough example. For any event in your game you can create an instance of the ModEvent class which will handle the execution of the default action. For example
public ModEvent primaryFire = new ModEvent();
private void DefaultPrimaryFire(Parameters aParams)
{
// your default action
}
public void ExecutePrimaryFire(Parameters aParams)
{
primaryFire.Execute(aParams);
}
A plugin can now simply register a callback for the event primaryFire event by adding a method to it
yourClass.primaryFire.plugins.Add(PrimaryFireCallback);
bool PrimaryFireCallback(Parameters aParams)
{
// do something
return true; // should the default action be executed or not?
}
Again this is just a very basic example. You may want to be more thoughtful how you design the events, how you pass and handle parameters, how you may chain callbacks if necessary, ...
Holy crap, I wasn't expecting this much detail. Thank you so much for the reply, I think this will really help me out!
Your answer
Follow this Question
Related Questions
MMO server side programming 0 Answers
Photon Server vs Dedicated Master Client with PUN 1 Answer
Unity networking tutorial? 6 Answers
[Multiplayer] How to make the client get an attribute calculate by the server? 0 Answers
UNET OnCollisionEnter issues 0 Answers