Jump to content

Photo

This script crashes the ZQ compiler.


  • Please log in to reply
7 replies to this topic

#1 MoscowModder

MoscowModder

    Run for your life.

  • Script DB Staff
  • Gender:Male
  • Location:Not where you'd think

Posted 28 April 2012 - 02:53 PM

Whenever I try to compile this code, ZQ stops at step 5 (the object code), freezes, and then crashes.

Any ideas why??

CODE
//Usage: Press/hold 'L' to charge energy
//This does not consume any magic
//The color of the meter is based on the element equipped to 'B'
//If you release L, the charge ends and nothing happens
//The 1st and 2nd meters only show up if you have the 2nd and 3rd tier spells of the equipped element

//int I_ELEMENTS[5] = {151,147,148,149,150}; //The element items, in the order L,F,W,A,E
int I_ELEMENTSPELLS[15] = { 57, 125, 127,  //Light: L.Arrow, L.Aura, Supernova
                            10,  25,  64,  //Fire: Flame/F.Wall, Fireball, Inferno
                             0,   0,   0,  //Water: ???, ???, ???
                            23,  91,   0,  //Air: Whirlwind/S.Wind, Boost jump, ???
                            54,   0, 124}; //Earth: Tremor, ???, M.Healing
int elementColors[5] = {134, 132, 168, 177, 120}; //Colors of the elements (for the meter)
const int chargeRadius1 = 10; //Radius of tier-2 charge meter
const int chargeTime1 = 55; //Time to charge tier-2 spells
const int chargeRadius2 = 13; //Radius of tier-3 charge meter
const int chargeTime2 = 130; //Total time to charge tier-3 spells

int spellChargeMeter = 0; //Global variable storing spell charge state

//Shared by all five elements
ffc script spellChargeFFC{ //Supposed to be global; using as FFC for testing
    void run(){
        spellChargeMeter = 0;
        while ( true ){
            spellCharge();
            Waitframe();
        }
    }
}

void spellCharge(){
    int elementEquipped;
    if ( Link->InputL && !PD_cooldown ){
        elementEquipped = -1;
        for ( int i = 0; i < 5; i++ ) //Find what element is equipped
            if ( GetEquipmentB() == I_ELEMENTS[i] ){
                elementEquipped = i;
                break;
            }
        //If Link is equipping an element and owns the tier-2 spell of that element
        if ( elementEquipped > -1 && Link->Item[I_ELEMENTSPELLS[arr2D(elementEquipped,1,3)]] ){
            spellChargeMeter++;
                    
            //If Link also has tier-3 spell and charge meter is over the tier-1 charge, draw the next meter
            if ( Link->Item[I_ELEMENTSPELLS[arr2D(elementEquipped,2,3)]] && spellChargeMeter > chargeTime1 ){
                if ( spellChargeMeter > chargeTime2 )
                    spellChargeMeter = chargeTime2;
            }
            else if ( spellChargeMeter > chargeTime1 ) //If the tier-3 spell isn't owned, cap the meter at chargeTime1
                spellChargeMeter = chargeTime1;
                    
            Game->Counter[CR_RUPEES] = spellChargeMeter;
        }
    }
    else{
        if ( spellChargeMeter ) //Dissipate charge
            spellChargeMeter -= 3;
        if ( spellChargeMeter < 0 ) //If charge is negative, set to 0
            spellChargeMeter = 0;
    }
            
    //Now, draw the meter(s)
    if ( spellChargeMeter )
        drawMeter ( elementColors[elementEquipped], chargeRadius1, spellChargeMeter, chargeTime1 );
        if ( spellChargeMeter > chargeTime1 )
            drawMeter ( elementColors[elementEquipped], chargeRadius2, spellChargeMeter-chargeTime1, chargeTime2-chargeTime1 );
}

//Draws a circular meter around Link of the defined color
void drawMeter ( int color, int radius, int curVal, int maxVal ){
    if ( curVal >= maxVal ){ //If current value is over the max, draw a full circle
        Screen->Circle(7, Link->X+8, Link->Y+8, radius, color, 1, 0, 0, 0, false, 128);
    }
    else{
        Screen->Arc(7, Link->X+8, Link->Y+8, radius, 0, curVal/maxVal*360, color, 1, 0, 0, 0, false, false, 128);
    }
}


#2 LinktheMaster

LinktheMaster

  • Site Staff
  • Real Name:Matt
  • Gender:Male
  • Location:Kentucky

Posted 28 April 2012 - 03:08 PM

Okay. Time to teach you some troubleshooting techniques. icon_razz.gif Well, I'll teach you the main technique that will come in handy here.

