Freeze before spawn

0 favourites
  • 15 posts
  • Hello everyone.. in my space game i have a obkect that is a explosion animation .. int that object i have 17 diferent animations with about 40 frames each.

    So when i spawn it i do a floor(random(17)) to create a variation of the explosion.

    But when it does the game freezes for a bit.

    Is there a option to preload all animations?

  • That's way too many frames for a simple explosion! Why do you need 17 animations?! Just use one or two, randomly flip, mirror, rotate, apply some effect and you can save lots of memory!

    Also, you can make very resource-efficient explosions with particles. (see Particle Explosions template in C3)

  • The reason is to have a very good visual effect.

  • Hello everyone.. in my space game i have a obkect that is a explosion animation .. int that object i have 17 diferent animations with about 40 frames each.

    So when i spawn it i do a floor(random(17)) to create a variation of the explosion.

    But when it does the game freezes for a bit.

    Is there a option to preload all animations?

    There are three reasons it might freeze,

    1) Not enough rendering power on your device in which case you want to optimize the frames and lower the quality of them and adjust frame size based on your game design screen ratio. One mistake most people do when they create a game, is forget to optimize their assets, which saves a lot of memory usage, for images i always use services like tinypng, which compresses the images by 80% or more in some cases, for example from a batch of 20 files at 50mb i get back 2/3mb size batch file. Which is insane, and in the same time very important for game memory usage, your game runs faster, higher fps etc.

    2) The 2nd reason it might freeze, is that your load animation event the floor(random(17)) trigger to pick the animation, might run in a loop. Make sure it triggers only once when the bomb explodes, or car explodes or whatever it is. Instead of floor(random(17)) i would use choose(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) its less computation power for the runtime to calculate, in choose(123...) the runtime picks one item from list 1 time, in floor(random(17)) the runtime has to pick a random number including fractions like 1.111111 ,2.3543677 and then discard the decimal to make it a round fixed number.

    3) I mentioned above resizing your images based on your game design screen ration, for example if you have a game that is running on a 864x480 layout, and your animation is like 32x32px then that should be the frame inside the animation, if you have frames that are 256 x256 and you use C2 to resize them down to 32px that might also cause your freeze, because when the animation loads Construct has to resize all frames in that animation in 1-10 ticks to 32px.

  • There is practically no difference in performance between using floor(random(17)) or choose(1,2,3...17).

    You can try them in "Repeat 1000000 times" loop and you'll see that both expression take the same (very little) time to compute.

  • dop2000 There is practically no difference in performance between using floor(random(17)) or choose(1,2,3...17).

    You can try them in "Repeat 1000000 times" loop and you'll see that both expression take the same (very little) time to compute.

    the idea of optimizing was mostly meant for mobiles, while on computers you wont see any difference, when it comes to mobiles, you will see there is a big difference in performance especially when you load heavy assets, and have a non-optimize code of logic. Think on mobiles as a 10% of ur computer power. while there are mobile phones that can beat a Pentium 2 or even some low end today's laptops on memory their power is not stable is just fluctuating. So having a clean code would help a lot. As you said, pc performance difference of code from floor to choose is not noticeable, and agree on that, but i think is a difference on mobile, atleast from my past experience (last 4 years). Also keep in mind not everybody has a workstation to build their games on like me and you, their probably running on some normal computer build of <300$. And not everybody has the latest iphone or samsung.

  • I agree with what you are saying, but have you actually tried running random() and choose() a million times and comparing them? I have tried it just now on my very low-end android phone (which probably costs about $20 now).

    1,000,000 calculations of round(random(17)) take about 5 seconds.

    1,000,000 calculations of choose(1,2,3...,17) take about 6 seconds.

    I really doubt that igalencar needs to spawn one million explosions at once, and even if he was, the difference in performance of these two math functions would be the least of his worries.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I guess you guys are wrong or the documentation is wrong..

    floor(random(14))

    i go from steps 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14

    round numbers.

    But i guess im wrong

  • Yeah, random() starts with 0. So floor(random(3)) is the same as choose(0,1,2), but it will never equal 3.

  • dop2000 I agree with what you are saying, but have you actually tried running random() and choose() a million times and comparing them? I have tried it just now on my very low-end android phone (which probably costs about $20 now).

    1,000,000 calculations of round(random(17)) take about 5 seconds.

    1,000,000 calculations of choose(1,2,3...,17) take about 6 seconds.

    I really doubt that igalencar needs to spawn one million explosions at once, and even if he was, the difference in performance of these two math functions would be the least of his worries.

    90% agree on that (since my entire point was about optimization), for sake of argument, im not going to write 100 pages of how C3 calculates things, and what to take in consideration convince yourself

    Here is the 1000.000 loop example , you will also say that the 20% -80% difference in cpu usage has no effect when comes to 14 animations, and i have a argument against that also(which is based on how C3 calculates things, as any html5 engine, however it should not freeze your computer at 14 animations, but would significantly increase your cpu usage causing more load for a spike of a moment added to other cpu utilization ur app does in same time, that might cause ur game to freeze.), but im going to wait for your reply not anticipate it.

    He asked why his animation freezes, and all i said is depends on multiple variations basically but a bit more specific. One of the reason being the way he coded the logic to pick that animation, 2nd being the compression the images he uses have, 3rd being depending on the device, 4th various input add here.

    You still didn't said if the problem was solved or not??? igalencar Also if the problem persists, a screen shot of that call and before and after wait calls or every 2 seconds stuff, will help a lot to identify the problem.

  • I will skip past all this logic debate and move to the logical question, what is the file size of the image with its animation set? A possible problem, the images are all to scale as 1024x1024 which would easily be 256k per image minimum, the that x 40 for the frames, even with those resized in the layout, that won't change file size.

    Thus logically we need to start simple, what is the size of the images in memory, 4K? 256k? 1mb?

    A 40 slide animation with each image at 4K = neglable affect in loading

    A 40 slide animation with each image 256k = performance hit

    As far as I see, C3 caches the animation to play then plays it after cache

  • > dop2000

    You still didn't said if the problem was solved or not??? igalencar Also if the problem persists, a screen shot of that call and before and after wait calls or every 2 seconds stuff, will help a lot to identify the problem.

    Yes it was solved. i down size of images from my explosions effects

  • Here is the 1000.000 loop example , you will also say that the 20% -80% difference in cpu usage

    You are doing it wrong.. There is no point in checking cpuutilization one million times inside of the loop, because the loop is done in one tick (however long). And cpuutilization will return the CPU load for the previous second, that was before the loop.

    Here is the more correct way (both events run on every tick):

    I'm getting maybe 2-5% lower CPU utilization with choose(). Yes, it's a bit more efficient, but only if you need to use it hundreds of thousands times every second. Definitely not worth worrying about if you need to spawn just one explosion.

  • dop2000

    You are doing it wrong.. There is no point in checking cpuutilization one million times inside of the loop, because the loop is done in one tick (however long).

    The point of fetching the cpu util during each loop was to get the highest main thread across the entire loop run, without waiting for the above condition to finish. And the whole point of 1 million times check was cause you suggested such a test. lol...

    Anyway .... good debate though we learned a lot didn't we?!

  • The point of fetching the cpu util during each loop was to get the highest main thread across the entire loop run, without waiting for the above condition to finish.

    And I'm telling you, this is wrong :)

    cpuutilization in that loop returns exactly the same value a million times. And that value is useless, because it shows what cpu utilization was before the loop started (when the app was idle).

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)