Optimizing a Function-Heavy Sonic Platform Engine

Get help using Construct 2

Post » Sun Dec 01, 2013 10:20 am

So, last time I tried to get help when it came to a optimizing my Sonic-based platform engine, the focus was collision checking. Turns out I was looking at the wrong area, because, apparently, that wasn't the actual problem.

For refernence:
- engine .capx (requires C2 Standard, obviously)
- Playable Engine Test

So, it seems the major CPU bottleneck is fairly obvious, now that I've rechecked the CPU profile in the debug:



Yikes.

Yeah, that's more than a bit much. For reference, the Pre- and Post- movement sections only call a couple of functions each tick, and the former modifies a couple of variables. Those two functions are "Player.Reposition" and "Player.Collision", the former of which properly aligns the position of the player sensors, and the second runs though every sensor and checks if they're colliding with something, by ALSO using a function for each sensor. And there's eight different sensor functions. "Movement" itself does plenty of function calls to both above functions depending on the circumstances and to other functions as well. Sensor Routines and Movement Routines in general does a LOT of function calls.

Unfortunately, this seems to be incredibly inefficient in C2, whereas it's perfectly fine in Classic, probably because of the way the two work differently.

So, yeah. I'm somewhat at a loss as to what to do. The whole thing is rather complicated, and I wouldn't know where to start in order to optimize the whole thing.Candescence2013-12-02 06:06:52
B
94
S
37
G
11
Posts: 404
Reputation: 11,275

Post » Sun Dec 01, 2013 5:16 pm

Your files don't seem to be linked properly... :/
B
10
S
1
Posts: 163
Reputation: 1,436

Post » Mon Dec 02, 2013 6:07 am

Whoops! Turns out I accidentally missed them when composing my post. Fixed, sorry about that!
B
94
S
37
G
11
Posts: 404
Reputation: 11,275

Post » Mon Dec 02, 2013 10:38 pm

very impressive use and trick for slope and mouvement ;)

B
85
S
14
G
6
Posts: 72
Reputation: 7,237

Post » Mon Dec 02, 2013 11:59 pm

Well I noticed it looks like your abandoned the the Platformer behaviour. Possible becuase you need too for some of Sonics abilities. But doing so puts a lot more stress on your cpu code.

You should figure another way to cut out the WHILE loop in movment and try to only run it once per tick. I haven't sit down to see how many times per tick it will run. But I can imagaine that it's running a few hundred times. And is likely the reason it accounts for %16.

Also I noticed you call Player.Repo and Collision with both on Evertick.

you should run a single every tick and put them in order once. As it looks now If assuming these are running in order.

Position/Collision
Movement while loop
position/Collision

ok. se thats the supposed run order. Now imagine 3 ticks of that

Position/Collision
Movement while loop with position/Collision
position/Collision
-
Position/Collision
Movement while loop with position/Collision
position/Collision
-
Position/Collision
Movement while loop with position/Collision
position/Collision

notice how poosition/collision call functions are right after each other. Is that really needed?

now I did some testing. You can disable the entire post-move and have no negative gameplay effect i noticed. And this saved apx 8%

I then tried leaving the post animation on and turned all the other collision and position checks prior tot he post and managed to get the the group from 36% to 16%. However this comes with that the animation starts going off. Character doesn't rotate as smoothly. There is some room for improvement :)jayderyu2013-12-03 00:00:43
B
90
S
18
G
9
Posts: 2,455
Reputation: 15,018

Post » Mon Dec 09, 2013 11:49 am

Alright, here's what I've got when trying to tweak the engine:

1. The timescale has been set to 1, because the old way it worked, for some bizarre reason, doesn't stayconstant.The movement works slightly better as a result.
2. The pre-movement function calls areentirelyunnecessary.Now, removing both the pre- and post- movement function calls pretty much sorta breaks the movement, but removing just one set seems to have no negative effects, making the whole thing slightly more efficient.
3. Now, you'd think just having one set of calls for Player.Reposition and Player.Collision once every frame would be fine, right? So just removing those calls from the main movement group would be fine, right? Nope! The whole thingcrashes upon startupif those calls are removed. I'm serious. By all rights, it shouldn't be capable ofdoing that.You'd think it would simply cause the movement engine to spazz out or something, but instead, it crashed! It would be impressive it were not so frustrating.

I'm pretty much calling this engine a lost cause at this point, point no. 3 just makes it seem fundamentally broken. I'm likely going to have to explore new avenues at this point.
B
94
S
37
G
11
Posts: 404
Reputation: 11,275

Post » Mon Dec 09, 2013 12:20 pm

I remember using Sonic Worlds a lot when I used to use MMF2!
Looking at this capx reminds me EXACTLY of the layout of the events from the MMF2 Sonic Worlds.
Not that I'm exactly genius on all of this, but I can see a lot of events that are very badly optimised (the same things @jayderyu picked up on).
Mostly avoid repeated Collision checks. I know it's difficult to sparse collisions, but some checks aren't necessary, such as, whilst Sonic is moving right (XSpeed>0), it's not really needed to check the "Left" sensor if it's overlapping a wall (Sure, it could create problems such as if a moving wall was coming towards sonic whilst he's moving right slowly, it may not push him, but I'm thinking "Basic" Sonic engine :P )
"Every Tick" with lots of math involving Sin and Cos, probably is a bit overly taxing on the CPU (Even general calculators lag for seconds when you do multiple sin calculations in one go), I guess to avoid that though is a bit complicated, as Sonic needs to detect stuff constantly as he's running.
I haven't got time to analyse or write any more right now :( but I hope I gave some sort of useful insight!
B
51
S
20
G
10
Posts: 571
Reputation: 9,819

Post » Mon Dec 09, 2013 1:11 pm

Honestly, at this point, I'd say it would be easier to just find someone who could make a custom behavior. It would certainly be more efficient, I think, and probably easier to work with when it comes to stuff like multiplayer, due to the lack of need for picking with all those freaking sensors.
B
94
S
37
G
11
Posts: 404
Reputation: 11,275

Post » Mon Dec 09, 2013 1:29 pm

Don't give up, man! It's your baby. And just like all babys it is not always cute and innocent. Sometimes it just cries for no obvious reason. But you as the daddy have to find out what the crying is for.
And at least you're learning a lot about optimizing - and about all the things you could have done in the beginning ;)
Optimizing in this case means being familiar with how C2 works (best), so that you can avoid redundant events, functions or checks.
Again, don't leave your baby alone crying xD
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Mon Dec 09, 2013 1:56 pm

@Candescence It's easier than you'd think to make multiplayer. You could have as many different players as you desire using only one set of sensors (each player would have their own "Player" sensor, but all the slopes, walls, ceiling censors). In the most simplist way, it would literally require a "ForEach" loop that sends a function that does all the movement events, with a function parameter of the Player's UID. Sure there might be a hickup or two, but trust me, it's very straight forwards (a lot more straightforward than optimisation stuff!).

Listen to tulamide! Don't abandon this, you've managed to get very far already, it's further than I ever got in my few attempts of making a "Sonic" engine in C2 (I have tried a few times and only end up making a ball that can walk up slopes and stick to them using trig, then I gave up lol). If you're stuck, just keep asking questions on here, people here love to help others :P
B
51
S
20
G
10
Posts: 571
Reputation: 9,819

Next

Return to How do I....?

Who is online

Users browsing this forum: atmas, Bootfit and 32 guests