Find the lowest value in a column in a table?
owen_dennis
Just a guy, you know.Member, PRO Posts: 236
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)?
Comments
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.
My GameSalad Academy Courses! ◦ Check out my quality templates! ◦ Add me on Skype: braydon_sfx
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
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!
My GameSalad Academy Courses! ◦ Check out my quality templates! ◦ Add me on Skype: braydon_sfx
Too slow, @Braydon_SFX, too slow!
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Cool, thanks guys! I'll try this out tonight as an alternate way to deal with homing missiles. Hope it works.
Vote for Nearest Neighbor Scaling option in gamesalad! Let's make our games look truly stunning!
I beat you in that other thread this morning, don't forget. My caffeine must be wearing off - time to get more Monster!
My GameSalad Academy Courses! ◦ Check out my quality templates! ◦ Add me on Skype: braydon_sfx
Oh I haven't forgotten! Lol
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
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?
Vote for Nearest Neighbor Scaling option in gamesalad! Let's make our games look truly stunning!
Here's a demo.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Problem is here once you move away from the object the lowest value remains
@grapesforall -- you would need to constantly update the table search. Perhaps you could use a timer set to a reasonably low interval.