playMaker

Author Topic: how to take lower float in an array ?  (Read 210 times)

blackant

  • Sr. Member
  • ****
  • Posts: 426
  • http://blackantmaster.com
    • View Profile
    • blackantmaster.com
how to take lower float in an array ?
« on: May 17, 2019, 05:12:22 AM »
hello,

in my game i have a setup in wich the ai must take a look around him and check wich gameobject is the nearest.

so i registered all distances in an array, but now, i don't know if there is a better way to compare each value and take the smallest, (and i'm pretty sure there is one) ?

cause for now i'm making multiple loops and set différents steps (<10, <20, <30...) but doing this way may takes ages for me but also to calculate.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 4518
    • View Profile
    • jinxtergames
Re: how to take lower float in an array ?
« Reply #1 on: May 17, 2019, 03:05:21 PM »
Hi.
I think you should use some arrays for this.

Have 2 arrays 1 to store the object and one for the distance.

Then 1st use an array get and get the distance on index 0.
store it in a variable (call it 'Closest' for example)
also store the index. (call it 'ClosestIndex')

Then in next state use an array get next. and get the distance as well.
store it in a variable (call it 'compare' for example)
and also store the index (call it 'CurrentIndex' for example)

Then in next state do a float compare.
if 'compare' is less that 'Closest' set the 'compare' value to the 'closest' variable.
also set the 'CurrentIndex' value to the 'ClosestIndex'

Then loop back to the get next state.
when done, the result should be the closest one.

For the compare you could also use the equal to change as well but that depends on your preferences.
FOR HIRE:Playmaker/C#

Playmaker Samples <-------------> Easy Exporter <---------------------> TPA Actions

blackant

  • Sr. Member
  • ****
  • Posts: 426
  • http://blackantmaster.com
    • View Profile
    • blackantmaster.com
Re: how to take lower float in an array ?
« Reply #2 on: May 17, 2019, 03:10:03 PM »
yes !! thanks you, got it !!
i already have my arrays, but i didn't think about replacing the first by the lowest each time it fund one .
that's perfect  :)