Echo can used be both in synced and unsynced.
However,
When you press a key, you are alone to press the key. You're not pressing the key of other players along as yours.
Keypresses are by definition unsynced!
Working exemple:
Code: Select all
function gadget:GetInfo()
return {
name = "KeyCatchEx",
desc = "Show how to catch keypresses in gadgets",
author = "zwzsg",
date = "21st of January 2010",
version = "1.0",
license = "Public Domain",
layer = 0,
enabled = true
}
end
local up_arrow,down_arrow,right_arrow,left_arrow,enter,space,dee = 273,274,275,276,13,32,100
local message_identifier = gadget:GetInfo().name..": "
if (gadgetHandler:IsSyncedCode()) then--SYNCED
function gadget:RecvLuaMsg(msg,player)
local MsgForMe=string.match(msg,message_identifier.."(.*)")
if MsgForMe then
Spring.Echo("Synced recieved a message from unsynced:")
Spring.Echo(MsgForMe)
end
end
else--UNSYNCED
function gadget:KeyPress(key)
Spring.Echo("Key "..key.." pressed in unsynced")
Spring.SendLuaRulesMsg(message_identifier..key.." was pressed")
end
end