Modding:Code Tutorial Simple Item: Difference between revisions

From Vintage Story Wiki
m
no edit summary
mNo edit summary
mNo edit summary
Line 48: Line 48:
Create your new class. Right click the new folder, hover over ''Add'', and select ''Class''. Ensure you have the standard ''Class'' selected in the template list, enter your class name, and click create.
Create your new class. Right click the new folder, hover over ''Add'', and select ''Class''. Ensure you have the standard ''Class'' selected in the template list, enter your class name, and click create.


=== Registering the Block Class ===
The class you created needs to extend the 'Item'  
The class for the trampoline class is created, however before it can be used it needs to be registered through the game API. When you created your mod, a ''mod system'' will have been automatically made. Look for it in your solution explorer, it should be called ''VSTutorialModSystem'', and double click it to open it.


Your mod system will likely contain three functions at this point - ''Start, StartServerSide,'' and ''StartClientSide.'' The server and client side functions are not required here, so feel free to delete them or keep them in for the future. Either is fine.
=== Registering the Item Class ===
The class for the thorns blade is created, however before it can be used it needs to be registered through the game API. In your mod system, which you used in the previous tutorial, you need to register an item class for both the client and server.


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:<syntaxhighlight lang="csharp">
In the ''Start'' function, make a call to the RegisterItemClass function in the API. Add the following code on a new line inside of the ''Start'' function's block:<syntaxhighlight lang="csharp">
api.RegisterBlockClass();
api.RegisterItemClass();
</syntaxhighlight>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.
</syntaxhighlight>Similarly to the previous tutorial, this will result in an error. You'll need to add some parameters to the function. Don't forget, you can hover over the function with your mouse and the required parameters will be shown. In this case, we once again need a class name, and a type.
[[File:VSBlockTutorialRegisterBlockClassFunction.png|center|frameless|659x659px]]
Your 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 itemType will be the class you just made. Replace the function with the following:<syntaxhighlight lang="csharp">
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.
api.RegisterItemClass(Mod.Info.ModID + ".thornsblade", typeof(ItemThornsBlade));
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:<syntaxhighlight lang="csharp">
</syntaxhighlight>This will register the ItemThornsBlade class with the name "vstutorial.thornsblade".
api.RegisterBlockClass(Mod.Info.ModID + ".trampoline", typeof(BlockTrampoline));
</syntaxhighlight>This will register the BlockTrampoline class with the name "vstutorial.trampoline".
{| class="wikitable mw-collapsible mw-collapsed"
{| class="wikitable mw-collapsible mw-collapsed"
|Why include the mod id here?
|Why include the mod id here?
Line 68: Line 66:
|}
|}


=== Adding Block Class to Asset ===
=== Adding Item Class to Asset ===
Before your block class will work, you need to add a new property to the blocktype JSON asset. In the solution explorer, open the file at ''assets/vstutorial/blocktypes/trampoline.json.''  
Before your item will work, you need to add the class to the asset file. Open the file in ''assets/vstutorial/itemtypes/thornsblade.json''.


You need to add the following property to the file:<syntaxhighlight lang="json">
You need to add the following property to the file:<syntaxhighlight lang="json">
"class": "vstutorial.trampoline",
"class": "vstutorial.thornsblade",
</syntaxhighlight>Generally, this element is placed as the second property in your json file, immediately below the code property.
</syntaxhighlight>Generally, this element is placed as the second property in your json file, immediately below the code property.
Note that the value of this property is identical to the value we used in the ''RegisterBlockClass'' function. This is how the game links the JSON asset files to your code's registered classes.
Note that the value of this property is identical to the value we used in the ''RegisterItemClass'' function. This is how the game links the JSON asset files to your code's registered classes.


== Testing the Block Class ==
== Testing the Block Class ==
Confirmedusers
643

edits