Legacy GM Game gets very laggy when there are a lot of enemies

sylvain_l

Member
Well, this is the only structure I could come up with that worked. I tried simply using direction and speed, but there was no way to get them to stop ending up stuck inside each other.
when you are running your game at 30 fps, making collision checks at each step make generally sens. At 120fps, you should be able to pass some from time to time without real noticeble effect if you aim at the one whose speed are low.
things like bullets can move fast enough that it make sens. but if something is moving at 30pixels/sec at 120fps your are only moving 1px every 4 step.

also for the get stuck effect, you can have an unstuck(). (often when thing are stuck it's because of a pixel overlap, not like it will hurt much the visual experience of player if you correct that )

"stuck" also can be good, if an ennemy decide to to walk along/on a wall, stuck them on a path and so get rid of all collision checks with wall. (for what I have seen of your game just running it 30sec most of your ennemies tend actually to move along wall once they hit it.)

to be honest you can gain a lot by making your object a little more collaborative. For now it's still a bit over my head. but just a direction if you want to look at. (also seeing your game it feels to me that it can greatly improve the behaviour of the ennemies group)
in groups of fish or birds, they don't need to be aware of the positions of all the others in the group to move in group and avoid collision. just knowing and checking the movement of the 5-7 nearest is enough and ajusting to that is enough. and for bird in arrow or line formation, just adjusting to the 2 nearest is enough.
 

Simon Gust

Member
just knowing and checking the movement of the 5-7 nearest is enough and ajusting to that is enough. and for bird in arrow or line formation, just adjusting to the 2 nearest is enough.
Question: Is there an efficient way to get the nearest instances that doesn't already require looking up every instance to figure out which are the closest?
 

sylvain_l

Member
Question: Is there an efficient way to get the nearest instances that doesn't already require looking up every instance to figure out which are the closest?
In his game I suspect it's possible (at least for what I've seen of the first room, not sure for others :p)

as his instances comes from spawner (I assume) outside the view.
they are spawn in groups. and all aims to move to a unique target: the player in the center.
also as for the first current room it's divided with wall for a large part in 4 sectors clearly distinct, it sure is:


not sure what would be best, certainly someone can come with better, but my first idea would be faking with 4 different pseudo child of the ennemies for each sector spawn so for those far from players you only check with those from same spawn sector. For those near the player I suspect checking with the ennemies main object should be necessary again in case one run into one from another sector.
(but wrinting this, I remind me of something I read I think somewhere in the gmc forum, about spliting room in zones and doing the collision check inside those zones (cose if 2 object aren't in the same zone they shouldn't collide right ? :p things was a bit tricky I thing with the broder when you move from one zone to another...)
 

Simon Gust

Member
In his game I suspect it's possible (at least for what I've seen of the first room, not sure for others :p)

as his instances comes from spawner (I assume) outside the view.
they are spawn in groups. and all aims to move to a unique target: the player in the center.
also as for the first current room it's divided with wall for a large part in 4 sectors clearly distinct, it sure is:


not sure what would be best, certainly someone can come with better, but my first idea would be faking with 4 different pseudo child of the ennemies for each sector spawn so for those far from players you only check with those from same spawn sector. For those near the player I suspect checking with the ennemies main object should be necessary again in case one run into one from another sector.
(but wrinting this, I remind me of something I read I think somewhere in the gmc forum, about spliting room in zones and doing the collision check inside those zones (cose if 2 object aren't in the same zone they shouldn't collide right ? :p things was a bit tricky I thing with the broder when you move from one zone to another...)
I was more thinking of creating artificial instance loops using arrays. Collision would be a bit more complicated though.
 

sylvain_l

Member
@Simon Gust
well, game is much more fun and complex that what I got with my first try of the game.
When I get used to the control, knowing the AI behaviour (first I thought fact they get stuck in mass in corner, etc... was silly, but better try to clean them that let them mass cause when you need to flee you'll be doomed), finding my way through all the map not just doging around the middle and also finding that I can buy more ammo and other stuff...
good job @EndermanTamer13 ;)

my idea of pseudo child was clearly nuts.

I was more thinking of creating artificial instance loops using arrays. Collision would be a bit more complicated though.
not sure I understand.
you mean instance loops of arrays like in some kind of quadtree K-tree(*) method?

(*)as often I mix algorythms ^^' to quote Mike Collisions are now much faster as it uses a K-Tree (I think) if it's already implemented no use to do it ourselves.
 
Last edited:
E

EndermanTamer13

Guest
In his game I suspect it's possible (at least for what I've seen of the first room, not sure for others :p)

as his instances comes from spawner (I assume) outside the view.
they are spawn in groups. and all aims to move to a unique target: the player in the center.
also as for the first current room it's divided with wall for a large part in 4 sectors clearly distinct, it sure is:


