Ranking
Original Post
[DEV] Useful Function Database
I feel we should have some place to post useful bits of code (try to help new coders, stuff like that). So feel free to post any functions or whatever you find useful

Guess I'll try to get the ball rolling:
-- Variable Change Tester
-- USAGE: /echo a=b; c = a; b   = -34.34323   ; 
add_hook("console","",
    function(s,i)
        if(s:find("=")) then -- if the string incoming is a set variable command.
            if(s:sub(-1) ~= ";") then -- add ; to the end if it doesn't have it.
                s = s .. ";"
            end
            s = string.gsub(s,"%s","") -- Get Rid of spaces.
            for k, v in string.gmatch(s, "(.-)=(.-)%;") do -- Go through each "k=v;".
                _G[k] = v -- Set the variable k to v
                echo("Set: \"" .. k .. "\" to \"" .. v .. "\"") -- Echo to confirm change.
            end
            return 1 -- Don't show the line in chat.
        end
    end
)
Last edited by Blam; Nov 28, 2009 at 03:22 PM.
:D
MEGA BUMP. This adds a very simple custom chat interface if you wish to draw over the existing chat but still want to access for example echoes.

-- Setup the chat variables and console hook, override echo command.
local maxchat = 8 --Max number of lines.
local chat = {}; for i = 1,maxchat do chat[i] = "" end -- Make and populate chat table
_echo = echo; function echo(a) run_cmd("echo "..a) end --Echo now processed by console hook.
add_hook("console","chat",function(text,ttype) for i = 2,maxchat do chat[i] = chat[i-1]; chat[1] = text end end) -- Moves array to the right and adds chat to beginning of array.

-- Rest of code

-- Draw Chat, place after all others so chat draws on top.
add_hook("draw2d","chatdraw",function() set_color(0.6,0,0,1); for i = 1,maxchat do draw_text(chat[i],5,height-((i*15)+20),1) end end) -- Draws all the chat.
:D
I don't know Lua, but I think it's fairly self-explanatory. The commenting is good enough to get me through, anyway.
It adds a hook for the console, which I guess is the chatbox in-game. I have no idea what the second parameter does, but I'll guess that it's not too important if it's an empty string. The third parameter should be a callback.
I don't know why "i" is there, as it isn't used, but "s" seems to be the string that is to be echoed. The callback function checks for an "=" in the string, rather loosely ensuring that it is actually supposed to perform whatever operations it performs with the string.
Then it makes sure that the string ends with a semicolon; if it doesn't, it appends one. I'm not sure what the gsub function does, but I'll hazard a guess at some sort of pattern replacing function. The comment says that it gets rid of spaces, so I'll believe it.
"for k, v in ..." seems much the same as PHP's "foreach (... as $k => $v)", and the gmatch function looks like it iterates through the string s to find k=v pairs, delimited by a semicolon. That looks similar to a RegEx, but I don't know what the "%" does. Possibly the same as a backslash in a RegEx.
_G looks like PHP's $GLOBALS array, which contains a reference to all variables in the global scope. Setting _G[k] to v would be the same as setting the value of k to v. That probably doesn't make sense, so pretend "k" is set to "foo".
_G[k] = "bar" would be the same as foo = "bar".
Then a line is echo()'d to somewhere, probably the chatbox, to tell the user what's going on.
Returning a value of 1 seems to prevent the line that you have echo()'d with the command "/echo" from showing up in the chatbox.

There's a lot of uncertainty in the above, but I think that's the general idea of it. I'll attempt to learn a little bit of Lua sometime in the future and correct any mistakes.
Although there was a lot of guessing ZaggZigg is basicly right.
If you add that piece of code to your script, you can edit your variables via the chat.
Let's pretent there's a variable called 'text' in your script and your script does echo(text) if you hit a button.
If you type in the chat '/echo text = "asdf";' , press enter and then hit the button, your script echoes 'asdf'.
Signature temporarily out of order.