Hello,
I am trying to get a proper way to spawn entity at random position without having trouble like
Population is null, entity spawned from outside the spawn handler?
Here is my code
Each time a resource spawn i get spammed by this message.Code:private void spawnResourceNode(Vector3 location) { string prefabName = "assets/bundled/prefabs/autospawn/resource/ores/"; // Select a random ResourceNode type {Metal, Stone, Sulfur}. int r = UnityEngine.Random.Range(0, 2); switch (r) { case 1: prefabName += "metal-ore"; break; case 2: prefabName += "stone-ore"; break; default: prefabName += "sulfur-ore"; break; } prefabName += ".prefab"; // & spawn the ResourceNode at Vector3(location). var newPos = AdjustPosition(location); BaseEntity resourceNode = GameManager.server.CreateEntity(prefabName, newPos, new Quaternion(), true); resourceNode.Spawn(); } private Vector3 AdjustPosition(Vector3 pos) { Vector3 randomPos = Quaternion.Euler(UnityEngine.Random.Range((float)(-100 * 0.2), 100 * 0.2f), UnityEngine.Random.Range((float)(-100 * 0.2), 100 * 0.2f), UnityEngine.Random.Range((float)(-100 * 0.2), 100 * 0.2f)) * pos; Vector3 correctPos = GetGroundPosition(randomPos); return correctPos; } static Vector3 GetGroundPosition(Vector3 sourcePos) // credit Wulf & Nogrod { RaycastHit hitInfo; if (Physics.Raycast(sourcePos, Vector3.down, out hitInfo, LayerMask.GetMask("Terrain", "World", "Construction"))) sourcePos.y = hitInfo.point.y; sourcePos.y = Mathf.Max(sourcePos.y, TerrainMeta.HeightMap.GetHeight(sourcePos)); return sourcePos; }
@Wulf is there a way to avoid this message?
Proper way to spawn entity
Discussion in 'Rust Development' started by sami37, Oct 29, 2016.
-
Every object that can be spawned from the `autospawn` prefabs has a population. This `SpawnPopulation` takes care of spawning and respawning the resources in specific areas of the map whilst keeping a "steady" population to not spawn too many resources all over the map. So every time you spawn a tree, stone ore, metal ore, etc. it will check this against the spawn population resulting in that `Population is null` message showing up. The only way to get around it is to setup your own `SpawnHandler` to handle it instead of manually spawning the resources.
