Synapse
Search…
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:
1
using Synapse;
2
3
namespace Example
4
{
5
public class Example : Synapse.Plugin
6
{
7
public override void OnEnable()
8
{
9
//As Soon as the Player join and his Components can be added start the method LoadComponents
10
Events.LoadComponentsEvent += LoadComponents;
11
}
12
13
private void LoadComponents(Synapse.Events.Classes.LoadComponentsEvent ev)
14
{
15
//Check if the Component somehow already exist
16
if (ev.Player.GetComponent<ComponentExample>() == null)
17
//If not add the Component to the player
18
ev.Player.AddComponent<ComponentExample>();
19
20
//Change The value for this One specific Player
21
ev.Player.GetComponent<ComponentExample>().my_Variable_i_want_to_store = "Yea i can change it!";
22
//Start this method for this One specific Player
23
ev.Player.GetComponent<ComponentExample>().SayMyVariable();
24
}
25
26
public override string GetName => "YourPluginName";
27
}
28
29
public class ComponentExample : MonoBehaviour
30
{
31
//A Variable every Player gets when he joins
32
public string my_Variable_i_want_to_store;
33
34
// A Method which is called when the Components gets added
35
public void Awake()
36
{
37
my_Variable_i_want_to_store = "Hello World";
38
Log.Info(quot;My Awesome Component from Player : {this.gameObject.GetPlayer().NickName} was added :D");
39
40
//Hook a Event in this Player
41
Events.ConsoleCommandEvent += OnCommand;
42
}
43
44
private void OnCommand(ref ConsoleCommandEvent ev)
45
{
46
//Checks if the Command Author is the Player of this Component
47
if (ev.Player == this.gameObject.GetPlayer())
48
{
49
//Return the Value of this Component
50
if (ev.Command.ToLower() == "myvalue")
51
ev.ReturnMessage = my_Variable_i_want_to_store;
52
}
53
}
54
55
//A Method which will activate when the Components get Removed (player leave in this case most likely)
56
public void OnDestroy()
57
{
58
Log.Info(quot;My Awesome Component from Player : {this.gameObject.GetPlayer().NickName} was removed D:");
59
60
//Remove the Event so that no Errors will occure because this object does no longer exist
61
Events.ConsoleCommandEvent -= OnCommand;
62
}
63
64
//A Method which gets the gameobject from this Component to find out the name and say something in the Console
65
public void SayMyVariable()
66
{
67
Log.Info(quot;{this.gameObject.GetPlayer().NickName} want to say: {my_Variable_i_want_to_store}");
68
}
69
}
70
}
Copied!
Last modified 1yr ago
Copy link