• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Movement code optimization ideas?

RyanC

Member
Hi everyone, does anyone have any ideas how this code can be optimized, as it's eating up 30% off my game time.

Code:
/// movement and collsion

alarm[2] = 1 // step frequency


// gravity
if ! place_meeting(x,y+grav,obj_floor) {
    gravity = grav
}
else {

    gravity = 0
    vspeed = 0

    if grav move_contact_all(270,24) else move_contact_all(90,24) 
 
    y-=grav // move off floor to use faster code below
}


var xtarg = x+lengthdir_x(move_speed,move_dir);
var ytarg = y+lengthdir_y(move_speed,move_dir);
 
if ! place_meeting(xtarg,ytarg,obj_floor) {
    x = xtarg;
    y = ytarg;
}
else {
    vspeed = -move_speed*image_yscale   
}

I normally make the step frequency less frequent but when I do this my enemies are jumping off the floor and don't move smoothly.
 

Simon Gust

Member
Are you sure this takes 30%? What is your average ms per step? In the profiler select the "t" button and average ms should be displayed next to it.
 

RyanC

Member
Are you sure this takes 30%? What is your average ms per step? In the profiler select the "t" button and average ms should be displayed next to it.

Yes very sure.
Average ms listed next to steps at the top of the profile is around 0.55
 

Simon Gust

Member
Yes very sure.
Average ms listed next to steps at the top of the profile is around 0.55
30% of 0.55 isn't very alarming at all.
Is there some other stuff going on in your game that has a higher percentage?
How many objects of obj_floor exist in the current room?
Are there any other instances executing similar code?
 

RyanC

Member
30% of 0.55 isn't very alarming at all.
Is there some other stuff going on in your game that has a higher percentage?
How many objects of obj_floor exist in the current room?
Are there any other instances executing similar code?
I was just concerned because my draw pipeline is always the most intensive and is the reason I'm excluding lots of android devices, but this new code is 3 times more than the draw code.
Less than 50 floor objects.
There are around 3 or 4 of these instances at any one time.
 
Z

zendraw

Guest
just put it in the step event, + try it without the move_contact codes also remove lengthdir functions and replace them with custom hspeed/vspeed ones.
 

RyanC

Member
just put it in the step event, + try it without the move_contact codes also remove lengthdir functions and replace them with custom hspeed/vspeed ones.
Wow! you're right, the code doesn't even need move_contact!

custom hspeed/vspeed ones? not sure what you mean here?
 

JackTurbo

Member
60 fps is equal to around 16ms per step, so 0.55ms seems ok to me? Or is it that you worried about how this will translate to weaker devices? Have you tried testing this on the kind of devices you have in mind?

One thing to bear in mind in case you're looking at fps in regards to optimisation, is that it isnt a linear gradient.
Each fps lost actually results exponentially more time per step. So say your prototype gets 1000 fps and adding some code drops that to 800 fps, that means your step time has gone from 1ms to 1.25ms.

Where as if it where running at 200 fps, you'd have a step time of around 5ms. If you then added the same code with the same performance hit (0.25ms) your fps would actually only drop to around 190.

I know for me I find it helpful to keep this in mind when I'm adding features, as it can be a bit alarming when a feature seems to lose you so many frames. Which can make premature optimisation such a serious temptation.
 

RyanC

Member
yes I've tested on some devices. Samsung Galaxy S7 is the best one I've tried and no problems there.

The amazing thing is that the game will run fine on my Nexus 7 2012 with 120+ fps but on a Lenovo tablet that has twice theRAM and the same speed processor it strugles to get 30fps.

I think this is to do with devices that are on Marshmallow, any device that's old and running Marshmallow lags for games big time, and not just talking about my games either. (I'm considering not supporting Marshmallow at all!)
 
Z

zendraw

Guest
i meant to remove it to see how it performs without it, when debuggin i remove bits of the code to see what changes so im more aware what goes on. for custom hspeed/vspeed i meant to try and change the way you move the instance, i personally try to avoid lengthdirs, i think theyr expensive.
 
Top