Low FPS

This forum is currently in read-only mode.
0 favourites
From the Asset Store
High Low is a casino game using luck and points accumulation.
  • Hi there,

    I'm getting low FPS on my game when the object amount reaches around 600. This Is unacceptable really as Its going to go alot higher than that. I'm wondering what procedures i should follow to find out what is causing this high FPS and cut it down.

    I made my sprites under half size, but it seems to not make any difference. In The task manager, it is using 100% of CPU resources, leading me to believe its not a graphics card bottleneck, but a CPU one. Leading me to believe its something in the way i've coded it.

    • Alot of my variables go into an unnecessary amount of floating point digits (i only really need it to 2 decimal places or maybe even 1) Is there a way to set variables to have a maximum amount of DPs?
    • Alot of For eachs.. In some events i have for each object "Ship" for each object "turret" in the same condition. I found this is the only way for it to work... Does this sound inefficient?
    • How do you use the profiler? Could that help me?
  • - Alot of my variables go into an unnecessary amount of floating point digits (i only really need it to 2 decimal places or maybe even 1) Is there a way to set variables to have a maximum amount of DPs?

    This will have no impact on performance at all. The floats are always calculated to the same precision, but to display fewer decimal places, use the FormatDecimal system expression.

    [quote:1gux5b5x]- Alot of For eachs.. In some events i have for each object "Ship" for each object "turret" in the same condition. I found this is the only way for it to work... Does this sound inefficient?

    Yes - nested loops can be very CPU intensive. If you have 600 Ships and 600 Turrets and you do:

    + For Each Ship

    + For Each Turret

    -> Actions

    You are requiring that the actions are run 600 x 600 times, which is 360,000 times (over a third of a million). Repeat this a few times and you're asking the CPU to run millions of iterations!

    Only do loops like that if it is absolutely necessary. What are you doing that requires that code? Often, there are much more efficient solutions.

    [quote:1gux5b5x]- How do you use the profiler? Could that help me?

    The profiler object can be used to time regions of events. All you need to do is call the Begin action, run some events, call End, then the Get Seconds expression returns the number of seconds spent on that code.

  • I had (still have a little bit) similar problems, if you can, move any unnecessary filtering conditions to above the for each loops.

    So that instead of doing it 360000 times it will do it 600 or 1 time per tick.

    Should probably have some warning about these loops on the wiki. I've found it very easy to throw these loops at every problem in an attempt to fix, without thinking about lag. (Though I prob won't now)

    I was thinking the profiler would be a lot more useful if it displayed in the debugger how long each loop was taking. (dunno if this is feasible or not)

  • [quote:1xzwe4yr]Yes - nested loops can be very CPU intensive. If you have 600 Ships and 600 Turrets and you do:

    + For Each Ship

    + For Each Turret

    -> Actions

    You are requiring that the actions are run 600 x 600 times, which is 360,000 times (over a third of a million). Repeat this a few times and you're asking the CPU to run millions of iterations!

    Only do loops like that if it is absolutely necessary. What are you doing that requires that code? Often, there are much more efficient solutions.

    Yeah this is the problem... I disabled the group that had this code in and it run like a dream now. But i'm not sure how to do it without "For Each".

    Basically, Each ship has multiple turrets that are meant to "stick" to its action points. They also have multiple thrusters that stick to the action points. How can i make all the ships have the correct turret and thrusters stuck to their ship?

    So bascially, theres a buch of ship objects, a bunch of turret objects, and a bunch of thruster particle objects. Currently i'm saying for each turret, for each ship, if the UIDofparent value of turret is = to Ships.UID, then set X,Y accordingly.

    Please tell me there is a better way

  • are you scrolling around?, I managed to fix a lot of my lag by adding pick sprite by distance < *some number* above both loops. Is on screen would work too.

    Surely you only need one for each loop there, are you using a compare in the system object instead of a compare private variable on the sprite?

    so you'd have:

    for each ship:

    Turret value 'UIDofparent' = Ships.UID

    You need for each loops to pick from an expression like "Ships.UID" there, but you don't (usually) need them for picking normally.

  • Yeah this is the problem... I disabled the group that had this code in and it run like a dream now. But i'm not sure how to do it without "For Each".

    Basically, Each ship has multiple turrets that are meant to "stick" to its action points. They also have multiple thrusters that stick to the action points. How can i make all the ships have the correct turret and thrusters stuck to their ship?

    Use containers.

  • Can i still use containers when its uncertain how many turrets are wanting to be created and stuck to the ship at different points?

    eg. 1 ship may have 3 turrets, and 4 thrusters

  • Can i still use containers when its uncertain how many turrets are wanting to be created and stuck to the ship at different points?

    eg. 1 ship may have 3 turrets, and 4 thrusters

    No.

    Maybe. How about every ship always has it's max number of thrusters/turrets already attached, but the player doesn't know they're attached because the ones that aren't supposed to be attached are just invisible/deactivated? That way you can have your same number of objects in every container (required for containers to work) and you still have the appearance of different numbers of thrusters/turrets.

    Of course, I don't know the specifics of your game, this idea might not work at all with how you have things set up. Or it may not work at all. I'm just speculating.

  • You can't have multiple instances of the same object in a container - but you could do that if they're all different object types.

    As faggatron said, you should be able to do it with a single for-each: the 'Turret UID = Ship UID' condition has an internal for-each - it will test every turret and pick the ones whose UID is equal to the current Ship's UID. Conditions like this are much faster than using another for-each condition, though (it's optimised C++ code instead of a condition doing the iteration).

  • It would be faster to do this:

    for each ship

    turret : private variable 'parentOID' equals ship.UID

    Set turret's x,y

    Then comparison condition selects all the turrets which belong to the ship, and the action will be applied to each selected instance. Construct re-evaluates the parameters for each selected instances of the turret in the action.

    That will probably be a fair bit faster. But failing that, if it's still slow, you might want to try this approach:

    Always - hide turrets

    ship is in playfield

    turret : private variable 'parentOID' equals ship.UID

    Set turret's x,y

    Make turret visible

    If its likely that theres only going to be a couple of spaceships on the screen at once.

  • Deadeye, will that method save processing time? What i mean is, do you think containers are so much faster that having some invisible objects will still be faster that using the current method? I'd probably only have a maximum of 5 turrets per ship so it could work. Does this mean i have to make different objects for each of those 5 turrets? Or can i multiple instances?

    Also, i got rid of a bunch of the For Each conditions and the code is now more efficient with it still working, however its still HELLA slow After creating 40 ships, it drops from 200FPS to 40FPS. (Better than the 30fps i had before).

    Can i do better in optimization? Are containers faster? I would have hoped I wouldn't get any FPS drop from this...

  • Try deleting sections of your events (after a backup!) and finding what the culprit is. Is it still the for-each events? If so, how many of them are there and what do they look like?

  • Deadeye, will that method save processing time? What i mean is, do you think containers are so much faster that having some invisible objects will still be faster that using the current method?

    Honestly I really have no idea if it would be faster, I was just suggesting something else to try besides loops.

    I am wondering something though... do your ships really need separate turrets? Like, is it purely cosmetic or do the turrets themselves take damage/get destroyed? I'm asking because maybe you can fake it with different ship sprites. Like, have one animation for your ship that has one turret, have a second animation that has two turrets, a third with three, etc. It would cut down on the total number of objects dramatically.

    It seems to me that even without loops you're eventually going to hit a wall where you start getting sucky fps. Six hundred ships, each with multiple guns, each gun firing multiple bullets... that adds up pretty quick even without loops. It just sounds to me like you're going to have to make some sacrifices and cut some corners somewhere. Be as frugal as possible with your assets while still retaining the core of your gameplay idea.

  • I made an example using a for loop and comparing distance etc.

    Works pretty good

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Although I'm starting to think now that an 'object pairer' plugin might be a better direction to go.

    So you could just create something like this:

    Loop 600 times
            Create spaceship
            Loop number of turrets 
                          Create turret   
                          Pair turret to spaceship[/code:1pdae0ze]
    
    And then when you want to position all the objects you would just do
    
    [code:1pdae0ze]Pair: Loop each
               Set turret position to spaceship[/code:1pdae0ze]
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)