For lists up to a few hundred elements, then the following will work:
messages = {
"text1",
"text2",
"text3",
"text4",
-- ...
}
SendChatMessage(GetRandomArgument(unpack(messages)), "RAID")
For longer lists, you would be well served to replace GetRandomArgument
with GetRandomElement
that would take a single table as its argument and return a random entry from the table.
Edit: Olle's answer shows one way that something like GetRandomElement
might be implemented. But it used table.getn
on every call which is deprecated in Lua 5.1, and its replacement (table.maxn
) has a runtime cost proportional to the number of elements in the table.
The function table.maxn
is only required if the table in use might have missing elements in its array part. However, in this case of a list of items to choose among, there is likely to be no reason to need to allow holes in the list. If you need to edit the list at run time, you can always use table.remove
to remove an item since it will also close the gap.
With a guarantee of no gaps in the array of text, then you can implement GetRandomElement
like this:
function GetRandomElement(a)
return a[math.random(#a)]
end
So that you send the message like this:
SendChatMessage(GetRandomElement(messages), "RAID")