You are viewing an older version of the site. Click here to view
the latest version of this page. (This may be a dead link, if so, try the root page of the docs
here.)
A cookbook has many small, short recipes in it, that tell you how to cook a single dish. With each given recipe, you
are expected to modify it to suit your needs, as well as know which recipes to combine to make an entire meal. Just
like a cookbook, this page lists common tasks that are small, generic examples that need a bit of modification to suit
your needs, but should otherwise complete one small portion of a more complex task. Feel free to take and modify these
snippets to fit your needs, and put them in a larger ecosystem of scripts that make up the whole of your server. The
examples in this cookbook may only work with development features, so if it doesn't work, try updating to a dev build.
===Command Cooldown===
This shows how to add a cooldown to any command, so that it won't be able to be run by a player until after
a certain amount of time.
auto_include.ms:
Copy Code
In your actual command:
Copy Code
=== Continual Server Broadcast ===
If you want to continually broadcast a message to the server, you can add this little snippet:
In main.ms:
Copy Code
=== Delayed Looper ===
If you want to run some code in a loop a limited number of times, but put a delay between runs, you can use the
following code: (Note the [[Execution Queue|Execution Queue]] is a much more elegant solution to
this problem)
Copy Code
proc _cooldown(@time, @id){
@t = get_value('cooldowns.'.@id);
if(@t == null){
store_value('cooldowns.'.@id, time());
return(true);
} else {
if((time() - @t) / 1000 <= @time){
die('You must wait at least '.@time.' seconds between command runs');
} else {
store_value('cooldowns.'.@id, time());
}
}
}

01 {{keyword|proc}} {{function|_cooldown}}(@time, @id){
02 @t = {{function|get_value}}('cooldowns.'.@id);
03
04 {{keyword|if}}(@t == {{keyword|null}}){
05 {{function|store_value}}('cooldowns.'.@id, {{function|time}}());
06 {{function|return}}({{keyword|true}});
07 } {{keyword|else}} {
08 {{keyword|if}}(({{function|time}}() - @t) / 1000 <= @time){
09 {{function|die}}('You must wait at least '.@time.' seconds between command runs');
10 } {{keyword|else}} {
11 {{function|store_value}}('cooldowns.'.@id, {{function|time}}());
12 }
13 }
14 }
/command = >>>
_cooldown(60, 'SOMETHINGUNIQUEHERE') // 60 seconds is the time between command runs
// Do the rest of the command here
<<<

1 /command = >>>
2 {{function|_cooldown}}(60, 'SOMETHINGUNIQUEHERE') // 60 seconds is the time between command runs
3
4
5 // Do the rest of the command here
6
7 <<<
// We are setting it to run every 2 minutes (2 minutes, times 60 seconds, times 1000 ms gives us 2 minutes in ms)
set_interval(2 * 60 * 1000, closure(){
array @messages = array(
'Message 1',
'Message 2',
'Message 3'
// You can add as many messages as you like
);
int @index = rand(array_size(@messages)); // This is the index of a random message
broadcast(@messages[@index]);
});

01 // We are setting it to run every 2 minutes (2 minutes, times 60 seconds, times 1000 ms gives us 2 minutes in ms)
02
03 {{function|set_interval}}(2 * 60 * 1000, {{keyword|closure}}(){
04 {{object|array}} @messages = {{function|array}}(
05 'Message 1',
06 'Message 2',
07 'Message 3'
08 // You can add as many messages as you like
09
10 );
11 {{object|int}} @index = {{function|rand}}({{function|array_size}}(@messages)); // This is the index of a random message
12
13 {{function|broadcast}}(@messages[@index]);
14 });
int @n = 100; // The number of times you wish this to run
int @delay = 1000; // The delay between runs, in MS
double @counterName = rand(); // Prevents overlapping counter names
export('counter' . @counterName, @n);
set_interval(@delay, closure(){
int @n = import('counter' . @counterName);
msg(@n); // This bit becomes your code
dec(@n);
if(@n <= 0){
clear_task();
}
export('counter' . @counterName, @n);
});

01 {{object|int}} @n = 100; // The number of times you wish this to run
02
03 {{object|int}} @delay = 1000; // The delay between runs, in MS
04
05
06 {{object|double}} @counterName = {{function|rand}}(); // Prevents overlapping counter names
07
08 {{function|export}}('counter' . @counterName, @n);
09 {{function|set_interval}}(@delay, {{keyword|closure}}(){
10 {{object|int}} @n = {{function|import}}('counter' . @counterName);
11 {{function|msg}}(@n); // This bit becomes your code
12
13 {{function|dec}}(@n);
14 {{keyword|if}}(@n <= 0){
15 {{function|clear_task}}();
16 }
17 {{function|export}}('counter' . @counterName, @n);
18 });
Find a bug in this page? Edit this page yourself, then submit a pull request.