Page 1 of 1

Looking for a Perofmrance Boost

PostPosted: Thu Jun 07, 2012 2:42 pm
by Hblade
Mostly, my game spikes sometimes between 30 and lower FPS (Game default is 30), and it spikes when Im in a room full of Goblins, so it has to be the code in my goblins thats lagging.

I have WAY TOO MANY if statements, but I have no idea what do change in it to make it work otherwise.

I would like it if someone can increase the performance of this code for me. :) Thanks! +1 to whoever does it.
Code: Select all
if(transp==0)
{
if(distance(x, y, Player.x, Player.y)<=220)
{
    gTimer++;
    if(gTimer==15)
    {
        CreateActor("rockchuck", "Rock", "(none)", "(none)", 0, 0, false);
        gTimer=0;
    }
    if(Player.x>x)
    {
        x+=3;
        ChangeAnimation("Event Actor", "gobRight", NO_CHANGE);
    }
    if(Player.x<x)
    {
        x-=3;
        ChangeAnimation("Event Actor", "gobLeft", NO_CHANGE);
    }
}
else {
    switch(animindex)
    {
        case 0:
            ChangeAnimation("Event Actor", "GivStandRight", NO_CHANGE);
        break;
        case 1:
            ChangeAnimation("Event Actor", "GobStandLeft", NO_CHANGE);
        break;
    }
    aTimer+=rand(3);
    if(aTimer>=60)
    {
        aTimer=0;
        switch(animindex)
        {
            case 3:
                ChangeAnimation("Event Actor", "GivStandRight", NO_CHANGE);
            break;
            case 2:
                ChangeAnimation("Event Actor", "GobStandLeft", NO_CHANGE);
            break;
        }
     }
     }

 
 
 
 
if((x<=(Player.x+3) && x>=(Player.x-3)) && gJump==0)
{
     gJump=1;
     yvelocity=-12;
}
 
 
if(yvelocity<5)
{
    yvelocity++;
}
if(yvelocity>3)
{
    gJump=1;
}
g+=25;
b+=25;
}
if(eHP<=-3&&cTimer==0)
{
    cTimer=1;
    transp=.9999;
    yvelocity=0;
    xvelocity=0;
    PlaySound2("data/enemDefeated.wav", 1.000000, 1, 0.000000);
}
safeDraw();


safeDraw() is this code and its used for every actor outside of the view
Code: Select all
void safeDraw()
{
    if((xscreen>640||xscreen<0) || (yscreen>480||yscreen<0))
    {
        VisibilityState("Event Actor", DISABLE);
    }
    else {
        VisibilityState("Event Actor", ENABLE);
         }
}

Re: Looking for a Perofmrance Boost

PostPosted: Thu Jun 07, 2012 3:17 pm
by DarkParadox
Well I messed around with your code here and brought the main code you posted down to one if() function, and made all the others switch() using min() and max() functions. I'm not exactly sure how much this will improve the efficiency, but surely removing so many more-thans and less-thans would do something, right? (I'm not even sure this will work actually, since I just did it quickly in a text editor. Go for it and tell me if anything screws up!)

Code: Select all
switch((transp*100))
{
case 0:
switch(min(distance(x, y, Player.x, Player.y),221))
{
    case 221:
    break;

    default:
    gTimer++;
    switch(gTimer)
    {
        case 15:
        CreateActor("rockchuck", "Rock", "(none)", "(none)", 0, 0, false);
        gTimer=0;
    }
    switch(max(Player.x,x))
    {
        case x:
        break;
   
        default:
        x+=3;
        ChangeAnimation("Event Actor", "gobRight", NO_CHANGE);
    }
    switch(min(Player.x,x))
    {
        case x:
        break;
   
        default:
        x-=3;
        ChangeAnimation("Event Actor", "gobLeft", NO_CHANGE);
    }
    break;
}
else {
    switch(animindex)
    {
        case 0:
            ChangeAnimation("Event Actor", "GivStandRight", NO_CHANGE);
        break;
        case 1:
            ChangeAnimation("Event Actor", "GobStandLeft", NO_CHANGE);
    }
    aTimer+=rand(3);
    switch(max(aTimer, 59))
    {
        case 59:
            break;
       
        default:
        aTimer=0;
        switch(animindex)
        {
            case 3:
                ChangeAnimation("Event Actor", "GivStandRight", NO_CHANGE);
            break;
            case 2:
                ChangeAnimation("Event Actor", "GobStandLeft", NO_CHANGE);
            break;
        }
     }
     }




switch(gJump)
{
case 0:
if(x<=(Player.x+3) && x>=(Player.x-3))
{
     gJump=1;
     yvelocity=-12;
}
break;
}

switch(min(yvelocity, 5))
{
    case 5:
        break;
    default:
    yvelocity++;
}
switch(max(yvelocity, 3))
{
    case 3:
        break;
    default:
    gJump=1;
}
g+=25;
b+=25;
break;
}

