Optimizing code

0 favourites
  • 12 posts
From the Asset Store
Two levels with two sistem Wave. with Source-code (.c3p) + HTML5 Exported.
  • Hey

    I'm doing a condition that check if an object overlaps another and the one with Y higher is set above to the other.

    but right now this condition is consuming a lot of FPS when there's a lot of objects (testing with 100)

    the condition is simple:

    sprite1: is overlaping sprite1

    sprite1:pick nearest to (450, layoutheight)    ------- sprite 1:move to top layer

    note: The ideal way is check only his y position but didn't found a condition like that so I added a random X value in the pick nearest event.

    without the event the game runs with smooth fps(50+fps) with 100 objects, with the event on, it drops to 10fps.

    Any idea how to optimize this event? the problem is probably checking every object overlaping another one..

  • What are you trying to achieve? If it's z-ordering based on y position, try this plugin: scirra.com/forum/plugin-isometric-z-ordering-based-on-y_topic46247.html

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • What makes sense to overlap the same sprite that you mention sprite 1 overlaps sprite 1?

    I think you should to post your .capx so we need to know what is clearly problem

  • What are you trying to achieve? If it's z-ordering based on y position, try this plugin: scirra.com/forum/plugin-isometric-z-ordering-based-on-y_topic46247.htmlfrom few tests i made with this plugin it seems to work very good with a lot of objects.. =D

    bug I had to add a new layer only for the objects I want to change his y according to his Y position..

  • If you test "Sprite is overlapping Sprite" with 100 Sprites, Construct 2 must test for a collision between every combination of pairs of Sprites, which is about 100 x 100 = 10,000 collision checks per tick, which at 60 FPS is trying to do 600,000 collision checks per second. That kind of thing doesn't even run well in C++ engines, you're just asking for way too much processing to be done.

    A simple way around it is to maybe only check every 1 second, or come up with a simpler way of trying to do what you're doing.

  • If you test "Sprite is overlapping Sprite" with 100 Sprites, Construct 2 must test for a collision between every combination of pairs of Sprites, which is about 100 x 100 = 10,000 collision checks per tick, which at 60 FPS is trying to do 600,000 collision checks per second. That kind of thing doesn't even run well in C++ engines, you're just asking for way too much processing to be done.

    A simple way around it is to maybe only check every 1 second, or come up with a simpler way of trying to do what you're doing.ty for reply.

    I thought this was the problem as i mentioned.. the plugin working good for now but the ideal would be achieve the same performance without plugin

  • the problem is your checkin constantly and its not somethin thats gonna happen constantly. You should only check in instances where it might change and then only once as long as it doesnt change again.

  • Unfortunately, computers just are not capable of performing 600000 collision checks a second... sorry ;)

  • the problem is your checkin constantly and its not somethin thats gonna happen constantly. You should only check in instances where it might change and then only once as long as it doesnt change again.any idea about how to do that?

  • I was wondering along the same lines- My game spawns bad guys and they sometimes spawn on top of each other as they are programmed to randomly spawn outside of the screen and come into the screen every random number of seconds. For every new bad guy this adds another factorial to the amount of checks of overlapping that I would have to do. That is if there are five bad guys spawning- there are 5! number of ways in which they can spawn on top of each other- this gets really large really fast to write if this guy is over lapping this guy etc. I am new to programming games. SO I was wondering if there is some tool in construct 2 that can call all objects of a certain type a group name like ->ENEMY and then make it so they can't spawn on top of each other???? The thing is they have to be able to overlap with objects in the same layer as they are programmed to animate when they die- and the animation is an object in the same layer.   I was also wondering if anyone knows how to make a color always on the bottom- with most spriting and animating there is a color that is always on the bottom- or every other color gets displayed if this color (say white) ever overlaps with other colors so that objects can overlap (like a plane flying over an island). I thought initially that the layers would suffice but since the sprite image is always a square then sprites which aren't squares have a color on the outside- like white.

  • I was wondering along the same lines- My game spawns bad guys and they sometimes spawn on top of each other as they are programmed to randomly spawn outside of the screen and come into the screen every random number of seconds. For every new bad guy this adds another factorial to the amount of checks of overlapping that I would have to do. That is if there are five bad guys spawning- there are 5! number of ways in which they can spawn on top of each other- this gets really large really fast to write if this guy is over lapping this guy etc. I am new to programming games. SO I was wondering if there is some tool in construct 2 that can call all objects of a certain type a group name like ->ENEMY and then make it so they can't spawn on top of each other???? The thing is they have to be able to overlap with objects in the same layer as they are programmed to animate when they die- and the animation is an object in the same layer.   I was also wondering if anyone knows how to make a color always on the bottom- with most spriting and animating there is a color that is always on the bottom- or every other color gets displayed if this color (say white) ever overlaps with other colors so that objects can overlap (like a plane flying over an island). I thought initially that the layers would suffice but since the sprite image is always a square then sprites which aren't squares have a color on the outside- like white.

    When you mean group of objects probably it's the family function you want to take a look, it's a premium feature so you need to have the paid version of C2.

    After you made a family of objects you can call events with that family instead of individual sprites.

  • I was also wondering if anyone knows how to make a color always on the bottom- with most spriting and animating there is a color that is always on the bottom- or every other color gets displayed if this color (say white) ever overlaps with other colors so that objects can overlap (like a plane flying over an island). I thought initially that the layers would suffice but since the sprite image is always a square then sprites which aren't squares have a color on the outside- like white.

    That's actually a somewhat older way of doing things. I recall GameMaker used to let you pick a color for transparencies. These days though, it's best to just use transparent PNG files for your images.

    Besides red, green and blue values, PNG files can have an alpha value. That not only designates that a particular pixel can be seen through, but it can be seen through to a particular degree.

    If you take a look at many of the examples that come with C2, you'll see a bunch of such images. You just need an image editor that can export that format, like Photoshop or GIMP.

    Just to add to this discussion too, I think optimization is an art and science itself. I'm by no means adept at it, but have the highest regard for those who know how to optimize code.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)