Translations:Modding:Code Tutorial Simple Block/25/en

From Vintage Story Wiki
Revision as of 15:25, 30 May 2024 by FuzzyBot (talk | contribs) (Importing a new version from external source)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In the Start function, you need to make a call to the RegisterBlockClass function in the API class. Add the following code on a new line inside of the Start function's block:

api.RegisterBlockClass();

You'll notice that, currently, this results in an error. Hover over the 'RegisterBlockClass' with your mouse and it will show you a breakdown of the function.

VSBlockTutorialRegisterBlockClassFunction.png

This tells you the function's parameters and a description of what it does. Not all functions in the code have descriptions attached to them, however as the modding API becomes more documented, these will hopefully become more populated. In this particular case, you need to pass in a class name, and a block type to the function.

The class name argument should be fully lowercase, and should generally be your mod ID, joined with the name of the class you just made. The blockType will be the class you just made. Replace the function with the following:

api.RegisterBlockClass(Mod.Info.ModID + ".trampoline", typeof(BlockTrampoline));

This will register the BlockTrampoline class with the name "vstutorial.trampoline".

Why include the mod id here?
If you've made code mods before the introduction of these new tutorials, you probably haven't included your mod ID when registering classes. As more and more mods are being made, there are occasional collisions in regards to class names. Say, for example, two mods both add in a 'trampoline' block class. Only one instance of the trampoline class will exist, which can cause issues if they had slightly different functionality.

So, including your mod ID when registering classes will ensure that these 'collisions' do not occur.