Anyway to know if a number is even?

Hello,

Lets say I have 20 actors and they all have their own unique id between 1-20. Is there a way of telling the actor that its unique id is an even number? so 2,4,6,8,10,12,14,16,18,20.

The range will be varying throughout so cannot really be a static solution.

Thanks,
Keith

Comments

  • FallacyStudiosFallacyStudios Member Posts: 970
    edited February 2014
    %2 = 0 or 1
  • SocksSocks London, UK.Member Posts: 12,822
    edited February 2014
    Another way, if you divide the number in two and the result is the same as floor(result) then it's even.


    Should look something like this:

    if attribute Number = (floor(Number/2))*2 . . . .
  • beefy_clyrobeefy_clyro Member Posts: 5,394
    Thanks guys, so simple when you don't over think it!
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    Thanks guys, so simple when you don't over think it!
    I think a better way might be to first see if the number is prime. If so, it's not even. So embed another rule to check if it's less than two. If so, it's not even. Well, unless you have negative numbers, so add a behavior to change the attribute to max(2,attribute) first. If you divide the number by 3 you can tell if it's divisible by 3 but since some even numbers are divisible by 3 that won't rule it out completely. Then maybe if you... =))
  • beefy_clyrobeefy_clyro Member Posts: 5,394
    Did you just mind freak me @tatiang … I'm feeling rather baffled! haha
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    Glad I could help! b-(
  • beefy_clyrobeefy_clyro Member Posts: 5,394
    You actually have … just not here ;) I have just converted your Spawn unique instances to positions demo to fit my needs :)
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    Did you just mind freak me @tatiang … I'm feeling rather baffled! haha
    It's a Monday... sorry couldn't resist!
    You actually have … just not here ;) I have just converted your Spawn unique instances to positions demo to fit my needs :)
    Oh good! I had a lot of fun making that demo.

  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273
    Another way, if you divide the number in two and the result is the same as floor(result) then it's even.


    Should look something like this:

    if attribute Number = (floor(Number/2))*2 . . . .
    %2 = 0 or 1
    Nice bits of code! Bookmarked incase I need these...
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited February 2014
    @Braydon_SFX The nice thing about @Socks' method is that it's also the way you "snap to grid":

    Change attribute self.position.X to prec(self.position.X/game.gridSize,0)*game.gridSize
  • beefy_clyrobeefy_clyro Member Posts: 5,394
    Man I need to expand my brain :p

    I'm about to create a new thread, look out for it, sure you guys will have some way to make me feel utterly stupid again for not thinking of it (i've been having a week of this kind of thing).
  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273
    @Braydon_SFX The nice thing about @Socks' method is that it's also the way you "snap to grid":

    Change attribute self.position.X to prec(self.position.X/game.gridSize,0)*game.gridSize
    Yep! Very nice!
  • quantumsheepquantumsheep Member Posts: 8,188
    Because I'm stupid and hate maths, I came up with another method :D

    Make a table with 20 rows.

    In column 1, put a '1' in the row of each 'even' number row. Leave the prime number ones at 0.

    e.g.

    1: 0
    2: 1
    3: 0
    4: 1
    5: 0
    6: 1
    7: 0
    8: 1

    Select a random number between 1 and 20.

    In your rule, use the first part to check the table cell value based on your random number, and the '1' to check if it's even.

    e.g.

    If tablecellvalue(table,your random number,1) = 1

    Then you have an even number.

    This only works in pro with the ability to put stuff on both sides of a rule though.

    My mind is not normal - I came up with this as a way of being able to decide which level certain things happen on e.g.

    Table has 100 entries - each row represents a level.

    If I want boss characters to appear every 10 levels, I mark where I want them to appear with a '1' (row 10, 20, 30, 40, etc) and check every time the player moves on to a new level.

    Works a treat, and is much simpler than having 'if level =10' or 'if level =20' or 'if level =30' etc etc


    QS =D
  • beefy_clyrobeefy_clyro Member Posts: 5,394
    Ah yes that would work, unfortunately, in what I needed it wouldn't. The rows in my table will always be changing as its being populated by retrieving data from a web server, I would never know how many rows to fill out. Everything has its own unique id so socks method worked perfect for me
  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    If you use an index attribute there is no chance of getting a negative number.
Sign In or Register to comment.