- Home /
More of a Logic Qe for ordering an array
Using for(var i = 0; i < foo.length; i++)
I'm trying to get a 'following' concept set up to where a random number of objects (npcs) 1 to 3 , follows a specific transform point based off of the greatness of a stat they posses which could be unique or the same to each other. The points are the Main player transform, and two other empty game objects transforms i call leadLeft and leadRight, nested respectively within the player. The npcs are of different heights, the 'stat' is their own height level. I would like to have the shortest of any of these npcs follow leadLeft, the tallest to leadRight. Anyone who is taller than the shortest yet smaller then the highest will follow the mainPlayer transform which is between leadLeft and leadRight.
if there are 2 npcs out, they follow at the left or right with the same concept. if only one is out it will follow the player transform.
The code is simple enough at this point- My issue is that the npcs are picked at will, and there can be (duplicate) npcs who have the same height.
I need help trying to figure out how to state the 'if' statements for the best way to compare each npc in the array to handle any duplicate heights and still order them to follow the transform points from least to greatest to each other if one of the three is taller or shorter, or if all 3 are of equal heights.
For example, lets say npc[0]= 2, npc[1] = 7, and npc[3] = 2.
npc[0] would be assigned leadLeft npc[1] would be leadRight npc[2] would be the mainPlayer
If all three are equal they could be assigned to the leads in order to their place in the array.
I am confused how to state the logic at this point.
If I understand correctly, you need to 'separate' your arrays in 3 different arrays: Highest NPCs, Lowest NPCs and InBetween NPC's?
I suspect the easiest way to do this would be to get the highest and lowest values that exist in the array, then loop through it again and assign the elements to their proper array.
I'd do something like
using System.Collections.Generic;
//the array with the npcs
public NPC[] npcs;
//lists that contain npcs
List<NPC> shortestNPCs;
List<NPC> tallestNPCs;
List<NPC> otherNPCs;
//lowest and highest value
float maxHeight;
float $$anonymous$$Height;
void AssignNPCs(){
maxHeight = float.NegativeInfinity;
$$anonymous$$Height = float.PositiveInfinity;
//get $$anonymous$$ and max values in array elements
for(int i = 0; i < npcs.Length; i++){
float npcHeight = npcs[i].height;
if(npcHeight < $$anonymous$$Height) $$anonymous$$Height = npcHeight;
if(npcHeight > maxHeight) maxHeight = npcHeight;
}
shortestNPCs = new List<NPC>();
tallestNPCs = new List<NPC>();
otherNPCs = new List<NPC>();
//before you assing elements, decide on what to do if
//maxheight and $$anonymous$$heigt are the same.
if(maxHeight == $$anonymous$$Height){
//you could put all npcs in the otherNPCS list
//not sure if standard list has a constructor that takes an
//array as argument but oh wel.
otherNPCs = new List(npcs);
}else {
//assign elements
for(int i = 0; i < npcs.Length; i++){
//add tall npcs to tallest list, short to shortest, other to //other.
if(npcs[i].height == maxHeight) tallestNPCs.Add(npcs[i]);
else if(npcs[i].height == $$anonymous$$Height) shortestNPCs.Add(npcs[i]);
else otherNPCs.Add(npcs[i]);
}
}
}
Now you have lists of tallest, shortest, and in-between NPCs (and if they all have the same height they are all in inbetween).
So then what you do is you assign what lead to follow per list. So for example, all NPCs in the list of shortest ones you set it to leadLeft, for tallest to leadRight, and for other to leadCenter or something.
Alternatively, you could just order the array by element height, then assigne the first 1/3rd to left$$anonymous$$, second 1/3rd to center$$anonymous$$, and last 1/3rd to right$$anonymous$$.
I'm not completely sure what exactly you want actually, which is why this is all comments ins$$anonymous$$d of answers.
Why not enum three different types of players? Tall medium and short. This way you can just perform if if's at the start of the process and pre-assign every NPC with his length.
Siaran is absolutely getting my support with comment additions because question is to vague imho.
I'm really bugged too as why the main player is a Non Playing Character... Please explain!
Your answer
