RunService

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
服务
未复制

RunService 包含用于时间管理的方法和事件以及用于管理体验或脚本运行的上下文。方法如 IsClient() , IsServer()IsStudio() 可以帮助您确定在哪个上下文代码运行。这些方法对于 ModuleScripts 有用,可能需要由客户端和服务器脚本共同使用。此外,IsStudio() 可用于为在工作室测试中添加特殊行为。

RunService 还包含允许您的代码遵守 Roblox 框架循环的事件,例如 PreRender , PreAnimation , PreSimulation , PostSimulation , 和 Heartbeat .选择正确的事件以在任何情况下使用是很重要的,因此您应该阅读 任务调度器 以做出明智的决定。

上下文测试结果

<th><code>Class.RunService:IsStudio()|IsStudio</code></th>
<th><code>Class.RunService:IsClient()|IsClient</code></th>
<th><code>Class.RunService:IsServer()|IsServer</code></th>
<th><code>Class.RunService:是编辑()|是编辑</code></th>
<th><code>Class.RunService:是运行()|是运行</code></th>
<th><code>Class.RunService:IsRunMode()|IsRunMode</code></th>
</tr>
</thead>
<tbody>
<tr>
<td>实时玩家</td> <td><code>false</code></td> <td><code>true</code></td> <td><code>false</code></td>
<td />
<td />
<td />
</tr>
<tr>
<td>实时服务器</td> <td><code>false</code></td> <td><code>false</code></td> <td><code>true</code></td>
<td />
<td />
<td />
</tr>
<tr>
<td>编辑模式</td>
<td><code>真实</code></td>
<td><code>真实</code></td>
<td><code>真实</code></td>
<td><code>真实</code></td>
<td><code>错误</code></td>
<td><code>错误</code></td>
</tr>
<tr>
<td>协作编辑</td>
<td><code>真实</code></td>
<td><code>真实</code></td>
<td><code>错误</code></td>
<td><code>真实</code></td>
<td><code>错误</code></td>
<td><code>错误</code></td>
</tr>
<tr>
<td>运行模式</td>
<td><code>真实</code></td>
<td><code>真实</code></td>
<td><code>真实</code></td>
<td><code>错误</code></td>
<td><code>真实</code></td>
<td><code>真实</code></td>
</tr>
<tr>
<td>播放模式(客户端)</td>
<td><code>真实</code></td>
<td><code>真实</code></td>
<td><code>错误</code></td>
<td><code>错误</code></td>
<td><code>真实</code></td>
<td><code>错误</code></td>
</tr>
<tr>
<td>播放模式(服务器)</td>
<td><code>真实</code></td>
<td><code>错误</code></td>
<td><code>真实</code></td>
<td><code>错误</code></td>
<td><code>真实</code></td>
<td><code>真实</code></td>
</tr>
<tr>
<td>团队测试(玩家)</td>
<td><code>真实</code></td>
<td><code>真实</code></td>
<td><code>错误</code></td>
<td><code>错误</code></td>
<td><code>真实</code></td>
<td><code>错误</code></td>
</tr>
<tr>
<td>团队测试(服务器)</td>
<td><code>错误</code></td>
<td><code>错误</code></td>
<td><code>真实</code></td>
<td><code>错误</code></td>
<td><code>真实</code></td>
<td><code>错误</code></td>
</tr>
</tbody>
环境

概要

属性

方法

活动

属性

ClientGitHash

只读
未复制
Roblox 脚本安全性
读取并联
未复制
插件安全性
读取并联

方法

BindToRenderStep

()

BindToRenderStep() 函数将绑定一个自定义函数在渲染步骤期间特定时间被调用。有三个主要参数:namepriority、以及需要调用的function

由于与客户端的渲染过程相关,BindToRenderStep() 只能在客户端上调用。

名称

name 参数是用于绑定的标签,可以与 RunService:UnbindFromRenderStep() 一起使用,如果绑定不再需要。


local RunService = game:GetService("RunService")
local function functionToBind() end
-- 将上面的函数绑定到名为“tempBinding”的绑定中
RunService:BindToRenderStep("tempBinding", 1, functionToBind)
-- 解除对“tempBinding”绑定的函数
RunService:UnbindFromRenderStep("tempBinding")
优先级

绑定的 priority 是一个整数;它决定在渲染步骤期间调用自定义函数的时间。数字越低,调用自定义函数的时间就越早。如果两个绑定具有相同的优先级,Roblox 引擎将随机选择一个先运行。默认 Roblox 控制脚本以这些特定优先级运行:

  • 玩家输入:100
  • 相机控件:200 为了方便;枚举 Enum.RenderPriority 可用于确定整数值以设置绑定。例如,要在默认相机更新之前制定绑定权限,只需从相机优先等级中减去 1 即可。

使用 Enum.RenderPriority 时,请记得在所需枚举的最后使用 .Value 。如果仅使用枚举本拥有,RunService:BindToRenderStep() 将无法工作。


