Event: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Legend26
>Legend26
update
Line 1: Line 1:
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.
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==
==Methods==
Events have these methods:
Events have these methods:


*[[#connect|connect]]([[Function]] handler)
{|class="wikitable" style="width: 100%"
*[[#connectFirst|connectFirst]]([[Function]] handler)
! Method !! Description !! Notes
*[[#connectLast|connectLast]]([[Function]] handler)
|-
*[[#wait|wait]]()
| connect([[function]] handler)||Establishes a function to be called whenever the event occurs. ||
*<strike>disconnect([[Function]] handler)</strike>
|-
 
| connectFirst([[function]] handler)||Same as connect except handler functions gets called ''before'' any other handler.||'''Protected'''
=== connect ===
|-
The connect method establishes a function to be called whenever the event occurs.
| connectLast([[function]] handler)||Same as connect except handler functions gets called ''after'' all the other handlers.||'''Protected'''
The handler function is called with the arguments for that event.
|-
The connect method returns another special object called a [[#connection|connection]] object.
| wait()||Pauses the script until the event is fired and returns any arguments the event returns.||
{{example|
|-
game.Workspace.Part.Touched:connect(myTouchedFunction)
| disconnect([[function]] handler)||Disconnects the specified handler function. Use Connection:disconnect() instead.||'''Deprecated'''
 
|}
This causes myTouchedFunction to be called whenever that [[Part]] is touched by another Part.
}}
 
=== connectFirst ===
The connectFirst method acts like the connect method, except that the handler provided gets called ''before'' any other handler connected to that event.
 
=== connectLast ===
The connectLast method acts like the connect method, except that the handler provided gets called ''after'' every other handler connected to that event.
 
=== wait ===
Waits for the next occurance of the Event. The return values are the event arguments.
{{example|
<code>
local otherPart = Workspace.Part.Touched:wait()
</code>
This causes the script to stop and wait for that [[Part]] to be touched by another Part.
}}
 
=== disconnect (deprecated) ===
The old method of using Event:disconnect method is superceded by [[#disconnect|Connection:disconnect]].


== Usage ==
== Usage ==
Take the example of the [[Touched (Event)|Touched]] event of Part:
Take the example of the [[Touched (Event)|Touched]] event of the Part instance:
{{:Touched (Event)}}
{{:Touched (Event)}}
{{clear floats}}
{{clear floats}}
The first section tells you there's an event named "Touched" which has arguments "Instance otherPart". The middle section explains when the event will occur and why.
The Top of the event box tells you that it is an event named "Touched" which has arguments "Instance otherPart". The middle section explains when the event will fire and why. The last section lists the objects that have this event.
The last section lists types of objects that have this event.


We use this information to create a function and a "connection" line in our script.
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}:
Traditionally, such functions are called "handlers" and are named on{Event Name}:
{{example|1=
<code lua>
function onTouched()
--code
end
</code>
}}


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


The arguments that will be passed by the event are given by "Instance otherPart". This means that "otherPart" will be passed and the type will be Instance (which all Parts are).
To allow our function to use that, we defined it as:
<code lua>
function onTouched(otherPart)
--code
end
</code>


To allow our function to use that, we defined it as:
Finally, we can use the connection method of events to bind our function to the event.
<code lua>
part.Touched:connect(onTouched)
</code>


function onTouched(otherPart)
Here's an example using the Touched event of a Part object.
{{Example|
<code lua>
local part = game.Workspace.Part -- your part here
function onTouched(otherPart)
    otherPart.Transparency = 1            -- make part that was touched invisible
    part.BrickColor = BrickColor.Random() -- give this part a new random color
end


Finally, we can used the last piece of information to bind our function to that event:
part.Touched:connect(onTouched) -- call onTouched each time something touches
</code>
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.
}}


part.Touched:connect(onTouched)
== Advanced Usage ==
Any function name is valid:
{{example|
<code lua>
function blah()
  print("In event")
end
part.Changed:connect(blah)
</code>
}}


{{Example|
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.
<pre>
{{example|
local part = game.Workspace.Part -- your part here
<code lua>
function onTouched(otherPart)
part.Touched:connect(function(otherPart)
    otherPart.Transparency = 1           -- make part that was touched invisible
    otherPart.Transparency = 1
    part.BrickColor = BrickColor.Random() -- give this part a new random color
    part.BrickColor = BrickColor.Random()
end
end) -- ends the anonymous function and the ''connect'' method call.
</code>
}}
 
You can also use anonymous functions to change the arguments:
{{example|
<code lua>
player.Chatted:connect(function(message, recipient)
    onChatted(player, message) -- new arguments need to match onChatted function call
end)
</code>
}}
 
Connections can be created at any time, even inside of event functions:
{{example|
<code lua>
function onChatted(player, message)
    print(player .. " said " .. message)
end


part.Touched:connect(onTouched) -- call onTouched each time something touches
function onPlayerAdded(player)
    player.Chatted:connect(function(message, recipient)
        onChatted(player, message)
    end)
end


Here we are giving "otherPart" as an argument to the event handler, onTouched.
game.Players.PlayerAdded:connect(onPlayerAdded)
</pre>
</code>
}}
}}


== Limitations ==
== Limitations ==
Connections are also automatically removed if:
Connections are automatically disconnected if:
* The event handler generates an error before the first wait() call.
* The event handler generates an error before the first wait() call.
* The script itself is removed or reparented.
* The script itself is removed or reparented.
* The object they relate to is destroyed with the [[Destroy_(Method)|Destroy]] method.
* The object the event relates to is destroyed with the [[Destroy_(Method)|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:
'''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:
Line 88: Line 123:
Notice that the error has no script reference or line number. This is because it comes from Roblox itself inside the Event generation code.
Notice that the error has no script reference or line number. This is because it comes from Roblox itself inside the Event generation code.


== Advanced Usage ==
=Connections=
Any function name is valid:
The connection object is a special object returned by the connect methods of an Event.
function blah()
    print("In event")
end
part.Changed:connect(blah)
 
Using anonymous functions in connect is common:


part.Touched:connect(function(otherPart)
== Methods ==
    otherPart.Transparency = 1
Connections have one method:
    part.BrickColor = BrickColor.Random()
{|class="wikitable"
end) -- end anonymous function, and `connect` method call
! Method !! Description
 
|-
You can also use anonymous functions to change the arguments:
| diconnect()||Disconnects the connection from the event.
|}


player.Chatted:connect(function(message, recipient)
    onChatted(player, message) -- new arguments need to match onChatted function call
end)
You can create connections at any time even inside of event functions:
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)
= connection =
The connection object is a special object returned by the connect() method of an Event.
== Methods ==
It has 1 method:
*[[disconnect_(Method)|disconnect]]()


== Properties ==
== Properties ==
It has 1 property:
Connections have one property:
*[[Boolean]] [[connected_(Property)|connected]]
{|class="wikitable"
! Property !! Description
|-
| [[Bool]] connected||True if the connection is connected to an event. Otherwise false.
|}

Revision as of 22:50, 22 January 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
connectLast(function handler) Same as connect except handler functions gets called after all the other handlers. Protected
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 "Instance 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 "Instance otherPart". This means that "otherPart" will be passed and the type of otherPart will be an instance (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
diconnect() 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.