Code:
The channel class is used to create custom console channels. To better understand how it works, let's first consider what classes are. There are several classes in Xenobot scripting; Self, Creature, Map, Item, etc. When you need to interact with one of these classes you create an object of that class. You do this by calling a constructor function (in most cases something like Creature.New()). Each constructor function for each class requires certain information in order to make your object how you desire. For a channel, that information is a name (shown on the channel tab), a function to be called when a user inputs text, and a function to be called when the channel is closed.
What all this looks like:
################################
-- note that the onSpeak and onClose functions should be defined before creating the channel.
function onSpeak(channel, message)
-- receives the channel object that called it and the message that was inputted.
if (message == 'hi') then
channel:SendOrangeMessage('Robot', 'Hi!')
end
end
function onClose(channel)
-- receives the channel object that called it
print(channel:Name() .. ' has been closed.')
end
-- Now we create the channel.
local customChannel = Channel.New('My Channel', onSpeak, onClose)
################################
Some important functions for using channels.
Channel:SendYellowMessage(senderName, message)
Channel:SendOrangeMessage(senderName, message)
Channel:Close()
I don't really use any of the other ones... :(
Also, it's important to note that we define the callback functions before creating the channel. If they were after, the channel would be created first and it would not know those functions exist (error plx). Also important is the fact that they are passed the channel that called them. Inside the scope of these functions, always refer to the channel object as it is passed in the parameter, not what you have named the channel object when you created it (in my example, inside the function use channel:SendOrangeMessage() and outside the function use customChannel:SendOrangeMessage()).
Hope it helps!