Search Unity

Only one

Discussion in 'Scripting' started by Sydious, Jul 30, 2014.

  1. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    So I have some game objects. Let's call them Quests.

    I also have some game objects called Heros.

    Heros have an AI script attached and the Quests have a script with some vars.

    In the Hero's AI script, it searches for the nearest object with the tag "quest".

    In that script, I attempt to check a variable on the quest object to see if a hero has been assigned to that quest.
    If the questHero variable is null, then it assigns the hero to the quest.

    The problem I am having is I think that when each Hero object searches at the same time, it finds questHero = null at the same time so then the object thinks it has been assigned.

    Can someone help me understand what I need to do here to control only 1 hero finding the quest object?

    What i am trying to achive is as follows:

    I have 4 Hero Objects.
    In their idle state they search for a questObject.
    First hero is assigned to the quest object as questHero.
    the remaining 3 heros can not find the quest since questHero is no longer null.
    if I had 3 quest objects, then 3 heros would get 3 diffrent quests and 1 woudln't

    Here is the code I am using:
    Code (JavaScript):
    1. function FindQuest () : GameObject {
    2.     // Find all game objects with tag quest
    3.     var gos : GameObject[];
    4.     gos = GameObject.FindGameObjectsWithTag("quest");
    5.     var closest : GameObject;
    6.     var distance = Mathf.Infinity;
    7.     var position = transform.position;
    8.     // Iterate through them and find the closest one
    9.     for (var go : GameObject in gos)  {
    10.         var diff = (go.transform.position - position);
    11.         var curDistance = diff.sqrMagnitude;
    12.         var qh = go.transform.GetComponent(QuestScript).questHero;
    13.         if (curDistance < distance || qh != null) {
    14.             closest = go;
    15.             distance = curDistance;
    16.         }
    17.     }
    18.     return closest;  
    19. }
    Since this is fired basically on the same frame for all 4 heros, all 4 find the questHero var as null and returns the object as it's target. With the hero objects changing states, they potentially can be checking for this at the same time.

    hope this all made sense.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if you add an "if quest != taken" at the start of the for loop wouldn't it skip quests that are already taken. I can't remember how you cast types in unityscript, but if you cast the "go" to a quest and ask for it's questHero i think you should be able to do that.
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    "all 4 find the questHero var as null"
    But you aren't changing questHero to be not null, so that's probably true.
     
  4. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    I am setting questHero. Elsewhere in the AI script, the function "FindQuest"(above script) is called and returns the quest that is closest. The first hero gets assigned to the questHero var which is when questHero is no longer null. That much is happening. The problem is, when all 4 check at the same time, they all end up with the quest as a target although only hero 1 is truely assigned to the questHero. I expected the qh != null to be exactly like leftyrighty's if quest != taken suggestion.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    *looks sheepish* didn't see the qh...

    curDistance < distance || qh !=null

    if it's the closest quest and qh is null this if will return true. try && instead of ||
     
  6. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    *looks sheepishly* in return. Not sure why I used OR instead of AND. I meant AND. That must be the issue. I will try that.
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    blame the sheep :)