• Looking through a list of strings
    7 replies, posted
I am currently working on a Unstuck Command for a Prop Hunt Server. As I was working on it I needed to use a list of Strings; these strings would be all the commands that a player could say to get unstuck. hook.Add("PlayerSay", "playersaystuck", function(ply, text) local Commands = { "!unstuck", "!stuck", "/stuck", "/unstuck" } if (list.Contains(Commands, text)) then What are other ways for me to call for things in the list. I have tried the method above and below. hook.Add("PlayerSay", "playersaystuck", function(ply, text) local Commands = { "!unstuck", "!stuck", "/stuck", "/unstuck" } if (Commands[text]) then I would personally like to stay away from... hook.Add("PlayerSay", "playersaystuck", function(ply, text) if (text == "!unstuck" || text == "!stuck" || text == "/stuck" || text == "/unstuck") then
This is probably the fastest method local Commands = { "!unstuck" = true, "!stuck" = true, "/stuck" = true, "/unstuck" = true } if Commands[text] then
You could also do something like --Please dont define tables inside hooks without good reason local Commands = { "!unstuck", "!stuck", "/stuck", "/unstuck" } hook.Add( "PlayerSay", "PlayerSayStuck", function( ply, text ) --when taking user input its best to lower unless it should be case sensitive local text = string.lower(text) if table.HasValue(Commands, text) then     -- Unstick them end end)
table.HasValue is very slow. It goes through the entire table every single time just checking for one value. I don't recommend this method.
Lets not be petty on such pathetic differences in speed. We all know the difference in speed is so small you cant even measure it
That's not the point (although it's actually very easily measurable); it's the promotion of bad habits which start to accumulate into inefficient code in performance-heavy situations.
Just because you can't measure the difference in this example doesn't mean someone using it for a huge chunk of code isn't going to copy paste your answer and cause their code to slow down immensely. If there's a way to avoid writing slow code, why not take it?
Sorry, you need to Log In to post a reply to this thread.