Toribash
another question .....how can i pick up an random element from a table which uses strings as index/key?

like:
a["aa"]="ddd"
a["vadsv"]="vjicv"
a["02a"]="..."

how can i randomly pick a key/value from a[]?
put the keys in a numeric table, say "kees[]" and choose a random number n from 1 to #kees. kees[n] is your random key.

Edit:
Or just choose a random number m from 1 to #a, then loop over "k,v in pairs(a)" and stop at the m-th iteration.
Signature temporarily out of order.
Originally Posted by psycore View Post
Or just choose a random number m from 1 to #a, then loop over "k,v in pairs(a)" and stop at the m-th iteration.

sorry, is it possible to use "#a" as the length? i can't use it when the keys are all string.
lua code:
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end


http://stackoverflow.com/questions/2...in-a-lua-table
[23:23:53] <AndChat|700625> Blue eyes ultimate dragon best card
[23:24:29] <AndChat|700625> You know the one with 3 heads
[23:24:39] <~Lightningkid> just like my dick



[11:35:40] <box> Hampa suck
[11:36:21] <hampa> not the first to tell me that today
Originally Posted by wayto View Post
sorry, is it possible to use "#a" as the length? i can't use it when the keys are all string.

Oops, yeah, forgot that. See box' answer.

I'd recommend my first approach then since it loops over the array only once. You could also store the entry count as an extra field in the table.
Last edited by psycore; Aug 8, 2014 at 06:32 PM.
Signature temporarily out of order.
why does this work



but this doesnt?

Fukin spice and wolf
string.find() takes a regular expression (also called pattern in lua) as argument. "[" and "]" are special characters in a regular expression. For example 'string.find(yourString, "[aeiou]")' will find the first vocal in your string. You can read more about this in the lua-users wiki.

I think 'find.(a, "]")' works because it is not a valid regular expression because of the unmatching bracket so lua just assumes you are searching for the string instead.

The solution to your problem is to put a "%" infront of the bracket: 'find.(a, "%[")'
The "%" means "if a special character follows, don't treat it as a special character but use it literal instead".

Also a quick tip: You can write 'a:find("%[")' instead of 'string.find(a, "%[")'. Note the colon though, it's necessary. Works only for variables so '"asdf":find(...)' won't work.
Signature temporarily out of order.
Sorry guys, I forgot from the last time I was here.

Is there documentation for all of the toribash lua functions? I'm going to be really surprised if there isn't documentation that has every command. I can't find anything though.


Failing that, what's the command to detect/see who sent TC to the player running the script?

And is there a way to send TC with a script?


I'm thinking I probably have to use that addon that lets you use http functions, and have it login and do it that way?
Last edited by isaac; Aug 13, 2014 at 10:15 PM.
Originally Posted by isaac View Post
Is there documentation for all of the toribash lua functions? I'm going to be really surprised if there isn't documentation that has every command. I can't find anything though.

There isn't. In your data/script/ directory there's a folder called "sdk" that explains some of them but not all. You can get every function name by searching through a table called "_G". It contains every global variable, including functions. You won't get any documentation though and some of the functions don't seem to do anything.


Originally Posted by isaac View Post
what's the command to detect/see who sent TC to the player running the script?

And is there a way to send TC with a script?


I'm thinking I probably have to use that addon that lets you use http functions, and have it login and do it that way?

You can't do that. May work with an extention but I don't know/use any. Use at your own risk.
Signature temporarily out of order.
Is there any more effective way to get someones name from a line in chat than this?
local function getName(chat)

if string.find(chat, ")", 0) ~= nil then 
usr = string.sub(chat, string.find(chat, ")", 0), string.find(chat, ">")-4)
lineStart = string.find(chat, ")", 0)
lineEnd = string.find(chat, ">")-4
elseif string.find(chat, "]", 0) ~= nil then 
usr = string.sub(chat, string.find(chat, "]", 0), string.find(chat, ">")-4)
lineStart = string.find(chat, "]", 0)
lineEnd = string.find(chat, ">")-4

else usr = string.sub(chat, string.find(chat, "<", 0) + 4, string.find(chat, ">")-4)
lineStart = string.find(chat, "<", 0)+4
lineEnd = string.find(chat, ">")-4

end
return usr, lineStart, lineEnd

end

add_hook("console","console",getName)
Last edited by IceColor; Oct 21, 2014 at 04:38 PM.
Fukin spice and wolf