local RunService = game:GetService("RunService")
local function beforeCamera(delta)
-- 在这里的代码将在默认 Roblox 相机脚本之前运行
end
RunService:BindToRenderStep("Before camera", Enum.RenderPriority.Camera.Value - 1, beforeCamera)
自定义函数和 Delta 时间

最后的参数(function)是要调用的自定义函数。这个函数将传递一个名为 deltaTime 的参数,该参数显示了从前一个渲染步开始到当前渲染步开始之间的时间。

所有渲染更新都会等待直到渲染步骤中的代码完成。确保任何由 BindToRenderStep() 调用的代码快速高效运行;如果代码耗时过长,体验视觉效果将不佳。

参数

name: string

标签用于绑定,可以与 Unbind 一起使用,如果绑定不再需要。

默认值:""
priority: number

将绑定作为整数的优先级;它决定在渲染步骤期间调用自定义函数的时间。数字越低,调用自定义函数的时间就越早。如果两个绑定具有相同的优先级,Roblox 引擎将随机选择一个先运行。

默认值:""
function: function

正在绑定的自定义函数。

默认值:""

返回

()

代码示例

This code sample moves a GuiObject in a circle within its parent object using RunService's BindToRenderStep. It defines a parametric equation in a function to help with positioning the GuiObject.

To try this code out, put a ScreenGui in the StarterGui. Inside the ScreenGui, insert a Frame with a LocalScript. Paste this code into the LocalScript, then play the game. Watch the Frame travel counterclockwise within.

Frame Moving in Circle

local RunService = game:GetService("RunService")
-- How fast the frame ought to move
local SPEED = 2
local frame = script.Parent
frame.AnchorPoint = Vector2.new(0.5, 0.5)
-- A simple parametric equation of a circle
-- centered at (0.5, 0.5) with radius (0.5)
local function circle(t)
return 0.5 + math.cos(t) * 0.5, 0.5 + math.sin(t) * 0.5
end
-- Keep track of the current time
local currentTime = 0
local function onRenderStep(deltaTime)
-- Update the current time
currentTime = currentTime + deltaTime * SPEED
-- ...and the frame's position
local x, y = circle(currentTime)
frame.Position = UDim2.new(x, 0, y, 0)
end
-- This is just a visual effect, so use the "Last" priority
RunService:BindToRenderStep("FrameCircle", Enum.RenderPriority.Last.Value, onRenderStep)
--RunService.RenderStepped:Connect(onRenderStep) -- Also works, but not recommended

This example shows how to bind a simple function to the render step. All this function does is print how much time passed between the last render step and the current one. Note that this code will need to be in a LocalScript to run.

RunService Custom Function

local RunService = game:GetService("RunService")
local function checkDelta(deltaTime)
print("Time since last render step:", deltaTime)
end
RunService:BindToRenderStep("Check delta", Enum.RenderPriority.First.Value, checkDelta)

This example uses the RunService to bind and unbind a function named printHello. First, we bind the function to the RenderStep so that fires every step. Then, after we wait 5 seconds (wait(5)), we unbind the function.

Bind and Unbind a Function

local RunService = game:GetService("RunService")
-- Step 1: Declare the function and a name
local NAME = "Print Hello"
local function printHello()
print("Hello")
end
-- Step 2: Bind the function
RunService:BindToRenderStep(NAME, Enum.RenderPriority.First.Value, printHello)
-- Step 3: Unbind the function
RunService:UnbindFromRenderStep(NAME)

IsClient

写入并联

