Synapse
Search…
⌃K

Components

All the Functions are still the same but this was written for Synapse 1 so some parts of the Code regarding to Synapse does no longer work
Component is a Unity Feature and it's really useful.
If you dont use the NuGet package: You will need if you want to use them, add UnityEngine.CoreModule.dll to your project dependencies. You can find this file in your Server Directory.
Components are a part of an GameObject and any class can be added as an Component to an existing GameObject like f.e. the Player.
To add a Component to a GameObject you may use the method GameObject.AddComponent<Class>();. To get a Component from an GameObject, you may use GameObject.GetComponent<Class>().
Note: The Class must inherit from MonoBehavoir
In these Components you can store variables foreach Player or execute methods.
Extra: We have the Event LoadComponentsEvent which activates when all the other Components of a Player gets added
Example:
using Synapse;
namespace Example
{
public class Example : Synapse.Plugin
{
public override void OnEnable()
{
//As Soon as the Player join and his Components can be added start the method LoadComponents
Events.LoadComponentsEvent += LoadComponents;
}
private void LoadComponents(Synapse.Events.Classes.LoadComponentsEvent ev)
{
//Check if the Component somehow already exist
if (ev.Player.GetComponent<ComponentExample>() == null)
//If not add the Component to the player
ev.Player.AddComponent<ComponentExample>();
//Change The value for this One specific Player
ev.Player.GetComponent<ComponentExample>().my_Variable_i_want_to_store = "Yea i can change it!";
//Start this method for this One specific Player
ev.Player.GetComponent<ComponentExample>().SayMyVariable();
}
public override string GetName => "YourPluginName";
}
public class ComponentExample : MonoBehaviour
{
//A Variable every Player gets when he joins
public string my_Variable_i_want_to_store;
// A Method which is called when the Components gets added
public void Awake()
{
my_Variable_i_want_to_store = "Hello World";
Log.Info($"My Awesome Component from Player : {this.gameObject.GetPlayer().NickName} was added :D");
//Hook a Event in this Player
Events.ConsoleCommandEvent += OnCommand;
}
private void OnCommand(ref ConsoleCommandEvent ev)
{
//Checks if the Command Author is the Player of this Component
if (ev.Player == this.gameObject.GetPlayer())
{
//Return the Value of this Component
if (ev.Command.ToLower() == "myvalue")
ev.ReturnMessage = my_Variable_i_want_to_store;
}
}
//A Method which will activate when the Components get Removed (player leave in this case most likely)
public void OnDestroy()
{
Log.Info($"My Awesome Component from Player : {this.gameObject.GetPlayer().NickName} was removed D:");
//Remove the Event so that no Errors will occure because this object does no longer exist
Events.ConsoleCommandEvent -= OnCommand;
}
//A Method which gets the gameobject from this Component to find out the name and say something in the Console
public void SayMyVariable()
{
Log.Info($"{this.gameObject.GetPlayer().NickName} want to say: {my_Variable_i_want_to_store}");
}
}
}
Last modified 1yr ago