Modding:Moddable Mod: Difference between revisions

From Vintage Story Wiki
mNo edit summary
mNo edit summary
Line 137: Line 137:


         List<Tip> tips = new List<Tip>();
         List<Tip> tips = new List<Tip>();
         int tipInterval = 1000;
         double tipInterval = 10;
 
         public override void StartServerSide(ICoreServerAPI api)
         public override void StartServerSide(ICoreServerAPI api)
         {
         {
Line 150: Line 149:
         {
         {
             int tipCount = tips.Count();
             int tipCount = tips.Count();
            // Stop if there are no tips
            if (tipCount == 0) return;


             // Select a random number in the range of [0-1]
             // Select a random number in the range of [0-1]
Line 158: Line 160:
             Tip tip = tips[randomTip];
             Tip tip = tips[randomTip];


             api.SendMessageToGroup(0, tip.ToString(), EnumChatType.AllGroups);
             api.SendMessageToGroup(GlobalConstants.GeneralChatGroup, tip.ToString(), EnumChatType.AllGroups);
         }
         }


Line 167: Line 169:
     }
     }
</syntaxhighlight>
</syntaxhighlight>
For testing purposes, we set the interval to be very short (10 seconds). Feel free to change this accordingly.


Remember, both <b>classes and methods which other mods will interact with have to be declared public.</b>
Remember, both <b>classes and methods which other mods will interact with have to be declared public.</b>
In C#, classes and methods are not public by default.
In C#, classes and methods are not public by default. You can read more about this here (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers)
You can read more about this here (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers)


We can now compile our mod, add it to our VintageStory mod folder, and test it ingame. If the /commandhere prints out a message, we are ready
We can now compile our mod, add it to our VintageStory mod folder, and test it ingame. If the /commandhere prints out a message, we are ready
Line 176: Line 178:
== Setting up SpawnMod ==
== Setting up SpawnMod ==


Before we get started, we have to add a reference to TipMod. You can either reference the uncompiled project,
Before we start interacting with TipMod, let's setup SpawnMod. This involves registering a simple command which teleports the player to spawn.
<syntaxhighlight lang="c#">
    class SpawnMod : ModSystem
    {
        public override void StartServerSide(ICoreServerAPI api)
        {
            base.StartServerSide(api);
 
            api.RegisterCommand("spawn", "Teleport to spawn", "", OnCmdSpawn);
        }
 
        private void OnCmdSpawn(IServerPlayer player, int groupId, CmdArgs args)
        {
            player.Entity.TeleportTo(player.SpawnPosition);
        }
    }
</syntaxhighlight>
SpawnMod is ready to be tested. When added to the mods folder, the command /spawn should now teleport us to spawn.
We've setup the base of our mods, now let's make SpawnMod interact with TipMod.
 
== Mods interacting ==
Before we get started, we have to add a reference to TipMod. You can either reference the project itself,
or the compiled .dll file. If you are using Visual Studio, in your Solution Explorer, directly under your project,
or the compiled .dll file. If you are using Visual Studio, in your Solution Explorer, directly under your project,
right-click "References" and select "Add Reference".
right-click "References" and select "Add Reference".
Line 183: Line 206:


Next, we do the following
Next, we do the following
* Register a command for teleporting to spawn
* Add a using statement for tipmod.src
* Add the using statement for TipMod
* Retrieve TipMod through the ModLoader by passing it's type
* Access the ModLoader and retrieve TipMod
* Call the method provided by TipMod and add a variety of tips
* Call the method provided by TipMod to add our own tip


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
    class SpawnMod : ModSystem
    {
         public override void StartServerSide(ICoreServerAPI api)
         public override void StartServerSide(ICoreServerAPI api)
         {
         {
Line 199: Line 219:
             TipMod tipMod = api.ModLoader.GetModSystem<TipMod>();
             TipMod tipMod = api.ModLoader.GetModSystem<TipMod>();
             tipMod.AddTip(new Tip("codemeister32", "To quickly return to spawn, type /spawn"));
             tipMod.AddTip(new Tip("codemeister32", "To quickly return to spawn, type /spawn"));
            tipMod.AddTip(new Tip("codemeister32", "Can't find your way home? Type /spawn"));
            tipMod.AddTip(new Tip("codemeister32", "Being chased by wolves? Quick, type /spawn"));
         }
         }
</syntaxhighlight>


        private void OnCmdSpawn(IServerPlayer player, int groupId, CmdArgs args)
We're now ready to test our mods. After placing both our compiled mods in the mods folder, we should be seeing a random tip pop up in the chat every 10 seconds. If no tips are showing up, check the log files for any errors. Don't forget that both our mods have to be compiled and present in the mod folder for them to work.
        {
            player.Entity.TeleportTo(player.SpawnPosition);
        }
    }
</syntaxhighlight>


Let's compile our mod and try it ingame.
If we call /spawn and are teleported to spawn, our mod has been added successfuly. Next,
we can try calling /commandhere to see if SpawnMod successfuly described itself to TipMod.


- img -
- img -
Line 218: Line 233:




== Making SpawnMod not depend on InfoMod ==
== Making SpawnMod not depend on TipMod ==


add throw statement here
add throw statement here