How do you change which train wagons auto spawn?

Looking to make sure only trainwagona.entity spawns on the tracks.  Ideas?

Internally, Rust uses 2 populations for aboveground trains. The aboveground workcart population is determined by the traincar.population convar. The wagon population is derived from traincar.population times traincar.wagons_per_engine.

Each population has a list of prefabs that it can spawn. A simple plugin could probably be made to alter the prefabs in the wagon population to remove the ones you don't want. I can probably whip something out this week for that.

NKXTQs24ExGTuL8.jpg WhiteThunder

Internally, Rust uses 2 populations for aboveground trains. The aboveground workcart population is determined by the traincar.population convar. The wagon population is derived from traincar.population times traincar.wagons_per_engine.

Each population has a list of prefabs that it can spawn. A simple plugin could probably be made to alter the prefabs in the wagon population to remove the ones you don't want. I can probably whip something out this week for that.

Looking forward to it!

Here you go. Save as TrainWagonPopulationSettings.cs.

using System.Collections.Generic;
using System.Linq;

namespace Oxide.Plugins
{
    [Info("Train Wagon Population Settings", "WhiteThunder", "1.0.0")]
    [Description("Allows customizing which Train Wagon prefabs spawn on the aboveground rail.")]
    public class TrainWagonPopulationSettings : CovalencePlugin
    {
        private string[] AllowedWagonPrefabs = new string[]
        {
            // Instructions: Prefix each of the following lines with "//" to disable that prefab.
            // Note: You will have to manually remove existing train cars that have already spawned.
            "assets/content/vehicles/train/trainwagona.entity.prefab",
            "assets/content/vehicles/train/trainwagonb.entity.prefab",
            "assets/content/vehicles/train/trainwagonc.entity.prefab",
            "assets/content/vehicles/train/trainwagond.entity.prefab",
        };

        private void OnServerInitialized()
        {
            foreach (var population in SingletonComponent<SpawnHandler>.Instance.ConvarSpawnPopulations)
            {
                var railPopulation = population as ConvarControlledSpawnPopulationRailRing;
                if (railPopulation == null || !railPopulation.IsWagon)
                    continue;

                var prefabList = new List<Prefab<Spawnable>>();

                foreach (var prefabPath in AllowedWagonPrefabs)
                {
                    var gameObject = GameManager.server.FindPrefab(prefabPath);
                    if (gameObject == null)
                    {
                        LogWarning($"Invalid prefab: {prefabPath}");
                        continue;
                    }

                    var spawnable = gameObject.GetComponent<Spawnable>();
                    if (spawnable == null)
                    {
                        LogWarning($"Prefab has no Spawnable component: {prefabPath}");
                        continue;
                    }

                    prefabList.Add(new Prefab<Spawnable>(prefabPath, gameObject, spawnable, GameManager.server, PrefabAttribute.server));
                }

                population.Prefabs = prefabList.ToArray();
                population.numToSpawn = new int[population.Prefabs.Length];
            }
        }
    }
}

Worked like a charm.  Thank you!

NKXTQs24ExGTuL8.jpg WhiteThunder

Here you go. Save as TrainWagonPopulationSettings.cs.

using System.Collections.Generic;
using System.Linq;

namespace Oxide.Plugins
{
    [Info("Train Wagon Population Settings", "WhiteThunder", "1.0.0")]
    [Description("Allows customizing which Train Wagon prefabs spawn on the aboveground rail.")]
    public class TrainWagonPopulationSettings : CovalencePlugin
    {
        private string[] AllowedWagonPrefabs = new string[]
        {
            // Instructions: Prefix each of the following lines with "//" to disable that prefab.
            // Note: You will have to manually remove existing train cars that have already spawned.
            "assets/content/vehicles/train/trainwagona.entity.prefab",
            "assets/content/vehicles/train/trainwagonb.entity.prefab",
            "assets/content/vehicles/train/trainwagonc.entity.prefab",
            "assets/content/vehicles/train/trainwagond.entity.prefab",
        };

        private void OnServerInitialized()
        {
            foreach (var population in SingletonComponent<SpawnHandler>.Instance.ConvarSpawnPopulations)
            {
                var railPopulation = population as ConvarControlledSpawnPopulationRailRing;
                if (railPopulation == null || !railPopulation.IsWagon)
                    continue;

                var prefabList = new List<Prefab<Spawnable>>();

                foreach (var prefabPath in AllowedWagonPrefabs)
                {
                    var gameObject = GameManager.server.FindPrefab(prefabPath);
                    if (gameObject == null)
                    {
                        LogWarning($"Invalid prefab: {prefabPath}");
                        continue;
                    }

                    var spawnable = gameObject.GetComponent<Spawnable>();
                    if (spawnable == null)
                    {
                        LogWarning($"Prefab has no Spawnable component: {prefabPath}");
                        continue;
                    }

                    prefabList.Add(new Prefab<Spawnable>(prefabPath, gameObject, spawnable, GameManager.server, PrefabAttribute.server));
                }

                population.Prefabs = prefabList.ToArray();
                population.numToSpawn = new int[population.Prefabs.Length];
            }
        }
    }
}
This is exactally what I've been looking for! Any chance if you find a spare minute to update this? Getting error "CS0584: Internal compiler error: Object reference not set to an instance of an object". Im sure the recent updates broke it. This would be much apprecited.
masshole
This is exactally what I've been looking for! Any chance if you find a spare minute to update this? Getting error "CS0584: Internal compiler error: Object reference not set to an instance of an object". Im sure the recent updates broke it. This would be much apprecited.

I am also interested, it is to activate trains on smaller custom maps

To make it work correctly today you have to change the following line:
var railPopulation = population as ConvarControlledSpawnPopulationRail;

BLZhJCbjiCL6urU.png ninco90

To make it work correctly today you have to change the following line:
var railPopulation = population as ConvarControlledSpawnPopulationRail;

Ty homie

Now we have unloadable wagons. I mostly want only those, but maybe 1 or 2 C or B's. I couldn't figure it out, Anyone got an updated version I can leech?

Hello. I'm currently getting the following error: Error while compiling TrainWagonPopulationSettings

'ConvarControlledSpawnPopulationRail' does not contain a definition for 'IsWagon' and no accessible extension method 'IsWagon' accepting a first argument of type 'ConvarControlledSpawnPopulationRail' could be found (are you missing a using directive or an assembly reference?) | Line: 25, Pos: 63

Is it possible to fix this? Thanks!