Compiler

This article describes the **Compiler**, which compiles the PBS files, and some events.

## The Compiler

The compiler is a script section that converts PBS files into .dat files, converts Trainers and Item balls into full events, and corrects events with updated code. Most of the PBS files have comma separated values and a few also have key value pairs.

One of the frequently used methods in the compiler is the CSV interpreter `pbGetCsvRecord`. It makes use of a string of characters that correspond to an array of default values. The characters defined to have a function are as follows:

Though these are already set up, some other methods are available and a few of the scripts recreate this one to use their own functions The unlisted function are as follows:

 csvFloat!
 csvEnumFieldOrInt!

## Editing a Field

There are three different types of PBS files, depending on how it organizes its data: Key Value Pairs, Comma Separated Values, and Sections. Key Value pairs are split up with regular expressions then the value is split up into an array by its commas. Comma Separated Values are purely that, and often use the character codes detailed above. Sections use Key Value pairs, where the value is a Comma Separated Value and each set is organized by an id. Each type of field is edited slightly differently. The following table organizes most of the PBS files by type.

 shadowmoves

### Editing Key Value Pairs

Key Value pairs are the easiest to edit and create, mostly because they treat all of their values exactly the same. One example in v16+ is in `pbCompileBerryPlants`.

```
 if line[ /^([^=]+)=(.\*)$/ ]
 key=$1
 value=$2
 value=value.split(",")
 for i in 0...value.length
 value[i].sub!(/^\s\*/){}
 value[i].sub!(/\s\*$/){}
 value[i]=value[i].to\_i
 end
 item=parseItem(key)
 sections[item]=value
 end

```

This is the important part, that does the actual interpreting. The very first line checks if the line even is a key value pair, by matching the regular expression to a string that ends with an equal sign, the equal sign itself, and all of the values after the equal sign. The two brackets save the key and the value to built-in variables, and value is turned into a string. Here, since all the values are just numbers, it just loops over each string in value, cleans up any extra spaces and saves it as an integer. The last two lines involve saving the key as an item instead, since these are berry plants, and the next adds the key value pair to the array that gets into `berryplants.dat`.

Key Value pairs are easily extended with additional values, as the game will access only the needed information while the game is running. To see how those are read by the game, look at `pbGetBerryPlantData` in `PField_BerryPlants`.

### Editing Comma Separated Values

Comma separated values are used in almost all of the compiler's scripts involving PBS files, and it is valuable to understand how they work. This example will be from `pbCompileMoves` as it uses `pbGetCsvRecord` and a large variety of the codes from above as well.

```
 record=pbGetCsvRecord(line,lineno,[0,"vnsxueeuuuxiss",
 nil,nil,nil,nil,nil,PBTypes,["Physical","Special","Status"],
 nil,nil,nil,nil,nil,nil,nil
 ])

```

The first thing to note, is the schema `"vnsxueeuuuxiss"`. It matches with the following arguments, which is in fact the default values. The `nil` values coincide with codes that don't require an argument, so the integers, strings, etc. The two "e"s match up with `PBTypes` and `["Physical","Special","Status"]` respectively. `PBTypes` is the representation of every single type that's defined, while the array will map to 0, 1, or 2 depending on what was written in that position. If you aren't using enums, you don't need the additional items in that array.

### Editing Sections

Sections are very similar to Comma Separated Values with a key difference, it stores the key in a hash map which has the array from above containing the schema. Every Section type has two hash maps right at the very top called `requiredtypes` and `optionaltypes`. Required types require those keys to be present in the section while optional types are optional. This does introduce a new feature, setting of the byte offset or array index when stored in a data file. This example is from `pbCompilePokemonData`, though some keys were trimmed.

```
requiredtypes={
 "Name"=>[0,"s"],
 "Kind"=>[0,"s"],
 "InternalName"=>[0,"c"],
 #[Trimmed]
 "EffortPoints"=>[23,"uuuuuu"],
 "Compatibility"=>[31,"eg",PBEggGroups,PBEggGroups],
 "Height"=>[33,"f"],
 "Weight"=>[35,"f"],
 "BaseEXP"=>[38,"w"],
}
optionaltypes={
 #[Trimmed]
 "Evolutions"=>[0,"\*ses",nil,PBEvolution],
 "Abilities"=>[2,"EG",PBAbilities,PBAbilities],
 #[Trimmed]
}

```

Pokemon data is one of those that has its own version of the `pbGetCsvRecord`. Some new codes that you might have not seen include the \* code, which repeats the following codes until it no longer fits the schema. The rest are mostly exclusive to this method and mainly deal with varying amounts of bytes to be saved.