Basically, it's process of elimination in a sense. The basic premise is to remove chunks of your code to see what section of code is causing the problem. To do this, it's best to start big, then go further and further into the code to find out the precise place that is causing the crash.

Obviously some things to keep in mind is that you can't remove stuff that might cause other parts of your code to not compile. So try not to remove variable or function declarations. If you do, make sure you remove all code that references said variables or functions.

Again, start big, then work your way down.

For example, let's look at your code. You may want to start off by just clearing out your code in the spellCharge function. By removing the content inside of it, your code will still compile, but the function just won't do anything. If ZQuest doesn't crash, then you know the problem lies within the spellCharge function. If not, you'll need to look elsewhere.

After that, you could maybe remove the first if else chain, leaving if(spellChargeMeter) and all of its contents. If the code compiles, then the problem lies within the first if else chain. If it doesn't, then the problem lies within if(spellChargeMeter).

Keep on going until you find the exact line that's the problem. icon_smile.gif

Sorry, I just don't have the time to go through your code to find something that may be causing the problem since in all honesty, it can be something minute. But troubleshooting practice is something that every self-respecting coder needs to know anyway. icon_razz.gif

Note: This isn't really easily doable in ZQuest's script editor and should be done in an external editor through cutting, pasting, and undoing. Just a bit of a warning.

#3 MoscowModder

MoscowModder

    Run for your life.

  • Script DB Staff
  • Gender:Male
  • Location:Not where you'd think

Posted 28 April 2012 - 03:37 PM

Thanks for the tips! I narrowed it down to the if...else block inside the function.
I still don't know what exactly is the problem, as the exact same code worked fine when it was in the global/FFC script itself (I've used it as both for testing purposes). Since the complete setup works without dumping the code into an outside function, I'll leave it as is.

And yes, I use Notepad++ for all of my Zscripting.

#4 LinktheMaster

LinktheMaster

  • Site Staff
  • Real Name:Matt
  • Gender:Male
  • Location:Kentucky

Posted 28 April 2012 - 03:43 PM

No problem. icon_razz.gif Debugging and troubleshooting is something any coder has to face, so knowing some tricks to make it easier is always nice. I have a few more tricks if you're interested.

Well, you can always go further and figure out which exact line is causing the issue. I have a hunch it's those long Link->Item[] lines, but I'm not sure exactly.

#5 Saffith

Saffith

    IPv7 user

  • ZC Developers
  • Gender:Male

Posted 28 April 2012 - 03:59 PM

Not unlikely. We've had issues like that with arrays before. Used to be that anything in the form a[b[x]] would crash; maybe there are still some problems with that. Making a new variable for the array index should fix it, if that's the case.

#6 tox_von

tox_von

    Zelda Addict

  • Members
  • Real Name:Redgor
  • Gender:Unspecified
  • Location:Toxicville , Simcity

Posted 28 April 2012 - 06:14 PM

it only crashed once for me.

arr2D

this needs to be declared how many possibilitys does it have

i think the brackets are wrong also

!PD_cooldown this should be changed to !PD_cooldown = 0 or something like that.

and PD_cooldown and I_ELEMENTS need to be inted

Edited by tox_von, 28 April 2012 - 06:16 PM.


#7 Gleeok

Gleeok

    It's dangerous to dough alone, take this.

  • ZC Developers
  • Real Name:Pillsbury
  • Gender:Male
  • Location:Magical Land of Dough

Posted 28 April 2012 - 06:33 PM

Looks like it could be a problem with user arrays as indices to built-in get/set arrays. hmm..

..MoscowModder: I believe you should be able to fix this crash in the meantime by using:
CODE

int index = I_ARRAY[ Arr2D(n,y,x) ];
Link->Item[index] = wazzle.


Try that and see if the crashes persist.


[edit]
QUOTE(Saffith @ Apr 28 2012, 02:59 PM) View Post

Not unlikely. We've had issues like that with arrays before. Used to be that anything in the form a[b[x]] would crash; maybe there are still some problems with that. Making a new variable for the array index should fix it, if that's the case.

Ah, OK. Thanks Saffith.

#8 MoscowModder

MoscowModder

    Run for your life.

  • Script DB Staff
  • Gender:Male
  • Location:Not where you'd think

Posted 28 April 2012 - 07:43 PM

Tox: Every function and variable is declared, even if they aren't in that script file. Also, !PD_dashing is the same as PD_dashing == 0.

Gleeok and Saffith: Okay, I'll try splitting that check into two parts as you suggested.


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users