Guide 2: Red Panda

This guide covers the basics of scripting and creating mobs. This guide assumes that you have already followed Guide 1: Golden Berry.

Step 0. Downloading the files

  • localization.tsv - The full localization file for ALL three guides (make sure you download this file as localization.tsv!)

  • RedPanda.png - Place this in the Icons folder of your mod. Icon drawn by lopidav

Step 1. Creating the card

Let’s set up the very basics of the card first. Create a red_panda.json file in the Cards/ folder, and add the following code:

2  "$schema": "../schemas/card.schema.json",
3  "id": "examplemod_redpanda",
4  "nameTerm": "examplemod_redpanda_name",
5  "descriptionTerm": "examplemod_redpanda_description",
6  "type": "Mobs",
7  "icon": "RedPanda.png",
8  "value": -1

The card should now show up in the debug menu:

../_images/red_panda_2.png ../_images/red_panda_1.png


Possible mistakes:

  • If the card name or description shows up as ---MISSING---, you are missing the localization.tsv file, or it didn’t get copied correctly

  • If the card doesn’t have an image, make sure the file name in the Icons folder matches the value of the "icon" property in your JSON

Step 2. Turning it into an animal

Currently our red panda doesn’t do anything, so let’s fix that by adding a script to it.

7  "value": -1,
8  "script": "Animal"

If we take a look a the code, we can see that the Combatable class (which Animal inherits from) has a field for combat stats, which specifies things like the max health, damage, attack speed, and more.

../_images/red_panda_3.png ../_images/red_panda_4.png

Let’s implement it with an underscore property:

 2  "$schema": "../schemas/card.schema.json",
 3  "id": "examplemod_redpanda",
 4  "nameTerm": "examplemod_redpanda_name",
 5  "descriptionTerm": "examplemod_redpanda_description",
 6  "type": "Mobs",
 7  "icon": "RedPanda.png",
 8  "value": -1,
 9  "script": "Animal",
10  "_BaseCombatStats": {
11    "MaxHealth": 10,
12    "AttackDamage": 3,
13    "AttackSpeed": 1,
14    "Defence": 1,
15    "HitChance": 0.9
16  }

Our red panda now has the specified stats:


Step 3. Scripting

Let’s give the red panda the ability to heal by feeding it with an apple. We are going to make a new class in the Mod.cs file for this, inside the ExampleModNS namespace. All of the explanations can be found in the comments.

If you are new to coding, you should read/watch a couple guides to learn the basics of C#. Such a guide is not in the scope of this wiki, but here are some good ones:

Make sure the following code is inside the ExampleModNS namespace:

 1// create a class called RedPanda which extends the Animal class
 2public class RedPanda : Animal
 4  // this method decides whether a card should stack onto this one
 5  protected override bool CanHaveCard(CardData otherCard)
 6  {
 7    if (otherCard.Id == "apple")
 8      return true; // if the other card is an apple, we will let it stack
 9    return base.CanHaveCard(otherCard); // otherwise, we will let Animal.CanHaveCard decide
10  }
12  // this method is called every frame, it is the CardData equivalent of the Update method
13  public override void UpdateCard()
14  {
15    // the ChildrenMatchingPredicate method will return all child cards (cards stacked on the current one) that match a given predicate function
16    // the given function checks if the card is an apple, so the apples variable will be a list of the apple cards on the red panda
17    var apples = ChildrenMatchingPredicate(childCard => childCard.Id == "apple");
18    if (apples.Count > 0) // if there are any apples on the red panda
19    {
20      int healed = 0; // create a variable to keep track of how much health the red panda gained
21      foreach (CardData apple in apples) // for each apple on the red panda
22      {
23        apple.MyGameCard.DestroyCard(); // destroy the apple card
24        HealthPoints += 2; // increase the red pandas health by 2
25        healed += 2; // keep track of how much it healed in total
26      }
27, Position); // play the eating sound at the red pandas position
28      WorldManager.instance.CreateSmoke(Position); // create smoke particles at the red pandas position
29      CreateHitText($"+{healed}", PrefabManager.instance.HealHitText); // create a heal text that displays how much it healed in total
30    }
31    base.UpdateCard(); // call the Animal.UpdateCard method
32  }

To make the red panda use this script, change the "script" in the JSON to ExampleModNS.RedPanda.

8"script": "ExampleModNS.RedPanda",

If you build your mod now, the red panda should have all the functionality of the script.