Event: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
Undo revision 52371 by MrNicNac (talk) The output says they are... try to use them, it will print a yellow message in the output saying they're deprecated.
>Legend26
Stop using the var tags, and let's just do it this way as both are true. And leave game.Workspace, it can easily get confusing for new players if you leave that out.
Line 9: Line 9:
| connect([[function]] handler)||Establishes a function to be called whenever the event occurs. ||
| connect([[function]] handler)||Establishes a function to be called whenever the event occurs. ||
|-
|-
| connectFirst([[function]] handler)||Same as connect except handler functions gets called ''before'' any other handler.||'''Deprecated'''
| connectFirst([[function]] handler)||Same as connect except handler functions gets called ''before'' any other handler.||'''Protected''' and '''Deprecated'''
|-
|-
| connectLast([[function]] handler)||Same as connect except handler functions gets called ''after'' all the other handlers.||'''Deprecated'''
| connectLast([[function]] handler)||Same as connect except handler functions gets called ''after'' all the other handlers.||'''Protected''' and '''Deprecated'''
|-
|-
| wait()||Pauses the script until the event is fired and returns any arguments the event returns.||
| wait()||Pauses the script until the event is fired and returns any arguments the event returns.||
Line 19: Line 19:


== Usage ==
== Usage ==
Take the example of the [[Touched (Event)|Touched]] event of the [[RBX.lua.BasePart (Object)|BasePart]] instance:
Take the example of the [[Touched (Event)|Touched]] event of the Part instance:
{{:Touched (Event)}}
{{:Touched (Event)}}
{{clear floats}}
{{clear floats}}
Line 35: Line 35:
}}
}}


The arguments that will be passed by the event are given by the name of the event. In this case it is "BasePart otherPart". This means that <var>otherPart</var> will be passed and the type of <var>otherPart</var> will be a [[RBX.lua.BasePart (Object)|BasePart]], which is a class that includes [[RBX.lua.Part (Object)|Parts]].
The arguments that will be passed by the event are given by the name of the event. In this case it is "BasePart otherPart". This means that "otherPart" will be passed and the type of otherPart will be a BasePart (which all Parts are).


To allow our function to use that, we defined it as:
To allow our function to use that, we defined it as:
Line 52: Line 52:
{{Example|
{{Example|
<code lua>
<code lua>
local part = Workspace.Part -- your part here
local part = game.Workspace.Part -- your part here
function onTouched(otherPart)
function onTouched(otherPart)
     otherPart.Transparency = 1            -- make part that was touched invisible
     otherPart.Transparency = 1            -- make part that was touched invisible
Line 60: Line 60:
  part.Touched:connect(onTouched) -- call onTouched each time something touches
  part.Touched:connect(onTouched) -- call onTouched each time something touches
</code>
</code>
Here we are giving <var>otherPart</var> as an argument to the event handler, onTouched. When <var>part</var> is touched, the part that touches <var>part</var> will turn invisible and <var>part</var> will become a different color.
Here we are giving "otherPart" as an argument to the event handler, onTouched. When the ''part'' is touched, the part that touches ''part'' will turn invisible and ''part'' will become a different color.
}}
}}


Line 74: Line 74:
}}
}}


Using anonymous functions in connect is common and results in slightly shorter code. This can be useful if you only need to connect a function to an event only once.
Using anonymous functions in connect is common and results in slightly shorter code. This can be useful if you only need to connect a function to an event once.
{{example|
{{example|
<code lua>
<code lua>

Revision as of 20:01, 9 February 2012

An Event is a special kind of Roblox Object. It provides a way for user-defined functions to be called when something happens in the game.

Methods

Events have these methods:

Method Description Notes
connect(function handler) Establishes a function to be called whenever the event occurs.
connectFirst(function handler) Same as connect except handler functions gets called before any other handler. Protected and Deprecated
connectLast(function handler) Same as connect except handler functions gets called after all the other handlers. Protected and Deprecated
wait() Pauses the script until the event is fired and returns any arguments the event returns.
disconnect(function handler) Disconnects the specified handler function. Use Connection:disconnect() instead. Deprecated

Usage

Take the example of the Touched event of the Part instance:

Touched ( BasePart otherPart )
Description Fired when another object comes in contact with this object.
Member of: BasePart

The top of the event box tells you that it is an event named "Touched" which has arguments "BasePart otherPart". The middle section explains when the event will fire and why. The last section lists the objects that have this event.

We use this information on the event to create a connection to an event line in our script.

Traditionally, such functions are called "handlers" and are named on{Event Name}:

Example

function onTouched() --code end


The arguments that will be passed by the event are given by the name of the event. In this case it is "BasePart otherPart". This means that "otherPart" will be passed and the type of otherPart will be a BasePart (which all Parts are).

To allow our function to use that, we defined it as: function onTouched(otherPart) --code end

Finally, we can use the connection method of events to bind our function to the event. part.Touched:connect(onTouched)

Here's an example using the Touched event of a Part object.

Example
{{{1}}}


Advanced Usage

Any function name is valid:

Example

function blah()

  print("In event")

end part.Changed:connect(blah)


Using anonymous functions in connect is common and results in slightly shorter code. This can be useful if you only need to connect a function to an event once.

Example
{{{1}}}


You can also use anonymous functions to change the arguments:

Example

player.Chatted:connect(function(message, recipient)

   onChatted(player, message) -- new arguments need to match onChatted function call

end)


Connections can be created at any time, even inside of event functions:

Example

function onChatted(player, message)

   print(player .. " said " .. message)

end

function onPlayerAdded(player)

   player.Chatted:connect(function(message, recipient) 
       onChatted(player, message) 
   end)

end

game.Players.PlayerAdded:connect(onPlayerAdded)


Limitations

Connections are automatically disconnected if:

  • The event handler generates an error before the first wait() call.
  • The script itself is removed or reparented.
  • The object the event relates to is destroyed with the Destroy method.

Note: The connect method does not check that the argument is a valid function. If you misspell the name or otherwise give an invalid function you'll get an error when the event next triggers:

Fri Apr 23 21:50:48 2010 - attempt to call a nil value
Fri Apr 23 21:50:48 2010 - 

Notice that the error has no script reference or line number. This is because it comes from Roblox itself inside the Event generation code.

Connections

The connection object is a special object returned by the connect methods of an Event.

Methods

Connections have one method:

Method Description
disconnect() Disconnects the connection from the event.

Properties

Connections have one property:

Property Description
Bool connected True if the connection is connected to an event. Otherwise false.