Jump to content
  • 0

Need help with Timed Question


Bisuke

Question


  • Group:  Members
  • Topic Count:  51
  • Topics Per Day:  0.01
  • Content Count:  177
  • Reputation:   10
  • Joined:  04/02/12
  • Last Seen:  

I just want to ask on how to add timer to this kind of script. Let say that the player will be given a 5 second to answer the question, if the player can't answer it within 5 seconds, he will be kicked out of the server.

 

Here is my sample code.

OnPCLoginEvent:
mes "2+2?";
input .@ans$;
if (.@ans$ != "4" ) {
    mes "wrong!";
    end;
}
else {
    mes "right!";
    end;
}
Link to comment
Share on other sites

6 answers to this question

Recommended Posts


  • Group:  Members
  • Topic Count:  0
  • Topics Per Day:  0
  • Content Count:  44
  • Reputation:   5
  • Joined:  12/06/11
  • Last Seen:  

You have many choice:


*addtimer <ticks>,"NPC::OnLabel";
*deltimer "NPC::OnLabel";
*addtimercount <ticks>,"NPC::OnLabel";

These commands will create, destroy, and delay a countdown timer - 'addtimer' to
create, 'deltimer' to destroy and 'addtimercount' to delay it by the specified
number of ticks. For all three cases, the event label given is the identifier of
that timer. The timer runs on the character object that is attached to the script,
and can have multiple instances. When the label is run, it is run as if the player that
the timer runs on has clicked the NPC.

When this timer runs out, a new execution thread will start in the specified NPC
object at the specified label.

The ticks are given in 1/1000ths of a second.

One more thing. These timers are stored as part of player data. If the player
logs out, all of these get immediately deleted, without executing the script.
If this behavior is undesirable, use some other timer mechanism (like 'sleep').

Example:
<NPC Header> {
	dispbottom "Starting a 5 second timer...";
	addtimer 5000, strnpcinfo(3)+"::On5secs";
	end;
On5secs:
	dispbottom "5 seconds have passed!";
	end;
}

Or:


*initnpctimer{ "<NPC name>" {, <Attach Flag>} } |
             { "<NPC name>" | <Attach Flag> };
*stopnpctimer{ "<NPC name>" {, <Detach Flag>}  } |
             { "<NPC name>" | <Detach Flag> };
*startnpctimer{ "<NPC name>" {, <Attach Flag>} } |
              { "<NPC name>" | <Attach Flag> };
*setnpctimer <tick>{,"<NPC name>"};
*getnpctimer(<type of information>{,"<NPC name>"})
*attachnpctimer {"<character name>"};
*detachnpctimer {"<NPC name>"};

This set of commands and functions will create and manage an NPC-based timer.
The NPC name may be omitted, in which case the calling NPC is used as target.

Contrary to addtimer/deltimer commands which let you have many different timers
referencing different labels in the same NPC, each with their own countdown,
'initnpctimer' can only have one per NPC object. But it can trigger many labels
and let you know how many were triggered already and how many still remain.

This timer is counting up from 0 in ticks of 1/1000ths of a second each. Upon 
creating this timer, the execution will not stop, but will happily continue 
onward. The timer will then invoke new execution threads at labels 
"OnTimer<time>:" in the NPC object it is attached to. 

To create the timer, use the 'initnpctimer', which will start it running. 
'stopnpctimer' will pause the timer, without clearing the current tick, while 
'startnpctimer' will let the paused timer continue.

By default timers do not have a RID attached, which lets them continue even
if the player that started them logs off. To attach a RID to a timer, you can
either use the optional "attach flag" when using 'initnpctimer/startnpctimer', 
or do it manually by using 'attachnpctimer'. Likewise, the optional flag of
stopnpctimer lets you detach any RID after stopping the timer, and by using
'detachnpctimer' you can detach a RID at any time.

Normally there is only a single timer per NPC, but as an exception, as long as
you attach a player to the timer, you can have multiple timers running at once,
because these will get stored on the players instead of the NPC.
NOTE: You need to attach the RID before the timer _before_ you start it to
get a player-attached timer. Otherwise it'll stay a NPC timer (no effect).

If the player that is attached to the npctimer logs out, the "OnTimerQuit:"
event label of that NPC will be triggered, so you can do the appropriate
cleanup (the player is still attached when this event is triggered).

The 'setnpctimer' command will explicitly set the timer to a given tick.
'getnpctimer' provides timer information. Its parameter defines what type:

 0 - Will return the current tick count of the timer.
 1 - Will return 1 if there are remaining "OnTimer<ticks>:" labels in the 
     specified NPC waiting for execution.
 2 - Will return the number of times the timer has triggered and will trigger
     an "OnTimer<tick>:"  label in the specified NPC.

