Jump to content

Photo

Help with timed secrets script


  • Please log in to reply
5 replies to this topic

#1 klop422

klop422

    Guess I'm full of monsters and treasure

  • Members
  • Real Name:Not George
  • Location:Planet Earth

Posted 25 September 2017 - 08:24 AM

I am trying to write a script to make secrets activate after a certain time, repeatedly if so wished. This is my first script.

In any case, the script isn't compiling:

ffc script TimedSecrets {
// The script will activate secrets after 'frames' number of frames (set as D0).
// If D1 is set, it will repeat it as many times as specified.
// It will also play the sound specified in D2
	void run (int frames, int repeat, int sound) {
		// If repeat is above 0, then it will repeat that many times
		if (repeat > 0) {
			while(repeat > 0){
				Waitframes(frames);
				Screen->TriggerSecrets();
				Game->PlaySound(sound)
				this->repeat -= 1
			}
		}
		// If repeat is not above 0, then it will repeat indefinitely
		else if (repeat = 0) {
			While(true) {
				Waitframes(frames);
				Screen->TriggerSecrets();
				Game->PlaySound(sound)
			}
		}
	}
}

The error that ZC states is this:

Line 1181 (that's the first while loop): SYNTAX ERROR, UNEXPECTED IDENTIFIER, EXPECTING SEMICOLON OR OR, ON TOKEN THIS.

 

Anyway, I can't find what's wrong with my syntax, and even though I know it's got to be something minor, I'm asking for help.



#2 Gégé

Gégé

    Senior

  • Members
  • Real Name:Gérard
  • Location:France

Posted 25 September 2017 - 09:48 AM

Try this :
ffc script TimedSecrets {
// The script will activate secrets after 'frames' number of frames (set as D0).
// If D1 is set, it will repeat it as many times as specified.
// It will also play the sound specified in D2
void run (int frames, int repeat, int sound) {
// If repeat is above 0, then it will repeat that many times
    if (repeat > 0) {
        while(repeat > 0){
            Waitframes(frames);
            Screen->TriggerSecrets();
            Game->PlaySound(sound);
            repeat -= 1;
       }
    }
// If repeat is not above 0, then it will repeat indefinitely
     else if (repeat == 0) { // or else {
        while(true) {
            Waitframes(frames);
            Screen->TriggerSecrets();
            Game->PlaySound(sound);
       }
     }
   }
}

Suggestion :
 
ffc script TimedSecrets{
	void run (int frames, int repeat, int sound){
        int delay = frames;

        while(true){
            if(delay > 0)delay --;
            else{
                Screen->TriggerSecrets();
                Game->PlaySound(sound);
                if(repeat > 0)delay = frames;
                else break;
            }
            Waitframe();
	}
    }
}

Edited by Gégé, 25 September 2017 - 10:16 AM.


#3 Jamian

Jamian

    ZC enthusiast

  • Members

Posted 25 September 2017 - 10:04 AM

Don't use "this", just use repeat -= 1; or repeat --; which is the same thing.

 

You should probably also wait 10-15 frames or so at the beginning of the script, to make sure the scrolling is finished before secrets start activating.

 

You're also missing semi-colons after both Game->PlaySound(sound);


Edited by Jamian, 25 September 2017 - 10:06 AM.


#4 klop422

klop422

    Guess I'm full of monsters and treasure

  • Members
  • Real Name:Not George
  • Location:Planet Earth

Posted 25 September 2017 - 10:59 AM

Don't use "this", just use repeat -= 1; or repeat --; which is the same thing.

 

You should probably also wait 10-15 frames or so at the beginning of the script, to make sure the scrolling is finished before secrets start activating.

 

You're also missing semi-colons after both Game->PlaySound(sound);

 

Thanks a lot.

 

Also, thanks, Gégé, but did want to find out what I had done wrong so that I could do better next time. I do have one question about your more efficient script, though: I'm assuming 'break' means that the while loop stops?



#5 Gégé

Gégé

    Senior

  • Members
  • Real Name:Gérard
  • Location:France

Posted 25 September 2017 - 11:08 AM

"Break" stops the ffc script loop.
The script run again if Link returns to the ffc script screen.
Use a Screen Variable if you want a permanent script.

Edited by Gégé, 25 September 2017 - 11:23 AM.


#6 Timelord

Timelord

    The Timelord

  • Banned
  • Location:Prydon Academy

Posted 25 September 2017 - 10:20 PM

You might want to look at how I made TImed Secret Pressure Plate Triggers



Demo Quest, in QSU format

 

Also...


There is no need to create a new variable (int delay) to use as your timer!
 

This is wasting stack space:

ffc script TimedSecrets{
	void run (int frames, int repeat, int sound){
        int delay = frames;

        while(true){
            if(delay > 0)delay --;
            else{
                Screen->TriggerSecrets();
                Game->PlaySound(sound);
                if(repeat > 0)delay = frames;
                else break;
            }
            Waitframe();
	}
    }
}

You can instead, use the existing variable int frames and save some stack space:
 

ffc script TimedSecrets{
	void run (int frames, int repeat, int sound){
        if ( frames < 1 ) { this->Data = 0; this->Script = 0; Quit(); } //Exit on error config,
        while(true){
            while(frames--) //uses the inout value of frames. 
            {
                Waitframe();
            }

            //! If we reach here, we have stopped waiting.

            Screen->TriggerSecrets();

            if ( sound > 0 ) Game->PlaySound(sound);
            //! Note: I sanity check here because playing an invalid sound in 2.50.0 and 2.50.1 
            //! makes a clicking noise. 
            
            if ( repeat > 0 ) {
                frames = this->InitD[1]; //Set it back to the original value of arg D1.
                continue;
            }
            this->Data = 0; this->Script = 0; Quit(); //Cleanly exit. 
            //! You ALWAYS want to wipe and exit ffcs when you are done running their scipts!
                                                      
	}
    }
}

If it was not evident, you can modify the run line variables ( everything inside the parens of void run( ... ) ) as you could any other variable. I believe that it is still system-legal to use non-int/float types as well, although most users have no need of that.


Edited by ZoriaRPG, 25 September 2017 - 10:32 PM.



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users