Home > Enterprise >  Limit on the number of NPCs
Limit on the number of NPCs

Time:02-12

Such an idea, for which I don't even have any ideas how to do it right. In general, the thought is: There are, say, three NPCs on the stage (Conditionally), and there is also one building (Conditionally). Each NPC has its own task, but I do not know how to take an NPC who does not have a task and send one NPC to work in the building, and not several at once. Namely, each NPC has work statuses, and there is also a status 0, meaning that the NPC is not busy, there may be several such "free" ones, and if one is assigned the status of work - go to the building, then everyone who has this status will go, but how can we make sure that only one free NPC is taken and sent there? I know the question is as incomprehensible and terrible as possible, but maybe someone will understand...

CodePudding user response:

I made, so, to begin with, the code:

File WorkingPlace:

public static bool SignalFreeNPCPoint = false;
public static int WorkingPlaceNPC = 0;
public void ProductBoardNPC()
{
    SignalFreeNPCPoint = true;
    WorkingPlaceNPC  = 1;
}
public void ProductBoard_Stop()
{
    WorkingPlaceNPC -= 1;
    SignalFreeNPCPoint = true;
}

This "ProductBoardNPC()" and "ProductBoard_Stop()" method's for button in Unity.

File NPCSctipt:

public List<GameObject> ListGM = new List<GameObject>(); 
public int TaskNumber = 0;
void Update()
{
    TaskManager();
    LogicNPC();
}
private void TaskManager()
{
    switch (TaskNumber)
    {
        case 0:
            //You code
            break;
        case 1:
            ListGM.Remove(gameObject);
            break;
        case 2:
            ListGM.Remove(gameObject);
            break;
        case 3:
            ListGM.Remove(gameObject);
            break;
        case 4:
            ListGM.Remove(gameObject);
            break;
    }
}
private void LogicNPC()
{
    if (WorkingPlace.SignalFreeNPCPoint == true)
    {
        if (TaskNumber == 0 & WorkingPlace.WorkingPlaceNPC == 1)
        {
            ListGM.Add(gameObject);
            TaskNumber = 4;
        }
        if (TaskNumber == 4 & WorkingPlace.WorkingPlaceNPC == 0)
        {
            TaskNumber = 0;
        }
        WorkingPlace.SignalFreeNPCPoint = false;
    }
}

How the code works: When you click on the button, an NPC with the status 0 is searched, the last NPC with this status is taken and entered into the array, after which it is given a different status to perform "work", after which it is removed from the array to make room for another NPC. In this case, the array is a buffer and is simply necessary. If you have better ideas than mine, I will be happy to listen) The implementation is a clean bike made of crutches, but it works flawlessly. Yes, about FPS drawdowns, it doesn't seem to load the system much, if there are drawdowns, then write what to fix.

  • Related