#Code help: Creation of Command hierarchy SOLVED!

25 messages · Page 1 of 1 (latest)

valid sentinel
#

so whats your question

tight swallow
#

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.

valid sentinel
#

These seem like they should be implemented as ScriptableObject

tight swallow
#

ah, You know I used used scriptable objects before, but it did not occur to me to use them here. this is good suggestion

tight swallow
#

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

#

When starting from the top, it throws up these errors.

#

when starting from the bottom is throws this one error

warped crane
# tight swallow Okay, so I think I fixed a lot of the small issues. To summarize I want to spawn...
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 rankToAssign it now uses currentFormation.myRank - 1 so there's proper hierarchy depth
  • we check if subordinateRank is valid before spawning, and if its invalid we spawn a flock agent instead
  • we properly increment unitsSpawned when 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 rank before accessing numOfEachRank)
tight swallow
normal elmBOT
#

blushibewinston thanked helzky

tight swallow
#

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

tight swallow
#

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,

tight swallow
#

I did it!

#

Thank you so much guy!
Now time to finish that refactor to scriptable objects

#

Code help: Creation of Command hierarchy SOLVED!

warped crane
#

like i said, let me know if you ever need programming help

tight swallow
#

You're too generous, man.

tight swallow
valid sentinel
tight swallow
#

Finished, the solution for the final error came to me suddenly

valid sentinel