-
Notifications
You must be signed in to change notification settings - Fork 3
/
Plugin_YieldTest.as
45 lines (38 loc) · 1.35 KB
/
Plugin_YieldTest.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#name "Yield example"
#author "Miss"
#category "Examples"
/* This plugin demonstrates how a script loop works. Openplanet will
* execute the Main() function in our plugin every game/server frame.
*
* We can then call `yield()` to suspend script execution and return
* execution back to Openplanet & Maniaplanet, or we can call `sleep()`
* with a milliseconds parameter to yield for a specific amount of time.
*
* In this example, we will increase a variable by 2 on the first frame,
* decrease it by 1 the second frame, then wait 1 second and do it again.
*
* Note that we can only yield if we're inside of a coroutine. Main() is
* in fact a coroutine itself, so we may use it in there. Also see
* Plugin_CoroutineTest.as for more details.
*/
// This is the variable we will be modifying in our script loop.
int g_num = 0;
// Main() is our entry point function for our script loop.
void Main()
{
// This will be our script loop.
while (true) {
// Add 2.
g_num += 2;
print(Time::Now + " Num = " + g_num);
// Suspend script execution.
yield();
// When script execution is resumed in the next frame, we continue
// from here. So let's subtract 1 in this frame.
g_num -= 1;
print(Time::Now + " Num = " + g_num);
// Let's suspend the script for 1 second.
sleep(1000);
}
// The plugin lifetime is over when the end of this function is reached.
}