diff options
author | JMM889901 <41163714+JMM889901@users.noreply.github.com> | 2022-01-27 13:21:56 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-27 14:21:56 +0100 |
commit | 8ebc0a67c03886fe09c771af432683500ecabf83 (patch) | |
tree | 985b2cd15796b6a4d5142dc7653b4206fe907cb1 /docs/modding/squirrel/handling-threads-and-waits.md | |
parent | 7f08b061845ca4f926b939acad6bd2dea8a7dce3 (diff) | |
download | NorthstarWiki-8ebc0a67c03886fe09c771af432683500ecabf83.tar.gz NorthstarWiki-8ebc0a67c03886fe09c771af432683500ecabf83.zip |
Began adding entries for squirrel GNUT scripting (#27)
* bruh
* Update what-is-squirrel.md
* Create Handling-threads-and-waits.md
* Update Handling-threads-and-waits.md
* Update What-are-callbacks.md
* i got locked out while making this
* Update what-is-squirrel.md
* confusion is bad
* funne colors
* bruh
* Language moment
* Update the-mod-file.md
* Json moment
* Update The-JSON.md
* Update The-JSON.md
* Update The-JSON.md
* Update The-language-file.md
* Update The-language-file.md
* Update the-mod-file.md
* Update The-language-file.md
* Create The-mod-init.md
* got distracted, will finish later
* pain
* Update The-mod-init.md
* fixed typo
* Rename What-are-callbacks.md to what-are-callbacks.md
* Rename Loops-Functions-and-if.md to loops-functions-and-if.md
* Rename docs/modding/squirrel/Setting mods/the-mod-file.md to docs/modding/squirrel/setting-mods/the-mod-file.md
* Rename docs/modding/squirrel/Setting mods/The-language-file.md to docs/modding/squirrel/setting mods/the-language-file.md
* Rename docs/modding/squirrel/Gamemode mods/The-mod-init.md to docs/modding/squirrel/gamemode mods/the-mod-init.md
* Rename Handling-threads-and-waits.md to handling-threads-and-waits.md
* Rename docs/modding/squirrel/Setting mods/The-JSON.md to docs/modding/squirrel/setting mods/the-json.md
* Rename Respawns-Functions.md to respawns-functions.md
* Rename docs/modding/squirrel/gamemode mods/the-mod-init.md to docs/modding/squirrel/gamemode-mods/the-mod-init.md
* Rename docs/modding/squirrel/setting mods/the-json.md to docs/modding/squirrel/setting-mods/the-json.md
* Rename docs/modding/squirrel/setting mods/the-language-file.md to docs/modding/squirrel/setting-mods/the-language-file.md
* tables do kinda be cool tho
* var is better now
* bruh
Diffstat (limited to 'docs/modding/squirrel/handling-threads-and-waits.md')
-rw-r--r-- | docs/modding/squirrel/handling-threads-and-waits.md | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/docs/modding/squirrel/handling-threads-and-waits.md b/docs/modding/squirrel/handling-threads-and-waits.md new file mode 100644 index 0000000..2086dbd --- /dev/null +++ b/docs/modding/squirrel/handling-threads-and-waits.md @@ -0,0 +1,65 @@ +Timers and infinite statements +========================== +When using timers or infinite statements such as `while(true)` or `wait` it is important to use `thread` before the function in order to prevent crashes or freezes. + +What is `thread`? +----------- +The `thread` term is used to tell squirrel to run the following function **separately** from the rest of the game, while in a simple scripts code is run sequentially(line 1,2,3 etc) +if a line of code would last forever or need to function in parallel to normal gameplay, such as a `wait` command, it is important to use `thread` or the game will get stuck processing that line indefinitely +and will not move to the next lines, causing crashes or freezes. + +How do i use `thread`? +------------- +Using thread is fairly simple, if we have a function called `delayannouncement` that chooses one player as "it" 10 seconds after spawning we cannot use this function on its own, instead calling it with a thread by simply calling + +`thread delayannouncement()` + +The same applies to a `while(true)` function, for example `almostover` a function that checks every 5 seconds to see if the game has 2 or less minutes left and announces it if so. + + `thread almostover()` + + Example Script + ----------- + lets try implement both of our scripts from the previous 2 sections, as well as a callback to trigger the script. + + First, lets add our callback to the gamemodes core function. + + ```cpp + global function GamemodeTag_Init + + + void function GamemodeTag_Init(){ + AddCallback_GameStateEnter( eGameState.Playing, MatchStart ) + } + ``` +Then lets define the function matchstart and have it simply thread our two important functions. + ```cpp + void Matchstart{ + thread delayannouncement() + thread almostover() + } + ``` +This script waits 10 seconds, picks a player and announces that player as "it" however being `it` currently does nothing, we will define that later. + ```cpp + void delayannouncement(){ + wait 10.0 + string chosenplayer = GetPlayerArray()[RandomInt(GetPlayerArray().len())] + string message = chosenplayer + " is it!" + foreach ( entity player in GetPlayerArray() ) + SendHudMessage( player, message, -1, 0.4, 255, 0, 0, 0, 0, 3, 0.15 ) +} +``` +This function will now repeat endlessly, waiting 5 seconds before each repeat. make sure to add a `return` or `break` statement to prevent the message looping every 5 seconds after, unless you want that +```cpp + void almostover(){ + while(true){ + if(GameTime_TimeLeftSeconds() < 120){ + foreach ( entity player in GetPlayerArray() ) + SendHudMessage( player, "Two minutes left!", -1, 0.4, 255, 0, 0, 0, 0, 3, 0.15 ) + break + } + wait 5.0 + } + } + ``` +You have now created and threaded both functions. |