How to check which actor's number is smallest?

MantoManto Member Posts: 796
edited August 2012 in Working with GS (Mac)
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.

Best Answers

  • KiwiLeeKiwiLee Posts: 181
    Accepted Answer
    Lots 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?
  • KiwiLeeKiwiLee Posts: 181
    Accepted Answer
    You 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!
  • LiquidGameworksLiquidGameworks Anchorage, AKPosts: 956
    Accepted Answer
    If you have your values are in a table, you can do a table check using the min function.
  • gyroscopegyroscope I am here.Posts: 6,598
    edited August 2012 Accepted Answer
    Good 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

  • LiquidGameworksLiquidGameworks Anchorage, AKPosts: 956
    edited August 2012 Accepted Answer
    @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.
  • gyroscopegyroscope I am here.Posts: 6,598
    edited August 2012 Accepted Answer

    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

    Well, the user has possibility to add more actors (up to 30). Btw, is it far too much to check 30 attributes?
    If you're using the method I suggested, that'd be no problem, just carry on...

    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

  • LiquidGameworksLiquidGameworks Anchorage, AKPosts: 956
    Accepted Answer
    BTW, 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.
  • gyroscopegyroscope I am here.Posts: 6,598
    Accepted Answer
    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

  • MantoManto Member Posts: 796
    I want the actor which number is the smallest to jump in specifed position.

    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.
  • MantoManto Member Posts: 796
    Thanks for your answers! I'll be trying those today.

    Well, the user has possibility to add more actors (up to 30). Btw, is it far too much to check 30 attributes?
  • MantoManto Member Posts: 796
    edited August 2012
    I got an idea from the min function and tables. I'll probably be using this (if you don't have a better way).

    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.
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited August 2012

    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

  • LiquidGameworksLiquidGameworks Anchorage, AKMember, Sous Chef Posts: 956
    @Gyroscope Now that I look at it again, how I presented that only does show two values. I do know that I had gotten a simple system down before (I was using an orbit system, and a blackhole would only pull in the closest object, no matter the distance), so now you have me furiously looking for that lost sample file. :P
  • MantoManto Member Posts: 796
    edited August 2012

    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.
    That one doesn't compare anything yet. It's just min(tableCellValue(Table,Row,Col). After that comes the part it's gonna compare it to.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    edited August 2012
    Based upon the previous hints I'd suggest unlocking each actor and using something like this:

    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.
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited August 2012

    @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

  • MantoManto Member Posts: 796
    @gyroscope That's what I though too. That's why I made that long min function combo.
  • MantoManto Member Posts: 796
    @gyroscope Ahh. Didn't think about other functions. It would be great to know all the secrets :)
  • LiquidGameworksLiquidGameworks Anchorage, AKMember, Sous Chef Posts: 956
    @gyroscope These types of threads are the best, and good for everyone. I love trying to sort out new ways of doing things, particularly since this issue is one that can be commonly used in games. And I know you can use attributes, I'm just a tables whore now. :P
  • MantoManto Member Posts: 796
    I'm using tables with this because I have 3 types of actors. 30 of each the two types. And 10 of the one type. The table will be shared for all of them.
Sign In or Register to comment.