-
Notifications
You must be signed in to change notification settings - Fork 4
ScrewablePartAPI V2 setup guide
public class ScrewablePartTestMod : Mod
{
public override string ID => "ScrewablePart example";
public override string Version => "1.0";
public override string Author => "DonnerPlays";
private AssetBundle screwableAssetsBundle;
private ScrewablePartV2 screwablePart;
//This is a helper function
//I usually have it in a static "Helper" class
//This will try to load the save file from your mods config folder.
//If there is an error it will return a new saveFile
//This is recommended to prevent general problems in your mod
public static T LoadSaveOrReturnNew<T>(Mod mod, string savefileName) where T : new()
{
string path = Path.Combine(ModLoader.GetModConfigFolder(mod), savefileName);
if (File.Exists(path))
{
string serializedData = File.ReadAllText(path);
return JsonConvert.DeserializeObject<T>(serializedData);
}
return new T();
}
public override void OnLoad()
{
screwableAssetsBundle = LoadAssets.LoadBundle(this, "screwableapi.unity3d");
Dictionary<string, int[]> screwSave = Helper.LoadSaveOrReturnNew<Dictionary<string, int[]>>(this, "screwableV2_saveFile.json");
ScrewableBaseInfo baseScrewInfo = new ScrewableBaseInfo(screwableAssetsBundle, screwSave);
GameObject dashboard = GameObject.Find("dashboard(Clone)");
//This is the gameobject you created from your own assetsbundle
//The defined screws will be added as childs to this object
//It can also be something like myPart.rigidPart when using ModAPI by @tommojphillips
//This is just an example
GameObject myModelGameobject = new GameObject();
screwablePart = new ScrewablePartV2(baseScrewInfo, "part_id", myModelGameobject, new ScrewV2[]
{
//Here you define the screws you want to add, each screw is a new object
//Position and rotation are local values meaning these are the values based on the origin of the parent.
//If the parents origin is in the center of itself. the screw will with 0, 0, 0 will be at that positon.
new ScrewV2(new Vector3(0, 0, 0), new Vector3(0, 0, 0), 0.8f, 8, ScrewV2.Type.Nut),
//Wil create a screw with a scale of 0.8 applied to all axis
//and a size of 8 (meaning a ratchet/spanner of size 8 is required
//And the nut model from the game
new ScrewV2(new Vector3(0, 0, 0), new Vector3(0, 0, 0), 1.5f, 6, ScrewV2.Type.Screw1),
//Wil create a screw with a scale of 1.5 applied to all axis
//and a size of 6 (meaning a ratchet/spanner of size 6 is required
//And the screw1 model from the game
new ScrewV2(new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
//This will create a screw with default values which are:
//Scale = 1.0
//Size = 10
//Model = Screw2 (a scrw with a spanner/ratchet head and a small length
});
//Advanced
screwablePart.AddClampModel(new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(1, 1, 1));
//This will add a clamp model useful when you want to connect two pipes
screwableAssetsBundle.Unload(false);
//You have to unload assetsbundles after you are done with loading
//or your mod will crash when you exit the game and re-enter
}
public override void Update()
{
ModConsole.Print(screwablePart.partFixed);
//Writes "true" or "false" to the mod console.
//true meaning all screws are fixed and the collider of the part is disabled
//false meaning not all screws are fixed and the collider of the part is enabled
}
public override void OnSave()
{
try
{
ScrewablePartV2.SaveScrews(this, new ScrewablePartV2[] {
screwablePart
}, "screwableV2_saveFile.json");
}
catch (Exception ex)
{
//Show an error/log?
}
}
public override void ModSettings()
{
ScrewablePartV2.SetupModSettings(this);
//This will add an entry to your mods Settings page.
//Users can toggle to show the screw size they are aiming at when having a tool in hand.
//This is optional
}
}Now comes the "hard" part. You have to let the screwablepart api/ the screwable part know when it get's removed from it's parent. You have to either handle that yourself somehow or use the ModApi from @tommojphillips.
The ModApi from tommojphillips comes with overwritable methods called "assemble" and "disassemble" There you have to call two functions like this:
protected override void assemble(bool startUp = false)
{
screwablePart.OnPartAssemble();
}
protected override void disassemble(bool startup = false)
{
screwablePart.OnPartDisassemble();
}You can't inject these into a ModAPI part. This means for EVERY part you want in your mod You have to create a seperate class that extends from the Part class of the ModAPI and then override the two methods. And you need a way for the class to access the screwable part So either pass it in the constructor Or make the screwable part a static variable in your mods class and then do something like
ScrewablePartTestMod.screwablePart1.OnPartDisassemble();
...
## This is a simple explanation if you need more tips and help with it let me know.
Another solution I can recommend is creating a wrapper class.
I used 2 classes.
The first creates an object of the second class something like "NewPart" that extends from the Part class.
This "NewPart" gets a setter and getter that will set an action.
This action is then called on assemble/disassemble with .Invoke()
These actions that get set will include the screwable part.