72 posts
Posted 09 September 2013 - 09:43 PM
I already know what is wrong with this that im trying to call a table when it needs a string but unsure on how to fix it. Line 16.
local commands = {["speak"] = speak, ["dance"] = dance}
function speak()
print("I spoke")
end
function dance()
print("I danced")
end
while true do
local event, player, message = os.pullEvent("chat")
if message:find("turtle:") and message:find(commands) then -- this is where i know i messed it up so how would i combine this to be a string here instead a table
commands[message]()
else
m.say("I don't know that command")
end
end
http://pastebin.com/WiYMt29SSo how would i change it so it could look through the table so when u say speak in chat it will find the ["speak"] in the table and execute it. if i understand it right i have the stored list and the stuff to make it executable but just dont know how to connect them.
7508 posts
Location
Australia
Posted 09 September 2013 - 09:47 PM
Sadly if they're going to be entering something like "turtle:speak" then you'll need to do a loop with the way you've done it.
the only way to keep the lookup table would be to add "turtle:" to the front of all your keys in the commands table.
8543 posts
Posted 09 September 2013 - 09:55 PM
Uh, what? You must be having a really off-day, theoriginalbit. You just snip the "turtle:" part off.
local command = string.match(chatString, "turtle:(.*)")
if commands[command] then
commands[command]()
end
72 posts
Posted 10 September 2013 - 02:24 AM
where does the chatString come from, lyqyd. I am new with the string.match I understand what the symbols mean, but not how to use them properly. Let me fully explain the code so can get a better idea on what looking for.
What I want for this to do is look in the chat using the chatbox from miscPripherals. I want it to look for "turtle:" so that you can still talk to people in chat without messing with the computer, pretty much a "/". The reason I don't want it to look for "turtle: speak" is so that you can either say "turtle: speak" or "turtle: will you please speak for me?" and it will do the same thing, just so that its more interactive. It will check the table with what was said into chat to see if there where any commands said for example "speak" turtle will say in chat "I spoke" otherwise if it couldn't find the command it will say "I don't know that command", as i'm sure you can tell in the code. The code I pasted was just a concept design the turtle will do more that just speak in chat, its just easy to test it this way. The main point to this program will be able to make turtle companions with different personalities and features. Just want to get the logic going before I get into the majority of this project. Plus I feel this will be an interesting way for me to learn about string matching.
7508 posts
Location
Australia
Posted 10 September 2013 - 05:03 AM
Uh, what? You must be having a really off-day, theoriginalbit. You just snip the "turtle:" part off.
Oh wow, I really am/was…
72 posts
Posted 11 September 2013 - 03:58 AM
So this is what I have atm.
m = peripheral.wrap("left")
local commands = {["speak"] = speak}
function speak()
m.say("I spoke")
end
while true do
local event, player, message = os.pullEvent("chat")
local command = string.match(message, "turtle:(.*)")
print(command) -- For testing only
if commands[command] then
commands[command]()
else
m.say("I don't know that command")
end
end
But no matter what I say in chat it says "I dont know that command" which is suppose to be done when it cant find the command in the table. And it is returning correctly as far as I know if i say turtle: speak then speak is returned to check the table, but it acts like its not there.
997 posts
Location
Wellington, New Zealand
Posted 11 September 2013 - 04:39 AM
So this is what I have atm.
m = peripheral.wrap("left")
local commands = {["speak"] = speak}
function speak()
m.say("I spoke")
end
while true do
local event, player, message = os.pullEvent("chat")
local command = string.match(message, "turtle:(.*)")
print(command) -- For testing only
if commands[command] then
commands[command]()
else
m.say("I don't know that command")
end
end
But no matter what I say in chat it says "I dont know that command" which is suppose to be done when it cant find the command in the table. And it is returning correctly as far as I know if i say turtle: speak then speak is returned to check the table, but it acts like its not there.
You can't put the function in the table before you create the function.
1688 posts
Location
'MURICA
Posted 11 September 2013 - 04:39 AM
Your matching code doesn't check for spaces. Replace this:
local command = string.match(message, "turtle:(.*)")
With this:
local command = string.match(message, "turtle:%s*(.*)")
"%s" is a space character, and "*" means zero or more. Therefore this code matches "zero or more space characters" in that area.
72 posts
Posted 12 September 2013 - 01:15 AM
Ok, problem is that I need to have it where other stuff can be said with for example "turtle: can you speak for me" or "turtle: speak damn you" and it will still work. At the moment unless i did something wrong it only works with "turtle: speak"
8543 posts
Posted 12 September 2013 - 02:10 AM
Well, for that, you'd have to iterate through every word in the phrase and check for functions in your table. You can use string.gmatch to accomplish this:
local comStr = string.match(chatLine, "^turtle:%s*(.*)")
if comStr then
for word in string.gmatch(comStr, "(%S+)") do
if commands[word] then
commands[word]()
break
end
end
end
72 posts
Posted 12 September 2013 - 02:57 AM
yes it works perfectly thank you very much for your time, any good tutorials on something like this in the tutorial section?