This guide will show you how to load scenes from scripts and will cover these portions:
Needed References
How Scenes are Loaded
Implementation
Needed References
TaleWorlds.CampaignSystem;
TaleWorlds.Core;
How Scenes are Loaded
Village scenes appear to loaded by LocationEncounter objects. These objects come in three varieties: VillageEncounter, TownEncounter, and CastleEncounter objects. These objects represent the type of settlement that has been encountered which can be inferred by their name.
The LocationEncounter object has a method called CreateAndOpenMissionController which accepts these parameters (Type parameterName )
Location nextLocation
Location previousLocation = null | Default Value
CharacterObject talkToChar = null | Default Value
string playerSpecialSpawnTag = null | Default Value
This method takes these parameters and loads the scene that is specified by nextLocation. In order to successfully execute this method without error, it is important that you follow the implementation guide.
Implementation Outline
1. Reference Core and Campaign
2. Get Settlement Object from MBObjectManager
3. Create and set a new LocationEncounter object
4. Configure HanddleSettlementEncounter
5. Add LocationEncounter
6. Execute CreateAndOpenMissionController
Before I proceed, it is important to note that I am not covering every nitty detail that is needed to setup mods. Please refer to other tutorials for setting up MBSubModuleBase, adding references, configuring SubModule.XML etc.
1. References
In your SubModule.cs (Script) file, add these references:
Code:
using TaleWorlds.Core;
using TaleWorlds.CampaignSystem;
2. Get Settlement Object from MBObjectManager
The MBObjectManager contains instances of objects in the game. We want to get an instance of a settlement object, so we will first create an example class, then add a settlement.
Now, we will create and set a new LocationEncounter object. It is important to note that I am using VillageEncounter because the settlement is a Village.
Code:
LocationEncounter locationEncounter = new VillageEncouter(settlement);
4. Configure HandleSettlementEncounter
This is a very important step. Without this, you will get NullReferenceErrors! Proceed as follows:
Finally, we get to load the scene! Now it is important to note again that I am using a VillageEncounter because my scene will load a VillageEncounter. Make adjustments as needed and use the other two encounter objects (Castle | Town). Additionally, it is also important to note that I am getting a Location object from the LocationComplex by searching for the location with an ID. For villages, you can simple use village_center. For the others, you can inspect the LocationComplex object with a debugger to figure out what the ID is.
Code:
(PlayerEncounter.LocationEncounter as VillageEncouter).CreateAndOpenMissionController(