switch(min(eHP, -2))
{
    case -2:
        break;
    default:
        switch(cTimer)
        {
            case 0:
            cTimer=1;
            transp=.9999;
            yvelocity=0;
            xvelocity=0;
            PlaySound2("data/enemDefeated.wav", 1.000000, 1, 0.000000); 
        }
}

safeDraw();

Re: Looking for a Perofmrance Boost

PostPosted: Thu Jun 07, 2012 3:29 pm
by Hblade
Thanks for the attempt xD but:
errors.png



These can be fixed though using (int) before.

also remove the case x: those don't work xD

im fixing the cod eyou just sent and going to post it



EDIT:
now the only error I get is line 32, something about a missing ;

but thats the "else" statement o.O

Lookie:
Code: Select all
    switch(((int)transp*100))
    {
    case 0:
    switch((int)min(distance(x, y, Player.x, Player.y),221))
    {
        case 221:
        break;

        default:
        gTimer++;
        switch(gTimer)
        {
            case 15:
            CreateActor("rockchuck", "Rock", "(none)", "(none)", 0, 0, false);
            gTimer=0;
        }
        switch((int)max(Player.x,x))
        {
       
            default:
            x+=3;
            ChangeAnimation("Event Actor", "gobRight", NO_CHANGE);
        }
        switch((int)min(Player.x,x))
        {
       
            default:
            x-=3;
            ChangeAnimation("Event Actor", "gobLeft", NO_CHANGE);
        }
        break;
    }
    else {
        switch(animindex)
        {
            case 0:
                ChangeAnimation("Event Actor", "GivStandRight", NO_CHANGE);
            break;
            case 1:
                ChangeAnimation("Event Actor", "GobStandLeft", NO_CHANGE);
        }
        aTimer+=rand(3);
        switch((int)max(aTimer, 59))
        {
            case 59:
                break;
           
            default:
            aTimer=0;
            switch(animindex)
            {
                case 3:
                    ChangeAnimation("Event Actor", "GivStandRight", NO_CHANGE);
                break;
                case 2:
                    ChangeAnimation("Event Actor", "GobStandLeft", NO_CHANGE);
                break;
            }
         }
         }




    switch(gJump)
    {
    case 0:
    if(x<=(Player.x+3) && x>=(Player.x-3))
    {
         gJump=1;
         yvelocity=-12;
    }
    break;
    }

    switch((int)min(yvelocity, 5))
    {
        case 5:
            break;
        default:
        yvelocity++;
    }
    switch((int)max(yvelocity, 3))
    {
        case 3:
            break;
        default:
        gJump=1;
    }
    g+=25;
    b+=25;
    break;
    }

    switch((int)min(eHP, -2))
    {
        case -2:
            break;
        default:
            switch(cTimer)
            {
                case 0:
                cTimer=1;
                transp=.9999;
                yvelocity=0;
                xvelocity=0;
                PlaySound2("data/enemDefeated.wav", 1.000000, 1, 0.000000);
            }
    }

    safeDraw();




Lol hey I created an "error Paper" texture in GIMP using that last image xD its neat :P
error_paper.png

