🇨🇳
R2Northstar Wiki
Chinese
Chinese
  • 欢迎来到Northstar
  • 加入我们
  • 安装Northstar
    • 基础教程
    • 故障处理
  • Northstar用户手册
    • Mods(模组)
    • 服务器列表
    • 直连服务器
    • 游戏模式
    • 控制台指令
    • 启动参数
    • 在Linux上游玩
  • 常见问题解答
  • 使用 Northstar托管服务器
    • 前期准备
    • 托管私人比赛服务器
    • 托管独立的服务器端
      • 实践经验
      • 在Linux上托管服务器
    • 故障处理
    • 视频教程
  • 修改与开发教程
    • 开始修改
      • 基础修改
      • 教程
      • 参考列表
      • Squirrel(松鼠)脚本
        • 线程处理
        • 基础代码块
        • Respawn(重生)创建的函数
        • 表和数组
        • 回调函数
        • 游戏模式修改
          • 开始修改
        • 自定义设置
          • 关于mod.json文件
          • 关于本地化语言文件
          • 关于Mod(模组)文件
      • 设置语法高光
      • 本地化翻译
      • 收尾工作
    • 开发者
  • 其他
    • 特别感谢
由 GitBook 提供支持
在本页
  • What is thread?
  • How do i use thread?
  1. 修改与开发教程
  2. 开始修改
  3. Squirrel(松鼠)脚本

线程处理

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.

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.

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.

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

 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.

上一页Squirrel(松鼠)脚本下一页基础代码块

最后更新于3年前