Add a RenderWait function to the environment

This would be the equivalent of

function RenderWait(frames) local start = tick() for i=1,frames or 1 do -- frames defaults to 1 game:GetService("RunService").RenderStepped:wait() end return tick()-start -- Returns the time difference end

I find myself using a form of a renderwait function very, very, very frequently to run client-side effects. It would be awesome if this function was added to the environment by default.

It would pollute the environment with not much gain. I don’t believe it should be added.

Ah but it wouldn’t and should. Here is why:

local stepped = game:GetService("RunService").RenderStepped
local t1 = tick()
while true do
t1 = tick()
stepped:wait()
local t2 = tick()
local dt = t2-t1
--stuff
end

Versus

while true do
local dt = Renderwait()
--stuff
end

Ah but it wouldn’t and should. Here is why:

local stepped = game:GetService("RunService").RenderStepped
local t1 = tick()
while true do
t1 = tick()
stepped:wait()
local t2 = tick()
local dt = t2-t1
--stuff
end

Versus

while true do local dt = Renderwait() --stuff end [/quote]

It pollutes the environment.

As in, it makes a new global variable named ‘RenderWait’

Plus, it doesn’t have much gain. If you’re going to be using it often, you would make an API to do it.

  1. It pollutes the global environment. We are now trying to be careful with global environment. It has some unfortunate historical artefacts that would be good to remove at some point, and we’re definitely not introducing new globals without carefully considering whether they’re universally useful.

  2. Waiting for a constant number of frames causes unpredictable behavior on different clients. The difference in render frame times can be as much as 3x - I doubt you want the function to behave differently. RenderWait(time) makes more sense (but we will not add it to global env because see pt. 1)

ModuleScripts are awesome - you can just put your function in a module script with generally useful utility functions and use it.

Out of curiosity what are the “unfortunate historical artefacts”?

Most of the functions in Documentation - Roblox Creator Hub

The ones which you don’t use often, anyway.

[quote] 1. It pollutes the global environment. We are now trying to be careful with global environment. It has some unfortunate historical artefacts that would be good to remove at some point, and we’re definitely not introducing new globals without carefully considering whether they’re universally useful.

  1. Waiting for a constant number of frames causes unpredictable behavior on different clients. The difference in render frame times can be as much as 3x - I doubt you want the function to behave differently. RenderWait(time) makes more sense (but we will not add it to global env because see pt. 1)

ModuleScripts are awesome - you can just put your function in a module script with generally useful utility functions and use it. [/quote]

Just wondering: Do you guys have statistics on the usage of these methods?

Yeah, just throw it into a ModuleScript:

local rs = game:GetService("RunService").RenderStepped

return function(duration)
local t = tick()
while ((tick() - t) < duration) do rs:wait() end
end

[quote] Yeah, just throw it into a ModuleScript:

[/quote]

a small improvement:

local rs = game:GetService("RunService").RenderStepped

return function(duration)
  if not duration then
    duration = 0
  end
  local t = tick()
  while ((tick() - t) < duration) do
    rs:wait()
  end
  return tick()-t
end

[quote] Yeah, just throw it into a ModuleScript:

[/quote]

a small improvement:

[code]
local rs = game:GetService(“RunService”).RenderStepped

return function(duration)
if not duration then
duration = 0
end
local t = tick()
while ((tick() - t) < duration) do
rs:wait()
end
return tick()-t
end
[/code][/quote]

That’d be good if you’re distributing it. Do it short-hand too:

duration = (type(duration) == "number" and duration or 0)

Just out of curiosity, why is polluting the global environment a bad thing? Does it make the game slow or something?

It’s a bad thing because that’s exactly how PHP does it. And PHP is ugly.

Correct me if I’m wrong but if they use a variable name that people are already using isn’t it bad? I mean yeah, the global will just be overwritten, but it just doesn’t seem like a great thing to do.

Correct me if I’m wrong but if they use a variable name that people are already using isn’t it bad? I mean yeah, the global will just be overwritten, but it just doesn’t seem like a great thing to do.[/quote]

That’s certainly not a great thing to happen. More-so it probably (might be wrong though) has to do with the overhead in the engine to have all those globals. Lua scripts are supposed to be really quick to execute and get going, and anything slowing it down will hurt its ability to do that. There’s probably some standardized “practices” too that come into play with this decision. No need to make a global function or variable that the script itself could easily do itself.