Finding events
Unturned Plugin Development #7
MCrow • 3 months ago
MCrow • 3 months ago
This episode will cover the techniques to find events in the game that we can use in our plugins. I will show you how I do this using two ways: using the Unturned DataMining GitHub repository and using the JetBrains DotPeek tool.
RocketMod has built in events that you can use in your plugins.
Basic events like player connected, disconnected and server shutdown.
U.Events.OnPlayerConnected += OnPlayerConnected;
private void OnPlayerConnected(UnturnedPlayer player)
{
Logger.Log($"{player.DisplayName} connected.");
}
Player events like player death, damage, chat, etc.
UnturnedPlayerEvents.OnPlayerDeath += OnPlayerDeath;
private void OnPlayerDeath(UnturnedPlayer player, EDeathCause cause, ELimb limb, CSteamID murderer)
{
Logger.Log($"{player.DisplayName} died of {cause}");
}
💡 PRO TIP
In Visual Studio events are marked with âš¡ lightining icon
Although UnturnedPlayerEvents
has many events it is far from complete. Most events are in the original SDG.Unturned
types.
I'm gonna list few examples and then show how you can find them.
BarricadeManager.onDamageBarricadeRequested += OnBarricadeDamageRequested;
private void OnBarricadeDamageRequested(CSteamID instigatorSteamID, Transform barricadeTransform, ref ushort pendingTotalDamage, ref bool shouldAllow, EDamageOrigin damageOrigin)
{
// Get the player instance
Player player = PlayerTool.getPlayer(instigatorsteamid);
// Check if instigator is actually a player. It can be zombie.
if (player == null)
{
return;
}
// Get Rocket UntunredPlayer instance if necessary
UnturnedPlayer unturnedPlayer = UnturnedPlayer.FromPlayer(player);
// Do something
}
💡 PRO TIP
You can subscribe to delegate fields that are not events. They are not marked with the âš¡ icon in Visual Studio.
Level.onLevelLoaded += OnLevelLoaded;
private void OnLevelLoaded(int level)
{
Logger.Log($"Level {level} loaded.");
}
UseableGun.onBulletHit += OnBulletHit;
private void OnBulletHit(UseableGun gun, BulletInfo bullet, InputInfo hit, ref bool shouldAllow)
{
Logger.Log($"Bullet hit {hit.point}");
}
There is a great repository on GitHub called Unturned Datamining created and maintained by DiFFoZ
It automatically updates every time a new version of Unturned is released. It contains decopmiled code of the game. You can use it to find events and learn how the game works.
You will find all important Unturned types in Assembly-CSharp/SDG.Unturned
directory. In there is a file called 0README.md
that contains a list of all types in the game. If you are working on something related to barricades, you can search for keywords like Barricade
in the file (ctrl + f) and find all types that contain the keyword.
Despite being archived and no longer maintained, dnSpy is the best tool I use personally to explore the game code.
Here's how I do it:
Assembly-CSharp.dll
in the game directory (usually Steam/steamapps/common/Unturned/Unturned_Data/Managed
)SDG.Unturned
namespace then click on iton
prefix.Finding events in the game is harder than it should be and in the future I will look into creating a tool or maintaining a documentation to help with this. For now, you can use the methods I showed you in this episode.