# Find the lowest value in a column in a table?

Just a guy, you know.Member, PRO Posts: 236
edited February 2015

This is a slightly different question than the other missile question I asked, so I thought I'd start a new topic.

I've got a homing missile system I'm trying to set up and I'm using a table to track the location of randomly spawned enemies.
The table adds a new row with each new enemy who then broadcasts its position in the first and second column. The enemies are also indicating how close they are to my missile shooting hero in the third column.

How can I tell my missiles to always aim for the enemy that has the lowest number in that third column (as in, is the closest to my hero)?

• Member, Sous Chef, PRO, Bowlboy Sidekick Posts: 9,041

This tutorial may help you - it's sorting the table from high to low, but it would not be hard to edit to sort from low to highest values.

• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,936

Sorting is handy but if you only need the single lowest value, it's easier to Loop over Table with a rule that checks to see if the current tableCellValue is less than game.lowest (set to some high number initially e.g. 9999) and if so changes game.lowest to the tableCellValue. When the index row attribute equals the tableRowCount, then the loop is done and you can use game.lowest in your other rules.

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Member, Sous Chef, PRO, Bowlboy Sidekick Posts: 9,041
edited February 2015

You could also loop the table and grab each value and compare it with the lowest value before it. So you'd create a game attribute and set it to the lowest value it finds first (which would be the first cell checked), and then in the loop you would continue to determine if the cell the loop is currently on is smaller than the value in the game attribute.

EDIT: Beat by @tatiang!

• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,936
edited February 2015

Too slow, @Braydon_SFX, too slow!

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Just a guy, you know. Member, PRO Posts: 236

Cool, thanks guys! I'll try this out tonight as an alternate way to deal with homing missiles. Hope it works.

• Member, Sous Chef, PRO, Bowlboy Sidekick Posts: 9,041

@tatiang said:
Too slow, Braydon_SFX, too slow!

I beat you in that other thread this morning, don't forget. My caffeine must be wearing off - time to get more Monster!

• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,936

Oh I haven't forgotten! Lol

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Just a guy, you know. Member, PRO Posts: 236

Nevermind. I guess I really don't understand how the looping function even works. This homing missile thing has been very frustrating.

I made a game.closesttohero attribute. Then I tried making a loop in the missile and I'm just lost in there. Basically not sure how to make it say to check the cell value of any cell in column 3. Is it possible you could spell it out a little more in depth for me?

• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,936

Here's a demo.

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Member Posts: 1

Problem is here once you move away from the object the lowest value remains

• Member, Sous Chef, PRO Posts: 2,864

@grapesforall -- you would need to constantly update the table search. Perhaps you could use a timer set to a reasonably low interval.