not sure what would be best, certainly someone can come with better, but my first idea would be faking with 4 different pseudo child of the ennemies for each sector spawn so for those far from players you only check with those from same spawn sector. For those near the player I suspect checking with the ennemies main object should be necessary again in case one run into one from another sector.
(but wrinting this, I remind me of something I read I think somewhere in the gmc forum, about spliting room in zones and doing the collision check inside those zones (cose if 2 object aren't in the same zone they shouldn't collide right ? :p things was a bit tricky I thing with the broder when you move from one zone to another...)
cose if 2 object aren't in the same zone they shouldn't collide right ?
See, the enemies move fast enough that enemies coming from different "sectors" of the room will interact actually quite frequently, so...

when you are running your game at 30 fps, making collision checks at each step make generally sens. At 120fps, you should be able to pass some from time to time without real noticeble effect if you aim at the one whose speed are low.
things like bullets can move fast enough that it make sens. but if something is moving at 30pixels/sec at 120fps your are only moving 1px every 4 step.
How low of a speed counts as low speed? Most of the enemies move at a speed of at least 2 pixels per frame at 120fps.

also as for the first current room it's divided with wall for a large part in 4 sectors clearly distinct, it sure is:


not sure what would be best, certainly someone can come with better, but my first idea would be faking with 4 different pseudo child of the ennemies for each sector spawn so for those far from players you only check with those from same spawn sector. For those near the player I suspect checking with the ennemies main object should be necessary again in case one run into one from another sector.
Also, this won't work, because I plan to make more maps for this game and have it randomly select one when you start it, so these maps probably won't have these "sectors".

well, game is much more fun and complex that what I got with my first try of the game.
When I get used to the control, knowing the AI behaviour (first I thought fact they get stuck in mass in corner, etc... was silly, but better try to clean them that let them mass cause when you need to flee you'll be doomed), finding my way through all the map not just doging around the middle and also finding that I can buy more ammo and other stuff...
good job @EndermanTamer13 ;)
Also, thanks! I've just kinda had all these different ideas for games and they weren't complex enough to put in a game by themselves, so I just decided to put them all into one game, and I think it works quite well!



Also, I would like to point out this very important thing: obj_scout, obj_bounce, obj_tankscout, and obj_tankbounce, (as well as the two splitmini objects) don't chase the player, and instead roam aimlessly. They do not appear to cause ANY lag, so I would not worry about those enemies.
 
Last edited by a moderator:
E

EndermanTamer13

Guest
Guys! I just had the most (possibly) genius idea ever. It's so crazy it just might work. I may have found a way to prevent objects from going inside each other without requiring ANY collision events for those instances. I moved the code from the collision events to scr_collide when they DO run into these objects, and replaced other with xx and yy like so:

Code:
///scr_collide(xinst,yinst,xvec,yvec)
xx = argument0;
yy = argument1;
xv = argument2;
yv = argument3;

if xx = id or xx = noone{
    x += xv;
}else{
    var dir = point_direction(xx.x,xx.y,x,y);
    x += lengthdir_x(4,dir);
}

if yy = id or yy = noone{
    y += yv;
}else{
    var dir = point_direction(yy.x,yy.y,x,y);
    y += lengthdir_y(4,dir);
}
I am hoping and praying that this works! ;)

EDIT: It kinda works, the enemies are very jittery now, and tend to push each other around more forcefully than before... not sure how to fix that, and make it like before.

SECOND EDIT: Yeah, this method is too glitchy, and although it has less lag, it doesn't really work at all like I want it to, so I'm going to revert it back to how it was and find some other way to improve it.

I can't really think of any other way to fix the lag, and the only other method I can think of is kind of a last resort. 120fps may just simply not be the right kind of room speed for this game, and maybe I need to bump it down to 60fps and do the tedious work of changing all the values accordingly. But, it makes me feel like if I did that, I would be basically giving up, and I don't want to do that. I'd really like to have this game run at 120fps, but unless you guys can help me come up with some other way to have them behave the way I want to without lag, I might just have to change the game to 60fps. :(
 
Last edited by a moderator:
I

icuurd12b42

Guest
going through the code. I see a ton of duplicate calls to functions in the same script that use the same parameter... I'm sure people suggested to streamline these to store the result in a variable and reuse that result. point_distance, instance_nearest, all collision check functions are really slow and should not be abused...

also, distance_to_point and distance_to_object are more costly than point_distance

I did not see anyone suggest you enable fast collision
upload_2018-1-7_2-47-53.png

I also have a fast collision system in my assets on the store TMC SGS you may find improves performance
 
E

EndermanTamer13

Guest
going through the code. I see a ton of duplicate calls to functions in the same script that use the same parameter... I'm sure people suggested to streamline these to store the result in a variable and reuse that result. point_distance, instance_nearest, all collision check functions are really slow and should not be abused...

also, distance_to_point and distance_to_object are more costly than point_distance

I did not see anyone suggest you enable fast collision
View attachment 15594

I also have a fast collision system in my assets on the store TMC SGS you may find improves performance
Oh, wow! I did not realize there was such an option. Thank you! Also, I never really realized only calling a function once and using its output in a variable was more efficient, and that some functions are more costly than others. I just though they all ran so fast it might as well be instantaneous! But now I realize that I have to make things efficient, and I will do that from now on! :) I'll go ahead and optimize everything.

Yes! It pretty much worked! The game now stays in the 110-120fps range from what I've seen so far, which works for me! :D
 
Last edited by a moderator:
Top