Tutorial Coding How to Load Scenes from Script

Users who are viewing this thread

samdomino

Sergeant at Arms
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.
Code:
Settlement settlement = MBObjectManager.Instance.GetObject<Settlement>("castle_village_S8_2");


3. Create and set a new LocationEncounter object

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:
Code:
 Campaign.Current.HandleSettlementEncounter(MobileParty.MainParty, settlement);


5. Add LocationEncounter
In this step, we will add the location encounter we created to the PlayerEncounter object:
Code:
PlayerEncounter.Current.AddLocationEncounter(locationEncounter);


6. Execute CreateAndOpenMissionController
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(
                LocationComplex.Current.GetLocationWithId("village_center"),
                    (Location) null, (CharacterObject) null, (string) null);
 
Last edited:
Which I had read this before I embarked in my own quest with ILSpy as my weapon and tracing through the call stack from the menu all the way up to CreateAndOpenMissionController. I got 0 points in scouting for that, what a waste...
 
1 suggestion and 1 question:

- You might want to add the TaleWorlds.ObjectSystem include since MBObjectManager depends on it.

- You can't do this directly from the main menu, can you? Do you need to have a campaign loaded up prior to executing this code?
 
Back
Top Bottom