Anonymous

Mod Creation/Essentials: Difference between revisions

From Melvor Idle
no edit summary
No edit summary
Line 259: Line 259:
   });
   });
}</nowiki>
}</nowiki>
== Registering Game Objects ==
''View this topic's relevant API reference here [[Mod Creation/Mod Context API Reference#Game Object Registration]].''
Mods can now register or modify game objects (items, skills, pages, etc.) in a streamlined way. The entry point for doing so is the <code>gameData</code> endpoint within the mod context API. There is massive variety on what data is needed between different game object types but the general concept is the same. You will need to either define a data package using JSON and load that into the game, or you can dynamically build one via code (certain dynamic objects like skills requires the latter).
=== Defining a Data Package ===
The first, simpler option for building game object data is defining all (or as much as possible) data in a <code>.json</code> file that is then read into the game to register your game objects.
'''Pros'''
* Simpler
* More easily separate data from your mod's logic
* Your text editor can provide typing by defining the <code>$schema</code> property in your JSON file
'''Cons'''
* Does not support all game object types, such as skills
To begin with this approach, your JSON files should all be constructed with:
<nowiki>{
  "$schema": "https://melvoridle.com/assets/schema/gameData.json",
  "data": {
  }
}</nowiki>
If you're using a text editor that supports it, you should now get autocomplete and type checking on the fields you create.
Here is an example of defining an item:
<nowiki>{
  "$schema": "https://test.melvoridle.com/dlcPrep/assets/schema/gameData.json",
  "data": {
    "items": [{
      "id": "Wooden_Dagger",
      "name": "Wooden Dagger",
      "category": "Combat",
      "type": "Weapon",
      "media": "wooden-dagger.png",
      "ignoreCompletion": false,
      "obtainFromItemLog": false,
      "golbinRaidExclusive": false,
      "sellsFor": 0,
      "tier": "wooden",
      "validSlots": ["Weapon"],
      "occupiesSlots": [],
      "equipRequirements": [
        {
          "type": "SkillLevel",
          "skillID": "melvorD:Attack",
          "level": 1
        }
      ],
      "equipmentStats": [
        { "key": "attackSpeed", "value": 2200 },
        { "key": "stabAttackBonus", "value": 4 },
        { "key": "slashAttackBonus", "value": 1 },
        { "key": "blockAttackBonus", "value": 4 },
        { "key": "meleeStrengthBonus", "value": 1 }
      ],
      "itemType": "Weapon",
      "attackType": "melee"
    }]
  }
}</nowiki>
You would then register your game data using the following:
<nowiki>// setup.mjs
export async function setup({ gameData }) {
  await gameData.addPackage('path-to-your-data.json');
}</nowiki>
=== Building a Data Package at Runtime ===
The other option for building game object data is doing so dynamically through the mod context API.
'''Pros'''
* Can be used to register any type of game object
* Enables the ability to dynamically build game objects
'''Cons'''
* Messier and more complex
* No type support at the moment
The entry-point for using this approach looks like this:
<nowiki>// setup.mjs
export function setup({ gameData }) {
  gameData.buildPackage((p) => {
    // use the `p` object to add game objects
  }).add();
}</nowiki>
Following the same example above of adding an item:
<nowiki>// setup.mjs
export function setup({ gameData }) {
  gameData.buildPackage((p) => {
    p.items.add({
    id: 'Wooden_Dagger',
    name: 'Wooden Dagger',
    category: 'Combat',
    type: 'Weapon',
    media: 'wooden-dagger.png',
    ignoreCompletion: false,
    obtainFromItemLog: false,
    golbinRaidExclusive: false,
    sellsFor: 0,
    tier: 'wooden',
    validSlots: ['Weapon'],
    occupiesSlots: [],
    equipRequirements: [{
      type: 'SkillLevel',
      skillID: 'melvorD:Attack',
      level: 1
    }],
    equipmentStats: [
      { key: 'attackSpeed', value: 2200 },
      { key: 'stabAttackBonus', value: 4 },
      { key: 'slashAttackBonus', value: 1 },
      { key: 'blockAttackBonus', value: 4 },
      { key: 'meleeStrengthBonus', value: 1 }
    ],
    itemType: 'Weapon',
    attackType: 'melee'
  }).add();
}</nowiki>
Your game data should already be registered from the <code>.add()</code> method being called on your built package.


== Mod Settings ==
== Mod Settings ==
91

edits