DarkstaR
07-14-2013, 09:13 PM
This update brings some major updates to the Lua system. You can now share data between Lua scripts using the Signal class, and handle text messages and creature speech messages using the new Lua proxies. A big thanks to Syntax for helping with the Signal examples and class structures!
Changelog:
v3.3.0
Improved the Injector to now preserve the window titles of logged in clients and display them by character name in the list.
Added the Signal class to the Scripter, allowing scripts to talk to each other.
Signal.New(key, callback)
Creates a new signal handler for a signal with the specified key.
Parameters:
key: the key which identifies the signal.
callback: a callback to execute when a signal with the specified key is received
Signal:Send(value) or Signal.Send(key, value)
Sends a signal to all scripts for the specified key
Parameters:
value: the value of the signal. Supports integers, boolean, strings, and tables (max size 2047).
Signal:Close() or Signal.Close(key)
Stops the signal from receiving events.
Signal:Key()
Returns the key of the signal object.
Added the Proxy interface to the Scripter, allowing scripts to handle text and status messages.
The Proxy interface is implemented by different network events and is used to handle their execution.
Current Implementations:
LocalSpeechProxy : (mtype, speaker, level, text)
ErrorMessageProxy : (message)
BattleMessageProxy : (message)
LootMessageProxy : (message)
LookMessageProxy : (message)
ProxyImplementation.New(key, callback)
Creates a new proxy.
Parameters:
key: a key that serves no purpose but to identify the proxy
callback: a callback to execute when the event happens
ProxyImplementation:Close() or ProxyImplementation.Close(key)
Stops the specified proxy from receiving events.
ProxyImplementation:Key()
Returns the key of the proxy object.
Signal Class Example
The following scripts will use Lua signals to talk to eachother. Together, they display the two different usage models for Signals.
bluescript.lua
------------------------------------------------
-- This is "Blue Script", it uses static classes
------------------------------------------------
wait(10000)
-- Check for any message for blue script.
Signal.OnReceive('blue-script', function(signal, data)
-- print red script's message
print('Red: ' .. data)
-- Send a message to red script, depending on the message received
if(data == 'Hello')then
Signal.Send('red-script', 'Hi red script!!!')
elseif(data == 'Bye')then
Signal.Send('red-script', 'See ya later.')
-- We said our farewells, lets kill this listener
signal:Close()
end
end)
redscript.lua
--------------------------------------------------------
-- This is "Red Script", it uses object-oriented classes
--------------------------------------------------------
wait(10000)
local blue = Signal.New('blue-script')
local red = Signal.New('red-script')
-- Start the conversation, send a message to blue script
blue:Send('Hello')
-- Check for any message for red script.
red:OnReceive(function(signal, data)
-- print blue script's message
print('Blue: ' .. data)
-- Send a message to blue script, then kill the signal.
blue:Send('Bye.')
signal:Close()
end)
LocalSpeechProxy Example
This example handles local (say, yell, whisper, and spell cast) messages.
LocalSpeechProxy.OnReceive("myproxy", function(proxy, mtype, speaker, level, text)
print(string.format("{%s} %s [%d]: %s", mtype, speaker, level, text))
end)
11:39 {yell} DarkstaR [666]: I'M AWESOME
*MessageProxy Example
This example handles error, battle, loot, and look messages.
function messageProxyCallback(proxy, message)
print(proxy:Key() .. ": " .. message)
end
ErrorMessageProxy.OnReceive("ErrorMessageProxy", messageProxyCallback)
BattleMessageProxy.OnReceive("BattleMessageProxy", messageProxyCallback)
LootMessageProxy.OnReceive("LootMessageProxy", messageProxyCallback)
LookMessageProxy.OnReceive("LookMessageProxy", messageProxyCallback)
11:39 ErrorMessageProxy: You cannot use this object.
11:39 A spider loses 20 hitpoints due to your attack.
11:39 BattleMessageProxy: A spider loses 20 hitpoints due to your attack.
11:39 Loot of a spider: 2 gold coins
11:39 LootMessageProxy: Loot of a spider: 2 gold coins
11:40 You see dirt floor.
11:40 LookMessageProxy: You see dirt floor.
For download and operation instructions, refer back to this thread:
http://forums.xenobot.net/showthread.php?19
Changelog:
v3.3.0
Improved the Injector to now preserve the window titles of logged in clients and display them by character name in the list.
Added the Signal class to the Scripter, allowing scripts to talk to each other.
Signal.New(key, callback)
Creates a new signal handler for a signal with the specified key.
Parameters:
key: the key which identifies the signal.
callback: a callback to execute when a signal with the specified key is received
Signal:Send(value) or Signal.Send(key, value)
Sends a signal to all scripts for the specified key
Parameters:
value: the value of the signal. Supports integers, boolean, strings, and tables (max size 2047).
Signal:Close() or Signal.Close(key)
Stops the signal from receiving events.
Signal:Key()
Returns the key of the signal object.
Added the Proxy interface to the Scripter, allowing scripts to handle text and status messages.
The Proxy interface is implemented by different network events and is used to handle their execution.
Current Implementations:
LocalSpeechProxy : (mtype, speaker, level, text)
ErrorMessageProxy : (message)
BattleMessageProxy : (message)
LootMessageProxy : (message)
LookMessageProxy : (message)
ProxyImplementation.New(key, callback)
Creates a new proxy.
Parameters:
key: a key that serves no purpose but to identify the proxy
callback: a callback to execute when the event happens
ProxyImplementation:Close() or ProxyImplementation.Close(key)
Stops the specified proxy from receiving events.
ProxyImplementation:Key()
Returns the key of the proxy object.
Signal Class Example
The following scripts will use Lua signals to talk to eachother. Together, they display the two different usage models for Signals.
bluescript.lua
------------------------------------------------
-- This is "Blue Script", it uses static classes
------------------------------------------------
wait(10000)
-- Check for any message for blue script.
Signal.OnReceive('blue-script', function(signal, data)
-- print red script's message
print('Red: ' .. data)
-- Send a message to red script, depending on the message received
if(data == 'Hello')then
Signal.Send('red-script', 'Hi red script!!!')
elseif(data == 'Bye')then
Signal.Send('red-script', 'See ya later.')
-- We said our farewells, lets kill this listener
signal:Close()
end
end)
redscript.lua
--------------------------------------------------------
-- This is "Red Script", it uses object-oriented classes
--------------------------------------------------------
wait(10000)
local blue = Signal.New('blue-script')
local red = Signal.New('red-script')
-- Start the conversation, send a message to blue script
blue:Send('Hello')
-- Check for any message for red script.
red:OnReceive(function(signal, data)
-- print blue script's message
print('Blue: ' .. data)
-- Send a message to blue script, then kill the signal.
blue:Send('Bye.')
signal:Close()
end)
LocalSpeechProxy Example
This example handles local (say, yell, whisper, and spell cast) messages.
LocalSpeechProxy.OnReceive("myproxy", function(proxy, mtype, speaker, level, text)
print(string.format("{%s} %s [%d]: %s", mtype, speaker, level, text))
end)
11:39 {yell} DarkstaR [666]: I'M AWESOME
*MessageProxy Example
This example handles error, battle, loot, and look messages.
function messageProxyCallback(proxy, message)
print(proxy:Key() .. ": " .. message)
end
ErrorMessageProxy.OnReceive("ErrorMessageProxy", messageProxyCallback)
BattleMessageProxy.OnReceive("BattleMessageProxy", messageProxyCallback)
LootMessageProxy.OnReceive("LootMessageProxy", messageProxyCallback)
LookMessageProxy.OnReceive("LookMessageProxy", messageProxyCallback)
11:39 ErrorMessageProxy: You cannot use this object.
11:39 A spider loses 20 hitpoints due to your attack.
11:39 BattleMessageProxy: A spider loses 20 hitpoints due to your attack.
11:39 Loot of a spider: 2 gold coins
11:39 LootMessageProxy: Loot of a spider: 2 gold coins
11:40 You see dirt floor.
11:40 LookMessageProxy: You see dirt floor.
For download and operation instructions, refer back to this thread:
http://forums.xenobot.net/showthread.php?19