How to check which actor's number is smallest?
Manto
Member Posts: 796
I have five actors on my scene. All of them have integer attributes called number. What would be the best way to check which one's number is the smallest?
Thanks in advance.
Thanks in advance.
Best Answers
-
KiwiLee Posts: 181Lots of choices. Depending on what you want to do I.e if the actor needs to react to the fact that it has the smallest value or you simply need to know to display a message of some type?
Can you explain a little more what you want to achieve?
-
KiwiLee Posts: 181You can trigger the actors to write there value to a game.attribute or table and then start to check them off against each other.
If one > two then result1 = x.
If result 1 > three etc
Can two or more be the same size smallest etc?
Is there a scenario where there are going to be more actors as that would steer me to tables and loop through the rows! -
LiquidGameworks Anchorage, AKPosts: 956If you have your values are in a table, you can do a table check using the min function.
-
gyroscope I am here.Posts: 6,598Good point from @KiwiLee about any two values which are the same but happen to be the smallest...
And great idea from @LiquidGameworks about using the min function for the first two numbers to put into an attribute called Smallest, for instance, but you'd still have to compare 4 times in all... not sure if there's any advantage in using tables in this case but would be interested to be corrected there....
Another way I can think of, outside of tables, and not even using the min function (but assuming no two numbers will be the same) would be:
Change Attribute Smallest to numberA
Change Attribute ActorS to A --- text attribute
When numberB < Smallest
Change Attribute Smallest to numberB
Change Attribute ActorS to B
When numberC < Smallest
Change Attribute Smallest to numberC
Change Attribute ActorS to C
When numberD < Smallest
Change Attribute Smallest to numberD
Change Attribute ActorS to D
When numberE < Smallest
Change Attribute Smallest to numberE
Change Attribute ActorS to E
--------------------
When ActorS =A
Change Attribute ActorA.Position.X to ??
Change Attribute ActorA.Position.Y to ??
When ActorS =B
Change Attribute ActorB.Position.X to ??
Change Attribute ActorB.Position.Y to ??
etc.
You can do away with the lines: Change Attribute Smallest to number? in each case if you don't need the actual value of the smallest number for anything.
I think that should work... I'll wait to be corrected tho' ;-)
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
-
LiquidGameworks Anchorage, AKPosts: 956@Gyroscope @Manto1
Let me show ya a trick I stumbled on a couple weeks ago. All hail mighty tables!
min("self.attribute",tableCellValue( game.Table 1 ,tableRowCount( game.Table 1 ),1))
So, this is what I did. I made a table with 1 column and 5 rows (add 30 if you want!). Then, have each of your actors add their own integer values to a designated row. Once you do that, you should have 5 (or as many as you want) rows, each one representing a different actor.
Then, with the above formula, you can check for the minimum between an actors self attribute (in your case the integer attribute called number) and all the other values in the table.
If the actor has the lowest number, do whatever you want. -
gyroscope I am here.Posts: 6,598
Good stuff, @LiquidGameworks!
However, what you've shown is for comparing two values taken from a table; you'd still need more Rules for comparing the other values, similar to my method, so there's no less Rules there; and extra Rules would be needed using your method for changing attribute values to table values before checking (write to the tables first)... so I still can't see any advantage using tables in this case, speed-wise or any other way, sorry... :-S
If you're using the method I suggested, that'd be no problem, just carry on...
Well, the user has possibility to add more actors (up to 30). Btw, is it far too much to check 30 attributes?
When numberE < Smallest
Change Attribute Smallest to numberE
Change Attribute ActorS to E
--------------------
When ActorS =E
Change Attribute ActorE.Position.X to ??
Change Attribute ActorE.Position.Y to ??
---all the way up to---
When numberDD < Smallest
Change Attribute Smallest to numberDD
Change Attribute ActorS to DD
--------------------
When ActorS =DD
Change Attribute ActorDD.Position.X to ??
Change Attribute ActorDD.Position.Y to ??
I'll mention again though what @kiwiLee pointed out, whether you use the method suggested by @LiquidGames or my one, it doesn't take into account the possibility of more than one actor with the same lowest value, in as much as the last checked lowest value actor's attribute will be the one chosen....for example:
numberC, P and BB all have the smallest value, but because numberBB is the last in this group to be checked, that'll be the one deemed the smallest (even though numberC and numberP are equally as small).
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
-
LiquidGameworks Anchorage, AKPosts: 956BTW, I didn't find my project where I had a working system, but I just tested this, and you can use the min function with more than one entry. You can use min(a,c,b,d,x,y) and it'll still find the lowest one. I'm sure this was part of what I was doing with table values.
-
gyroscope I am here.Posts: 6,598
long min function combo.
Do you get fries with that? ;-)
Seriously, good stuff @Manto1
I'm thinking that as min accepts more than 2 values it surely will be the same for other functions I thought only accepted two.... a whole new world awaits me.... :-)
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
Answers
I think I just need to know a simple way to check which one of the five numbers is the smallest. I can do everything else after that.
Well, the user has possibility to add more actors (up to 30). Btw, is it far too much to check 30 attributes?
I have a table which has 30 rows for the attributes.
Change Attribute
smallest = min(tableCellValue( game.Table 1 ,1,1),min(tableCellValue( game.Table 1 ,2,1),min(tableCellValue( game.Table 1 ,3,1),min(tableCellValue( game.Table 1 ,4,1),min(tableCellValue( game.Table 1 ,5,1),min(tableCellValue( game.Table 1 ,6,1),min(tableCellValue( game.Table 1 ,7,1),min(tableCellValue( game.Table 1 ,8,1),min(tableCellValue( game.Table 1 ,9,1),min(tableCellValue( game.Table 1 ,10,1),min(tableCellValue( game.Table 1 ,11,1),min(tableCellValue( game.Table 1 ,12,1),min(tableCellValue( game.Table 1 ,13,1),min(tableCellValue( game.Table 1 ,14,1),min(tableCellValue( game.Table 1 ,15,1),min(tableCellValue( game.Table 1 ,16,1),min(tableCellValue( game.Table 1 ,17,1),min(tableCellValue( game.Table 1 ,18,1), min(tableCellValue( game.Table 1 ,19,1),min(tableCellValue( game.Table 1 ,20,1),min(tableCellValue( game.Table 1 ,21,1),min(tableCellValue( game.Table 1 ,22,1),min(tableCellValue( game.Table 1 ,23,1), min(tableCellValue( game.Table 1 ,24,1), min(tableCellValue( game.Table 1 ,25,1),min(tableCellValue( game.Table 1 ,26,1),min(tableCellValue( game.Table 1 ,27,1),min(tableCellValue( game.Table 1 ,28,1),min(tableCellValue( game.Table 1 ,29,1),tableCellValue( game.Table 1 ,30,1))))))))))))))))))))))))))))))
Even though it looks crazy it's just min functions inside another ones. It was surprising fast.
Hi, I'm surprised it was fast... I mean to say there's no reason why it's working at all....
or what Rules you've used with it to make it work.
Let's take the first one:
change attribute Smallest to min(tableCellValue( game.Table 1 ,1,1) where you are comparing your first value in the table (1) with 1, giving a result, lets call that x.
Then a comma, then the second line min(tableCellValue( game.Table 1 ,2,1) comparing the 2nd table value also with 1, lets call the result x2.
So far you've got Change Attribute smallest to x1,x2 and eventually will have the value of the attribute as a list i.e x1,x2,x3,x4, etc all the way up to 30. You can't have an attribute value to include a comma unless the attribute is a Text attribute. And the function min in GSC only compares two values and no more.
(By the way, because this list of min functions are already in their brackets, you don't need )))))))))))) etc at the end, only ( at the beginning and ) at the end, but even then, you're just bracketing a list of min functions to get a list of values separated by commas which is not accepted as an attribute value as I mentioned.
And even if that did work, not only will you have needed to add Rules to write to your table 30 table entries from your number attributes (because you don't know your actor's numbers), which is extra work, you are still only comparing numbers 1 to 30 with 1. What about comparing all numbers except 2 with 2, and all numbers except 3 with 3 etcetera all the way up to 30....
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
When Attribute: (self.number) = min( scene.Background.A.number , scene.Background.B.number , scene.Background.C.number , scene.Background.D.number , scene.Background.E.number )
-- MoveTo 300,300 (or whatever movement behaviors you want)
Note: The five actors were named A,B,C,D, and E respectively in the example above.
@Manto1 @LiquidGameWorks
Apologies, my mistake. I understand now.
I see from @RThurman comment min function works for as many values as is needed. I thought that as the min function only showed x and y values that it only worked for 2 values.... I learn something new every day...
So your minimum value would be found the way you both suggested... although I still think that the use of tables is unnecessary in this instance, as @RThurman seems to suggest as well...
My version without min would have still worked too, I think, but the use of min function is much more efficient in this case, I see that as well now.
:-)
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps