{"author_name":"csanyk","cat":"LD #35","comments":[],"epoch":1461718200,"likes":1,"metadata":{"p_key":"5774","p_author":"csanyk","p_authorkey":"10473","p_urlkey":"41222","p_title":"Shape Struggle 1.1.0.6 post-compo: performance optimized","p_cat":"LD #35","p_event":"LD35","p_time":"1461718200","p_likes":"1","p_comments":"0","p_status":"UPD5","us_key":"10473","us_name":"csanyk","us_username":"\/author\/csanyk\/","event_start":"1460678400","event_key":"33","event_name":"LD35"},"text":"<p>&nbsp;<\/p>\n <p>I&#8217;ve gone over the code for Shape Struggle, and optimized it for performance \u00a0On my machine, I can&#8217;t get it to drop frames in the latest post-compo build, 1.1.0.6. \u00a0The thing is, my laptop is pretty high-end (2.8GHz Xeon E3-1505M, 64GB RAM), so I&#8217;m not sure how well it runs on older machines&#8230; I need testers!<\/p>\n <h2 style=\"text-align: center\"><a href=\"http:\/\/ludumdare.com\/compo\/ludum-dare-35\/?action=preview&amp;uid=10473\">&gt;&gt;&gt; Play and Rate Shape Struggle &lt;&lt;&lt;<\/a><\/h2>\n <p>I figure my optimization tricks might help other developers, so in brief, here&#8217;s how I did it:<\/p>\n <ol>\n <li>Controller object: \u00a0I knew even before the compo ended that I needed to set up a controller object to optimize the performance of the up to 500 instances of oEnemy that I&#8217;d have active in the game. \u00a0But the code in the controller object from the 1.0 build was far from optimized. \u00a0I refactored and streamlined the code, eliminating redundant conditional checks, and pulling code out of loops to make them as tight as possible.\n <p>I also pulled some Step event code out of the two most complex objects, oSquare an oPentagon, and put it into the controller object. \u00a0Doing so enabled me to write some of the code in the controller so that it runs once for all instances of oSquare and oPentagon, rather than once <em>per<\/em> instance. \u00a0 This makes a <em>huge<\/em> difference in the amount of calculations necessary when there are hundreds of instances active. \u00a0No matter how many there are, there&#8217;s only one calculation performed.<\/li>\n <li>Simplified bullet object. \u00a0At maximum firepower, the player fires 5 bullets per shot, one shot every three steps. \u00a0At 60fps, that&#8217;s 100 bullets per second. \u00a0That&#8217;s 100 creation events per second. \u00a0I thought I should limit the number of bullets, and also I wanted to limit the range of the player&#8217;s weapon, so I put a timer in the bullet object that destroyed the bullet instance after 1 second.\n <p>Potentially, I might have optimized this by implementing object pooling, so that rather than creating and destroying bullets, I would re-use the instances so I wouldn&#8217;t need to keep creating and destroying them. \u00a0But first, I tried removing the timer, and found that the game plays and feels exactly the same, so that timer code doesn&#8217;t matter. \u00a0Normally setting a variable and then decrementing it doesn&#8217;t take a lot of CPU. \u00a0But with up to 100 fewer creation events every second, and 100 fewer timers to decrement every step, it adds up.<\/p>\n <p>Object pooling might yield still more performance optimization, but simplifying the objects as much as possible first before making such efforts is smart.<\/li>\n <li>I also removed the collision check from the bullet object that checks for collisions with the wall, and put it in the wall. There&#8217;s only 8 wall instances, so 8 collision checks vs. up to 100 each step means a substantial savings in calculations.<\/li>\n <\/ol>\n \n <p>Tags: <a href=\"http:\/\/ludumdare.com\/compo\/tag\/csanyk\/\" rel=\"tag\">csanyk<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/ld35\/\" rel=\"tag\">LD35<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/optimization\/\" rel=\"tag\">Optimization<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/post-compo\/\" rel=\"tag\">post-compo<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/shape-struggle\/\" rel=\"tag\">Shape Struggle<\/a><\/p>","time":"April 26th, 2016 7:50 pm","title":"Shape Struggle 1.1.0.6 post-compo: performance optimized"}