Add Moves - How to Add a New Type
parent
d8abd035e2
commit
0f7ac6c72d
1 changed files with 50 additions and 0 deletions
50
Moves - How to Add a New Type.-.md
Normal file
50
Moves - How to Add a New Type.-.md
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
1. In the folder _/scripts/objects/moves_, create a new .cs file corresponding to the proper type of move (for example, PhysicalMove.cs).
|
||||
2. Within the constructor, inherit the original Move class, and override the string Type to set to the moves name (see example below).
|
||||
|
||||
```
|
||||
public class PhysicalMove : Move
|
||||
{
|
||||
public override string Type => "PhysicalMove";
|
||||
|
||||
public PhysicalMove(string name, string description, int power, string action) : base(name, description)
|
||||
{
|
||||
Action = action;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3. Next, within the MoveConverter.cs class (_/scripts/loaders/_), add an entry into the switch statement that matches the Type string that you set up in the class. In addition, set up a blank object as well (see example below).
|
||||
|
||||
```
|
||||
public class MoveConverter : JsonConverter<Move>
|
||||
{
|
||||
//Overrides the ReadJson function.
|
||||
public override Move ReadJson(JsonReader reader, Type objectType, Move? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
//Load the raw JSON object from the reader.
|
||||
JObject obj = JObject.Load(reader);
|
||||
|
||||
// Read the "Type" field
|
||||
string? type = obj["Type"]?.ToString();
|
||||
|
||||
//Create a temporary item object.
|
||||
Move move;
|
||||
|
||||
//Uses the Type field to determine what kind of object to create, to allow for a mixed list.
|
||||
switch (type)
|
||||
{
|
||||
case "PhysicalMove":
|
||||
move = new PhysicalMove("", "", 0, "");
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new JsonSerializationException($"Unknown item type: {type}");
|
||||
}
|
||||
|
||||
// Populate the item with values from JSON
|
||||
serializer.Populate(obj.CreateReader(), move);
|
||||
return move;
|
||||
}
|
||||
```
|
||||
|
||||
After doing so, you should be able to create and add Moves of that type, and have them inside of generic Move Lists as well, allowing for a mixed List of different kinds of Moves.
|
||||
Loading…
Add table
Reference in a new issue