random reaction table

Post Reply
ella
Posts: 33
Joined: Sun Feb 28, 2010 5:46 pm

random reaction table

Post by ella »

alright, what I am trying to do is make a table that will automatically fire off one of several pre-made emotes at a certain trigger. I need a little push in the direction of how this coding would work though

kakku
Posts: 42
Joined: Tue Feb 22, 2011 12:03 pm

Re: random reaction table

Post by kakku »

maybe something like this works?

set this up first:
tableName = { "do this", "do that", "do something silly", "do something clever" }
then on trigger:
local randNumb = math.random(table.size( tableName ))
for numb, thingTodo in pairs(tableName) do
if numb == randNumb then send(thingTodo) end
end

SomeDude
Posts: 11
Joined: Sat Mar 05, 2011 9:02 am

Re: random reaction table

Post by SomeDude »

kakku wrote:maybe something like this works?

set this up first:
tableName = { "do this", "do that", "do something silly", "do something clever" }
then on trigger:
local randNumb = math.random(table.size( tableName ))
for numb, thingTodo in pairs(tableName) do
if numb == randNumb then send(thingTodo) end
end
table.size does not exist lua5.1. Also, iterating through the table to find the command is unintuitive given random access abilities.

Using table indexing ([]) and the length operator (#), you can just do:

Code: Select all

cmdTable = {"blee", "bloo", "blaa"}
send(cmdTable[math.random(#cmdTable)])

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: random reaction table

Post by Vadi »

table.size comes with mudlet, though I agree that is a much better solution (similar to http://forums.mudlet.org/viewtopic.php? ... 234&p=4600)

ella
Posts: 33
Joined: Sun Feb 28, 2010 5:46 pm

Re: random reaction table

Post by ella »

after giving this a few tries this simply dosent work, something about the '}''s wanted around the emote I want to send

SomeDude
Posts: 11
Joined: Sat Mar 05, 2011 9:02 am

Re: random reaction table

Post by SomeDude »

Post the code you're using and the error message you're being given?

Post Reply