Good-bye camelCase, Hello delay()

August

17, 2007

by Erik Cassel


Archive

Note: This is one of those highly technical articles. If you want something lighter then go here.

The latest build is primarily bug-fix release. However, we’ve added a couple of small scripting enhancements recently.

First, you may have noticed that most object members are now capitalized rather than “camelCase”. We’re supporting old scripts that call member functions that start in lower-case, but all new members will start with an upper-case character. (We’ve decided to standardize on the .NET style with regards to capitalization)

Second, there’s a new global function called “delay”. It complements the wait() function that many of you use. delay takes 2 arguments: The first is a time to wait in seconds, the second is the function to call after the wait time has elapsed. delay continues executing, unlike wait, which blocks.

You’ll find delay is a very useful way to spawn a thread. You might use it in place of connecting to a Heartbeat event and then disconnecting later on. For example, the following code will print “Hello” and “world” followed by “!” a second later:

local c

function exclamation()
   c:disconnect()
   wait(1)
   print("!")
end

print("Hello")
c = game:GetService("RunService").Heartbeat:connect(exclamation)
print("world")

Here is the same functionality written with delay:

function exclamation()
   print("!")
end

print("Hello")
delay(1,exclamation)
print("world")

-Erik