array values
Blitz3D Forums/Blitz3D Programming/array values
| ||
For completing a routine for enemies choosing targets, calculating the distance between entities (older posts), I must know which is the lowest of some numbers stored in an array. How could I know the index of the lowest number stored in an array? Thanks |
| ||
Maybe you could keep track of the values when you are writing to the array?Dim x(5) lowest = 65536 loindex = -1 for i = 0 to 5 x(i) = rand(6) if x(i) < lowest then lowest = x(i) loindex = i end if next print "lowest value is " + lowest + " at " + loindex Alternatively, you would need to iterate through the array to find it's lowest value. |
| ||
u just dont wanna help... i see where this is going.. fine.. if u want it that way.. fine.. thanks for nothing.. "friend".. and i dont mean u, sagitario. |
| ||
ok, so heres the final job: For e.enemy = Each enemy For b.bot = Each bot i=i+1 If b\number=i e\distancias[i]=EntityDistance(b\mesh,e\mesh) EndIf Next i=0 Next this stores in e\distancias[i] the distance from one enemy to all bots ****each enemy must have a e\number field ****each bot must have a b\number field ****unique numbers, that identify them then, (thanks to warner) we select the objective in function of lowest array value, and the enemy goes for the selected, and nearest, bot the same procedure could be done with each bot... Ha ha, I cant wait to test this in battlefield |
| ||
u just dont wanna help... i see where this is going.. fine.. if u want it that way.. fine.. thanks for nothing.. "friend".. and i dont mean u, sagitario. I've read quite a few of your posts recently and to be frank, your attitude sucks. The people on this forum are not your personal tutors! When you consider that Warner and others have practically written your programs for you I think you should show far more gratitude. Posting aload of messy unformatted code every couple of hours asking "What's wrong with this?" isn't going to help you in the long term. |
| ||
please dont argue people. @ DarkShadowWing - just a couple of words: these guys are helping us, not writing our programs for us... be grateful to them... and for gods sake, don't get angry if people in the forums need fresh air sometimes, they are humans and they usually get tired, ocasionally they even watch tv, or eat or drink, or think in other things than your program man, please be patient... dont be rude, i've seen your work, it deserves more than a quarrel in the forums... so lets be friends please... |
| ||
to answer your question, iterate through the entire array like warner said except do it every time you need to know the closest enemy. if yoiu need code, i can give some, but I think warner just about covered it. |
| ||
Ok, there is the problem. The value of loindex cannot increase... using this: For e.enemy = Each enemy For b.bot = Each bot i=i+1 If b\number=i e\distancias[i]=EntityDistance(b\mesh,e\mesh) If e\distancias[i]<e\lowest e\lowest = e\distancias[i] e\loindex=i endif EndIf Next i=0 Next Any ideas to check the value of loindex iteratively? Thanks for your help Nate! |
| ||
in your code, you have to init the value first, before [for each bot] For e.enemy = Each enemy e\lowest = +20000000 For b.bot = Each bot [...] ps : maybe you 'd better use a sorted list instead of an array. |
| ||
ok! working! didn't see the obvious, lost in types... thank you, everybody... you clever people! arrrgh! my brain is an old 486 compared to yours!!!!!! and to reach this tiny piece of code I had even to overclock it... For e.enemy = Each enemy e\lowest=2000000 For b.bot = Each bot i=i+1 If b\number=i e\distancias[i]=EntityDistance(b\mesh,e\mesh) If e\distancias[i]<e\lowest Then e\lowest = e\distancias[i]:e\loindex=i EndIf Next i=0 Next THANK YOU ALL! |
| ||
Ok people, I posted it in the code archives, with regards to Mr. Warner and Mr. Bobysait. Hopefully someone will find it useful. See ya! |
| ||
if I were you, I'd init "i=0" before the bot loop, not after. You don't know for the first e.ennemy if "i" has not been used somewhere else It should be : For e.enemy = Each enemy e\lowest=2000000 i=0 ; reset "i" value here ! For b.bot = Each bot i=i+1 If b\number=i [...] |