Low FPS

New releases and general discussions.

Post » Sat Sep 27, 2008 7:07 pm

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?
B
2
S
2
G
5
Posts: 448
Reputation: 2,546

Post » Sat Sep 27, 2008 7:20 pm

[quote="alee":1gux5b5x] - 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?[/quote:1gux5b5x]
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?[/quote:1gux5b5x]
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?[/quote:1gux5b5x]
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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Sat Sep 27, 2008 9:20 pm

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)
B
2
S
2
G
5
Posts: 236
Reputation: 2,122

Post » Sun Sep 28, 2008 12:30 pm

[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.[/quote:1xzwe4yr]

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 :D
B
2
S
2
G
5
Posts: 448
Reputation: 2,546

Post » Sun Sep 28, 2008 12:55 pm

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.
B
2
S
2
G
5
Posts: 236
Reputation: 2,122

Post » Sun Sep 28, 2008 1:24 pm

[quote="alee":33ucfh9k]
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?[/quote:33ucfh9k]

Use containers.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Sun Sep 28, 2008 1:28 pm

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
B
2
S
2
G
5
Posts: 448
Reputation: 2,546

Post » Sun Sep 28, 2008 2:16 pm

[quote="alee":3a6y0ov3]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[/quote:3a6y0ov3]

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.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Sun Sep 28, 2008 2:18 pm

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).
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Sun Sep 28, 2008 2:39 pm

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.
B
4
S
2
G
5
Posts: 641
Reputation: 3,011

Next

Return to Construct Classic Discussion

Who is online

Users browsing this forum: No registered users and 1 guest