performance with nested Otherwise statements

jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
edited April 2012 in Working with GS (Mac)
I finished coding my entire nested otherwise statements for automatic level creation, and it's killed the level loading time. when I manually place the buildings in the level it takes 3.2 seconds to load. when my level directors, it takes over 14.

I'm not sure i've done this the most efficient way, has anyone else had a large number of otherwise statements?

what I have right now is 7 positions. each position has it's own actor with 60 nested otherwise statements. I tried having everything contained within a single actor, but it was taking a minute to open it each time.

for example in actor 1, I have two groups:

if buildingNumber < 30 and if buildingNumber > 30

and in each of those, I have rules that go like this:

if buildingNumber = 1
spawn building1
otherwise
if buildingNumber = 2
spawn building2
otherwise
if buildingNumber = 3
spawn building3
otherwise
.....

so on and so forth until I hit 30, then the next rule group takes over.

is there a better way to do this that is more efficient? I'm hoping that once the new engine comes out that it will solve problems like this, but i'd rather not wish on the wind. I've read that having nested statements is better than 60 separate if statements, but this is no picnic.

is there a better way to do this?

Best Answer

Answers

  • App SurgeApp Surge Member Posts: 651
    @jonmulcahy
    I am currently using the same general idea, to check whether items have been purchased in the in-game store. However, I haven't found a different way to do it, but I'd love it if someone else has!

    Henry
  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
    yea, it's a huge performance hit.

    each levelDirector actor I have in the game increases the loading time by 1 second, and increases the engine RAM by 8MB.

    I really want to be able to dynamically create levels based on table entries, but if that is not going to work, I'll have some restricting to do.
  • tenrdrmertenrdrmer Member, Sous Chef, Senior Sous-Chef Posts: 9,934
    I have done a string of I think about 18 otherwises I didn't notice any hit on the performance at all but it was just a string that determined what animation to run at the time.
  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
    @tenrdrmer

    yea, i have 7 actors with 2 groups of 30 each for a total of 420 if statements.

    i wonder if i spilt it up further so each actor has 4 groups of 15? i dunno.
  • mynameisacemynameisace Hull, UKMember Posts: 2,484
    Have you tried spawning the actor with all the otherwise rules in? Just have a controller actor with nothing in other than to spawn the one with the crazy amount of otherwise's?

    May help so that it's not preloading all those rules?

    Ace
  • POMPOM Member Posts: 2,599
    edited April 2012
    @jonmulcahy

    Hey i believe a better way of doing this is not spawning at all and not nesting rules at all !
    Put all your building actors in the scene - off screen .
    And in every one of them give a rule :
    When "game.start a new level" IS true
    Change "self.position.X" and "self.position.Y" to the desired position .
    Now i suggest making a Table with all the info for the X and Y values so that the actors can take the position data from that table .

    Also you need to make it so that if there are actors that are not being used in that level, they won't execute their rules, and they'll be off screen.
    The best way of doing this is:
    You have an "index" attribute for the level number right ?
    So this is how your actor rules will look like -

    *************************
    When - "game.start a new level" is True
    Change "self.position.X" and "self.position.Y" to a table value .

    now a new rule :
    When "self.position.X" is grater than 0
    When "self.position.X" is lower than 480
    When "self.position.Y" is grater than 0
    When "self.position.Y" is lower than 320
    Be active .
    Otherwise -
    Don't be active.
    *************************

    Hope it helps Jon

    Photobucket Roy
  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
    @mynameisace - I have not tried that. I'll give that a shot

    @P-O-M - I may have to go down that route eventually, but I want to try and get this dynamic thing working first for 2 reasons. I want to have 200 levels, and it's a lot easier to fill out a table with the level layout than manually placing 100+ building pieces each level :) AND I really want to have a survival mode in the game where it will automatically generate random levels until you fail to complete one.

    If I can't get the nested performance issue solved. I'll scrap loading from the table and just drop my building constructor in each level. that actor creates the building based on it's location, so that will be a lot better.

    Thankfully I did a ton of table work before starting on coding, so I've got a very large table with each piece's width and height as well as it's relative position to every other building piece, so I've got a few options to deal with this.
  • bloomerbloomer Member Posts: 53
    What device are you playing the game on? This is a roundabout way of me asking - in general, has anybody seen anything they've tried that has caused performance hits on ipad games, as opposed to iphone or android games?
  • Rob2Rob2 Member Posts: 2,402
    You don't need to use otherwise at all - just a straight run of rules idk it may be considerably more efficient

    if buildingNumber = 1
    spawn building1
    if buildingNumber = 2
    spawn building2
    if buildingNumber = 3
    spawn building3 etc etc
  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
    @bloomer - this is on an ipad3 which makes me very nervous about performance on the original iPad.

    @rob2 - I've always seen that having nested if statements is better performance because once it hits the right trigger, it stops executing everything.

    Im going to do some playing around tonight to see if I can make it any better
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited April 2012

    @jonmulcahy > Hi Jon, I'd agree with Rob, ( @Rob2 ); you surely won't get faster performance than a list of if's in this case, surely? Just a thought...

    ----------------------------------------------
    http://davidgriffinapps.co.uk/
  • MotherHooseMotherHoose Member Posts: 2,456
    edited April 2012
    agree with @Rob2 … as the rules are processed linearly …

    couldn't figure out whether level completion > sceneChange or just levelChange

    … did try to demo some of this: http://www.mediafire.com/?m1l7181dht0326k
    may not help … but hopefully it is a start or pointer in the right direction

    @};- MH
  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
    Well I just spent 2 hours detesting everything, and there is no real performance improvement. I think I might have to scrap this for now.
  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
    of course I did! :)
  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
    Ugh', this is a pain

    I think I've come up with a work around solution which requires a bunch more work per level, but should dramatically increase performance. At least as long as we use the lua engine.

    Got some work ahead of me tonight!
  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
    Still have ot found the best way of doing this easily. It's a huge pain in the ass
Sign In or Register to comment.