#Code help: Creation of Command hierarchy SOLVED!
25 messages · Page 1 of 1 (latest)
You know, I think that is part of the problem. I am trying to do too much. I am trying to get it to work - right now the Flock class spawning the hierarchy does not work.
if it did, next issue that I am clueless about is where to spawn the commandposts.
I think part of the issue is you're not using/aware of ScriptableObject?
These seem like they should be implemented as ScriptableObject
ah, You know I used used scriptable objects before, but it did not occur to me to use them here. this is good suggestion
Okay, so I think I fixed a lot of the small issues. To summarize I want to spawn prefabs into a hierarchical structure.
Here is the code, I am very new so bear with me
https://paste.myst.rs/5szwmmf6
a powerful website for storing and sharing text and code snippets. completely free and open source.
When starting from the top, it throws up these errors.
when starting from the bottom is throws this one error
public class FlockSpawnTest : MonoBehaviour
{
public FlockAgent agentPrefab;
List<FlockAgent> agents = new List<FlockAgent>();
public CommandPost commandPostPrefab;
List<CommandPost> commandPosts = new List<CommandPost>();
int[] numOfEachRank = new int[5];
[Range(0, 500)]
public int unitCount = 250;
const float AgentDensity = 0.08f;
[SerializeField] private bool topDownCreation = false;
void Start()
{
int unitsSpawned = 0;
if (unitCount <= 0) return;
int initialRank = topDownCreation ? 4 : 0;
unitsSpawned = topDownCreation ? 0 : 1;
CommandPost currentFormation = SpawnCommandPost(initialRank, unitsSpawned);
if (currentFormation == null)
{
Debug.LogError("Initial command post failed to spawn.");
return;
}
while (unitsSpawned < unitCount)
{
if (currentFormation.Subordinates.Count < currentFormation.SubordinateLimit)
{
int subordinateRank = currentFormation.myRank - 1;
if (subordinateRank >= 0)
{
CommandPost newSubordinate = SpawnCommandPost(subordinateRank, unitsSpawned);
if (newSubordinate != null)
{
currentFormation.AddSubordinate(newSubordinate);
if (newSubordinate.myRank == 0) unitsSpawned++;
}
}
else
{
// Spawn agent at lowest rank
FlockAgent newAgent = Instantiate(agentPrefab, currentFormation.transform.position, Quaternion.identity);
agents.Add(newAgent);
currentFormation.AddSubordinate(newAgent);
unitsSpawned++;
}
}
bool movedDown = false;
foreach (CommandPost subordinate in currentFormation.Subordinates)
{
if (subordinate.Subordinates.Count < subordinate.SubordinateLimit)
{
currentFormation = subordinate;
movedDown = true;
break;
}
}
if (!movedDown)
{
if (currentFormation.Superior == null && currentFormation.myRank != 4)
{
int superiorRank = currentFormation.myRank + 1;
CommandPost superior = SpawnCommandPost(superiorRank, unitsSpawned);
if (superior != null)
{
superior.AddSubordinate(currentFormation);
currentFormation = superior;
}
}
else
{
if (currentFormation.Superior != null)
currentFormation = currentFormation.Superior;
else
break; // Prevent infinite loop
}
}
}
}
CommandPost SpawnCommandPost(int rank, int numAgentsSpawned)
{
if (rank < 0 || rank >= numOfEachRank.Length)
{
Debug.LogError($"Invalid rank {rank}.");
return null;
}
Vector3 pos = transform.position + new Vector3(
numAgentsSpawned / 10f,
(rank - 5) * 2f / 3f,
0
);
CommandPost newPost = Instantiate(commandPostPrefab, pos, Quaternion.identity);
newPost.myRank = rank;
newPost.name = $"{numOfEachRank[rank] + 1} {newPost.GetCommandLevelName(rank)}";
numOfEachRank[rank]++;
commandPosts.Add(newPost);
return newPost;
}
}```
there's a few things i did to refactor your code:
- instead of using
rankToAssignit now usescurrentFormation.myRank - 1so there's proper hierarchy depth - we check if
subordinateRankis valid before spawning, and if its invalid we spawn a flock agent instead - we properly increment
unitsSpawnedwhen creating agents or rank 0 command posts - when moving up the hierarchy, superiors are created with
currentFormation.myRank + 1 - we check if an array is out of bounds (like the
rankbefore accessingnumOfEachRank)
Wow, I am almost beyond words! Many thanks! I went through it a few times and it's very readable!
blushibewinston thanked helzky
I put it into the project it spawns without any crashes or infinite loops! There is still some logic problem I need to work out as you can see with topDownCreation false on the left and topDownCreation true on the right where when moving up it seems to mess up when moving up the hierarchy. But that's more so a logical issue that I should be able to figure that out myself
Changed alot of things, but it ended up being a problem in another class with how the superior was set
There is still something odd going on,
I did it!
Thank you so much guy!
Now time to finish that refactor to scriptable objects
Code help: Creation of Command hierarchy SOLVED!
nice good job!
like i said, let me know if you ever need programming help
You're too generous, man.
wait there is still a problem on the second lowest level. Huh.
dude i wish we could frame your help requests as an example of the gold standard tbh
Finished, the solution for the final error came to me suddenly
whatcha makin