Re: Looking for a Perofmrance Boost

PostPosted: Thu Jun 07, 2012 3:42 pm
by DarkParadox
Madness!, anyways, switches don't have "else" statements, which is what default is for, so I just chopped that out and put default there. The problem is though is that I forgot that switch doesn't take stuff like "x", sigh.

That means this code here won't work, since default just means "anything". The point was to have it break out of the code if it returned the value of "x" instead of "player.x".
Code: Select all
        switch((int)min(Player.x,x))
        {
       
            default:
            x-=3;
            ChangeAnimation("Event Actor", "gobLeft", NO_CHANGE);
        }


Barring that giant screw up, this fixes that else{} statement.
Code: Select all
    switch(((int)transp*100))
    {
    case 0:
    switch((int)min(distance(x, y, Player.x, Player.y),221))
    {
        case 221:
        break;

        default:
        gTimer++;
        switch(gTimer)
        {
            case 15:
            CreateActor("rockchuck", "Rock", "(none)", "(none)", 0, 0, false);
            gTimer=0;
        }
        switch((int)max(Player.x,x))
        {
       
            default:
            x+=3;
            ChangeAnimation("Event Actor", "gobRight", NO_CHANGE);
        }
        switch((int)min(Player.x,x))
        {
       
            default:
            x-=3;
            ChangeAnimation("Event Actor", "gobLeft", NO_CHANGE);
        }
        break;
        default:
        switch(animindex)
        {
            case 0:
                ChangeAnimation("Event Actor", "GivStandRight", NO_CHANGE);
            break;
            case 1:
                ChangeAnimation("Event Actor", "GobStandLeft", NO_CHANGE);
        }
        aTimer+=rand(3);
        switch((int)max(aTimer, 59))
        {
            case 59:
                break;
           
            default:
            aTimer=0;
            switch(animindex)
            {
                case 3:
                    ChangeAnimation("Event Actor", "GivStandRight", NO_CHANGE);
                break;
                case 2:
                    ChangeAnimation("Event Actor", "GobStandLeft", NO_CHANGE);
                break;
            }
         }
         }




    switch(gJump)
    {
    case 0:
    if(x<=(Player.x+3) && x>=(Player.x-3))
    {
         gJump=1;
         yvelocity=-12;
    }
    break;
    }

    switch((int)min(yvelocity, 5))
    {
        case 5:
            break;
        default:
        yvelocity++;
    }
    switch((int)max(yvelocity, 3))
    {
        case 3:
            break;
        default:
        gJump=1;
    }
    g+=25;
    b+=25;
    break;
    }

    switch((int)min(eHP, -2))
    {
        case -2:
            break;
        default:
            switch(cTimer)
            {
                case 0:
                cTimer=1;
                transp=.9999;
                yvelocity=0;
                xvelocity=0;
                PlaySound2("data/enemDefeated.wav", 1.000000, 1, 0.000000);
            }
    }

    safeDraw();

(Gonna retype this into something that might fix the "x" problem)

Re: Looking for a Perofmrance Boost

PostPosted: Thu Jun 07, 2012 3:45 pm
by Hblade
lol line 31 duplicate switch xD

its the default: thing, should I just say 0 instead?

Edit: the goblins dont move xD

Either way it still didn't help with the lag so nvm xD

Re: Looking for a Perofmrance Boost

PostPosted: Thu Jun 07, 2012 4:51 pm
by skydereign
A couple of if statements won't cause lag. You could have a hundred actors with a couple of if statements in their draw events and still not have lag. And while removing unnecessary ifs is a good idea, it isn't usually the big concern when dealing with efficiency. You want to be able to remove unnecessary draw events, collision events, and really any type of event. You want to control the number of actors on screen and off screen. Of course by doing this you will almost always have to improve your code by lowering the number of ifs or other decisions the game has to make.

Re: Looking for a Perofmrance Boost

PostPosted: Thu Jun 07, 2012 5:05 pm
by Hblade
Thanks sky, the collision thing could be re-worked, I think its whats causing the lag.