-
Notifications
You must be signed in to change notification settings - Fork 1
Components
Component are a Unity Feature and is really useful.
If you dont use the Nuget package you need to use them add UnityEngine.CoreModule.dll to your project dependencies.(This file is in the same Directory than the assembly-CSharp.dll)
Components are a Part of a gameobject and any class can be added as component to a existing gameobject like the Player.
To add a component to a Gameobject you can use the method gameobject.AddComponent<Class>(); to get a Component from a gameobject you can use gameobject.GetComponent<Class>().
Note: The Class must inheritance from MonoBehavoir
In this Components can you Store Variables for each 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}");
}
}
}