BindToRenderStep function binds a custom function to be called at a specific time during the render step. There are three main arguments for BindToRenderStep:
what function to call.
As it is linked to the client’s rendering process, BindToRenderStep can only be called on the client.
name parameter is a label for the binding, and can be used with
RunService/UnbindFromRenderStep if the binding is no longer needed.
local RunService = game:GetService("RunService") local function functionToBind() end -- Bind the function above to the binding named "tempBinding" RunService:BindToRenderStep("tempBinding", 1, functionToBind) -- Unbind the function bound to "tempBinding" RunService:UnbindFromRenderStep("tempBinding")
priority of the binding is an integer, and determines when during the render step to call the custom function. The lower this number, the sooner the custom function will be called. If two bindings have the same priority the Roblox engine will randomly pick one to run first. The default Roblox control scripts run with these specific priorities:
- Player Input: 100
- Camera Controls: 200
For convenience, the
Enum/RenderPriorityenum can be used to determine the integer value to set a binding. For example, to make a binding right before the default camera update, simply subtract 1 from the camera priority level.
Note: When using Enum.RenderPriority, remember to use InlineCode.Value at the end of the desired enum. BindToRenderStep will not work if just the enum on its own is used.
local RunService = game:GetService("RunService") local function beforeCamera(delta) -- Code in here will run before the default Roblox camera script end RunService:BindToRenderStep("Before camera", Enum.RenderPriority.Camera.Value - 1, beforeCamera)
Custom Function and Delta Time
The last argument of BindToRenderStep is the
custom function to call. This function will be passed one parameter called deltaTime. DeltaTime shows how much time passed between the beginning of the previous render step and the beginning of the current render step.
Note: All rendering updates will wait until the code in the render step finishes. Make sure that any code called by BindToRenderStep runs quickly and efficiently. If code in BindToRenderStep takes too long, then the game visuals will be choppy.
The name parameter is a label for the binding, and can be used with
The priority of the binding is an integer, and determines when during the render step to call the custom function. The lower this number, the sooner the custom function will be called. If two bindings have the same priority the Roblox engine will randomly pick one to run first. The default Roblox control scripts run with these specific priorities:
The custom function being bound
RunService Custom Function
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.
Bind and Unbind a Function
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.
Please note that we take caution to surround the function unbind in a pcall to prevent the code from breaking due to an error being thrown if the function name passed does not match the name of an already bound function. While we know that the function used in this example is bound when we try to unbind it, doing this is good coding practice.
Frame Moving in Circle
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.