I'm developing plugin that makes your rust server racing server with modular cars. This is how I'm spawning vehicle with defined modules;
var entity = GameManager.server.CreateEntity("assets/content/vehicles/modularcar/car_chassis_2module.entity.prefab", spawnPosition, rotation);
var modularCar = entity as ModularCar;
var moduleItem = ItemManager.CreateByItemID(-1501451746);
modularCar.TryAddModule(moduleItem, 0);
moduleItem = ItemManager.CreateByItemID(1559779253);
modularCar.TryAddModule(moduleItem, 1);However, I'm having problem when I try insert components in the engine module.. I don't know how to get right container to be added items. Also, if I do this after the code;
modularCar.HasAnyModules;It returns false.
When I run the code, car with cockpit module and engine module spawns. I am able to mount and drive it if I insert components manually.. Is this right way to add modules to vehicles?
I am very new to C#, but I have some experience on other languages. I have studied at least this and this, and those plugins have helped me a lot. On the "Car Spawn Settings" -plugin there is this code;
private void MaybeAddEngineParts(ModularCar car)
{
if (!pluginConfig.CanHaveEngineParts()) return;
foreach (var module in car.AttachedModuleEntities)
{
var engineModule = module as VehicleModuleEngine;
if (engineModule != null)
{
var engineStorage = engineModule.GetContainer() as EngineStorage;
if (engineStorage != null)
{
AddPartsToEngineStorage(engineStorage);
engineModule.RefreshPerformanceStats(engineStorage);
}
}
}
}
private void AddPartsToEngineStorage(EngineStorage engineStorage)
{
if (engineStorage.inventory == null) return;
var inventory = engineStorage.inventory;
for (var i = 0; i < inventory.capacity; i++)
{
// Do nothing if there is an existing engine part
var item = inventory.GetSlot(i);
if (item != null) continue;
var tier = pluginConfig.GetPossiblyRandomEnginePartTier();
if (tier > 0)
TryAddEngineItem(engineStorage, i, tier);
}
}
private bool TryAddEngineItem(EngineStorage engineStorage, int slot, int tier)
{
ItemModEngineItem output;
if (!engineStorage.allEngineItems.TryGetItem(tier, engineStorage.slotTypes[slot], out output)) return false;
var component = output.GetComponent<ItemDefinition>();
var item = ItemManager.Create(component);
if (item == null) return false;
item.conditionNormalized = pluginConfig.GetRandomNormalizedPartCondition();
item.MoveToContainer(engineStorage.inventory, slot, allowStack: false);
return true;
}I just can't use "GetContainer" for some reason.
Thank you if you read this far and hopefully you could help me!
-tyse