如果这个方法被触发的代码在客户端上运行(在 中,在 由 要求,或在 中设置为 ,那么这个方法将返回 .在所有其他情况下,该方法将返回 false .

如果该方法返回 true,当前环境可以访问客户端仅的功能,例如 RunService.PreRenderPlayers.LocalPlayer


返回

当前环境是否运行客户端。

IsEdit

插件安全性
写入并联

该方法返回当前环境是否处于“编辑”模式,例如在 Studio 中,当体验未运行时。

IsEdit() 将返回 IsRunning() 的反转,除非模拟已暂停,否则两种方法都将返回 false


返回

当前环境是否处于“编辑”模式。

IsRunMode

写入并联

该方法返回是否按下了 运行 按钮以在 Studio 中运行模拟。如果使用 true 按钮暂停模拟,它将继续返回 **** ;然而,一旦使用 停止 按钮停止模拟,它将恢复返回 false

请注意,Studio只在按下 运行 按钮时进入“奔跑行”模式,而不是按钮 播放 。还注意到,这种方法将返回 false 如果模拟使用 RunService:Run() 而不是 运行 按钮启动,那么将返回 。


返回

是否按下 运行 按钮以在 Studio 中运行模拟。

IsRunning

返回体验是否正在运行,这意味着模拟已使用 运行播放 按钮进行运行。

IsRunning() 将始终返回IsEdit()的反向,除非模拟已暂停,否则两种方法都将返回false


返回

体验目前是否运行。

IsServer

写入并联

该方法返回当前环境是否在服务器上运行。如果召用此方法的代码正在运行在服务器上下文(在 与 设置为 或 ,或在 由 要求),此方法将返回 。在所有其他情况下,该方法将返回 false .

如果此函数返回 true,那么当前环境可以访问服务器专用功能,例如 ServerStorageServerScriptService


返回

当前环境是否在服务器上运行。

IsStudio

写入并联

该方法返回当前环境是否在 Studio 中运行。它可以用来包装在 Studio 中仅在测试时执行的代码。


返回

当前环境是否在 Studio 中运行。

Pause

()
插件安全性

该方法在运行时暂停体验的模拟,暂停物理和脚本。模拟可以使用 Run() 或 Studio 中的 运行 按钮开始;当模拟暂停时,IsRunning() 将返回 false


返回

()

Run

()
插件安全性

该方法运行体验的模拟(物理和脚本)。当模拟运行时,IsRunning() 将返回true。但是,IsRunMode() 只会返回true,如果模拟使用 Studio 中的 运行 按钮开始。


返回

()

Stop

()
插件安全性

该方法如果运行,将停止体验的模拟;当模拟停止时,IsRunning() 将返回 falseIsEdit() 将返回 true

与 Studio 中的 停止 按钮相比,调用此方法不会将体验恢复到模拟运奔跑之前的状态。这意味着物理模拟和脚本对体验所做的任何更改将在模拟结束后持续存在。


返回

()

UnbindFromRenderStep

()

给予一个函数名称发送到 BindToRenderStep() , 这个方法将使函数在 PreRender 期间不被调用。这是用于在不再需要它们或不再需要发射每一步时解除绑定函数。

如果指定名称没有绑定函数,该方法不采取任何行动,继续无需提出错误。

参数

name: string

正在解除绑定的函数名称。

默认值:""

返回

()

活动

Heartbeat

Heartbeat 事件在物理模拟完成后触发每个框架。deltaTime 参数表示自上一帧发出以来已过的时间。

这个事件是大多数脚本奔跑的时候。它发生在每个框架结束时,也是当任何等待脚本执行时,例如那些由 task 图书馆预订的脚本。Heartbeat 通常用于定期任务,例如更新核心游戏系统,如生命恢复。

在此步骤之后,引擎将属性更新和事件发送到服务器或客户端,这些更新和事件稍后作为复制接收步骤的一部分收到。

参数

deltaTime: number

从上一帧开始已经过去的时间(秒)。


PostSimulation

PostSimulation 事件在物理模拟完成后触发每个框架。deltaTimeSim 参数表示当前框架已步入物理模拟时间,不考虑物理限制。

此事件对于进行模拟结果的最后调整有用。在此阶段之后,引擎触发了 Heartbeat 事件。

参数

deltaTimeSim: number

当前框架步入物理模拟的时间(秒),不考虑物理限制。


PreAnimation

PreAnimation 事件在物理模拟之前每一帧发生,但渲染后。deltaTimeSim 参数表示当前框架已步入动画的时间。

这个事件对于修改动画对象,例如调整速度或优先级有用。一旦 PreAnimation 事件完成,引擎将继续运行这些动画,更新将在物理模拟期间用于更新对象的关节变形。

动画步骤完成后,引擎触发PreSimulation事件。

参数

deltaTimeSim: number

当前框架有步骤动画的时间(秒)。


PreRender

PreRender(替换为RenderStepped)在画面渲染之前触发每个帧。deltaTimeRender 参数表示自上一帧发出以来已过的时间。

这个事件允许您在代码运行并更新世界之前在玩家屏幕上绘制它。这对于最后一刻的调整,例如更改对象位置、更新动画或准备视觉效果,有用,但应该稀有使用,因为引擎在此事件中运行代码完成后才能开始渲染框架。

由于 PreRender 是客户端,它只能在 LocalScript 、在 ModuleScriptLocalScript 要求,或在 Script 中使用 RunContext 设置为 Enum.RunContext.Client

PreRender 阶段之后,模拟阶段以 PreAnimation 事件开始。

参数

deltaTimeRender: number

从上一帧开始已经过去的时间(秒)。


PreSimulation

PreSimulation事件(替换为Stepped)在物理模拟之前触发每个框架。deltaTimeSim 参数表示当前框架将进行物理模拟的时间,不考虑物理限制。

这个事件对调整速度或力量等属性有用,在它们被应用于模拟之前。模拟然后运行,可能多次,因为物理解决器的频率比其他引擎系统高。一旦完成,PostSimulation事件将被触发。

参数

deltaTimeSim: number

当前框架将进行物理模拟的时间(秒),不考虑物理限制。


RenderStepped

在渲染框之前,发射每个框。

迁移注释

此事件已被 PreRender 取代,该应用于新工作。

参数

deltaTime: number

从上一帧开始已经过去的时间(秒)。


Stepped

在物理模拟之前,发射每个框架。

迁移注释

此事件已被 PreSimulation 取代,该应用于新工作。

参数

time: number

RunService 已运行的时间(秒)。

deltaTime: number

从上一帧开始已经过去的时间(秒)。