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.
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
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.
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.
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] 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.
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?
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]
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)
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.