Example 1:

    <NPC Header> {
        // We need to use attachnpctimer because the mes command below needs RID attach
        attachnpctimer;
        initnpctimer;
        npctalk "I cant talk right now, give me 10 seconds";
        end;
    OnTimer5000:
        npctalk "Ok 5 seconds more";
        end;
    OnTimer6000:
        npctalk "4";
        end;
    OnTimer7000:
        npctalk "3";
        end;
    OnTimer8000:
        npctalk "2";
        end;
    OnTimer9000:
        npctalk "1";
        end;
    OnTimer10000:
        stopnpctimer;
        mes "[Man]";
        mes "Ok we can talk now";
        detachnpctimer;
        // and remember attachnpctimer and detachnpctimer can only use while the NPC timer is not running !
    }

Example 2:

    OnTimer15000:
        npctalk "Another 15 seconds have passed.";

        // You have to use 'initnpctimer' instead of 'setnpctimer 0'.
        // This is equal to 'setnpctimer 0' + 'startnpctimer'.
        // Alternatively, you can also insert another 'OnTimer15001' label so that the timer won't stop. */
        initnpctimer;
        end;
       
    // This OnInit label will run when the script is loaded, so that the timer 
    // is initialized immediately as the server starts. It is dropped back to 0 
    // every time the NPC says something, so it will cycle continuously.
    OnInit:
        initnpctimer;
        end;

Example 3:

    mes "[Man]";
    mes "I have been waiting "+(getnpctimer(0)/1000)+" seconds for you.";
    // We divide the timer returned by 1000 to convert milliseconds to seconds.
    close;

Example 4:

    mes "[Man]";
    mes "Ok, I will let you have 30 more seconds...";
    close2;
    setnpctimer (getnpctimer(0)-30000);
    // Notice the 'close2'. If there were a 'next' there the timer would be 
    // changed only after the player pressed the 'next' button.
    end;

Read this and then if you don't understand something just ask :)

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  51
  • Topics Per Day:  0.01
  • Content Count:  177
  • Reputation:   10
  • Joined:  04/02/12
  • Last Seen:  

I forgot to say that, I tried adding both of them, but it doesn't work, I know attaching npc timer is the key, But, I tried and I failed, that is why i'm asking for a help. :)

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  0
  • Topics Per Day:  0
  • Content Count:  44
  • Reputation:   5
  • Joined:  12/06/11
  • Last Seen:  

Hum just a last question, are you trying to block player using bot?

Because that sort of script is useless.

 

If it's for another reason:


 

OnPCLoginEvent:
	mes "2+2?";
	initnpctimer;
	input .@ans$;
	if (.@ans$ != "4" ) {
		mes "wrong!";
		stopnpctimer;
		// You need to do something here, add a 'close' or make a loop. Or player will be freeze since there is no 'close'
	} else {
		mes "right!";
		stopnpctimer;
		close;
	}
	
	OnTimer5000:
		atcommand "@kick "+strcharinfo(0);
		end;
end;
Link to comment
Share on other sites


  • Group:  Developer
  • Topic Count:  10
  • Topics Per Day:  0.00
  • Content Count:  2407
  • Reputation:   613
  • Joined:  07/05/12
  • Last Seen:  

Just to point

addtimer <ticks>,"NPC::OnLabel";

is not the best choice for your request since the player would be kicked only if he 'closes' the npc (an example is the headgear preview)

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  51
  • Topics Per Day:  0.01
  • Content Count:  177
  • Reputation:   10
  • Joined:  04/02/12
  • Last Seen:  

Thanks Guys, I'm going to try this as soons as I get home! :D
 
@Khazou - Nope, this is not for a bot. :D

 

Btw, I just tried it and it didn't work. I'm using rAthena btw. :(

Edited by Bisuke
Link to comment
Share on other sites


  • Group:  Developer
  • Topic Count:  10
  • Topics Per Day:  0.00
  • Content Count:  2407
  • Reputation:   613
  • Joined:  07/05/12
  • Last Seen:  


- script inuinhuin -1,{

OnPCLoginEvent:

set .@number1, rand( 1000 );

set .@number2 rand( 1000 );

set .@total, .@number1 + .@number2;

mes .@number1 +" + "+ .@number2 +" ? you have 5 seconds to answer";

attachnpctimer;

initnpctimer;

input .@ans;

if (.@ans != .@total ) {

mes "wrong!";

stopnpctimer;

OnTimer5000:

atcommand "@kick "+strcharinfo(0);

}

else {

mes "right!";

stopnpctimer;

close;

}

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...