HTOTM: FUSION
Okay, so I've been studying a bit of lua since the first few questions I've asked, but heres 2 things I gotta ask.
1. How do I make a functional button like that in worldbuilder.(not just a square, but like a button that darkens when the mouse scrolls over it)
2. I get how to make a function start with a key press (the whole 'if key == # then' trick), but how do I get that to start with a ./ec phrase? For example I think this was a script by Psycore called Jump2Frame or something like that where you had to do "/ec jump <frame#>" and the replay did /opt for everything and paused it for you at the inserted frame. I've also noticed this line in that script
lua code:
if tonumber(TORIBASH_VERSION) > 3.9 then
add_hook("command", "jumpTo", function(cmd) if cmd:sub(0,4) == "jump" then jumpTo(tonumber(cmd:sub(6))) return 1 end end)
echo("Usage: /jump <frame>")
else
add_hook("console", "jumpTo", function(cmd, i) if cmd:sub(0,4) == "jump" then jumpTo(tonumber(cmd:sub(6))) return 1 end end)
echo("Usage: /ec jump <frame>")
end

I'm guessing that has something to do with how I do the phrase thing I was asking before. Someone mind to explain this for me thanks.
I don't know how detailed my explanation has to be so I'm starting with a few hints and if you need more you can ask again.

1. I guess I will do a tutorial for that some time
You need to draw a rectangle with a background color and some text. You can do that with
set_color(red, green, blue, alpha)
draw_quad(x, y, width, height) -- x,y = coordinates of top left corner
draw_text(text, style)
in the draw2d hook.

To change the color when hovering you have to do three things:
1. store background color and alternative background color (for hovering) in variables
2. Create a variable called isHovering (or something like that). In the function that draws your butotn, check if it is true. If it is use the alternate background color otherwise the default.
3. Check if the mouse is over the button. Do it like this:
local function onMouseMove(x, y)
    -- if x, y is in the bounds of the button then
        isHovering = true
    else isHovering = false
    end
end
add_hook("mouse_move", "checkButtonHover", onMouseMove)
Done.
Btw: You can check if the button was clicked pretty similar to the hover checking with the mouse_button_down or mouse_button_up hook. They use a function with the arguments (mouseButton, x, y)

2. Short explanation of the hooks (scroll down if you just want to know how to use them):
For chat commands there are two hooks you can use: Either console or command. The command hook was introduced in Toribash 3.91 so you have to use the console hook for people who still use earlier versions. This is why in my script I check the Toribash version. The difference between the two hooks is that the command hook is designed to listen for commands, so for example you can type "/jump" to execute stuff. The console hook listens for chat input. Stuff like "/jump" doesn't go to the chat (since it's a command) and therefore doesn't work with the console hook. But if you type "/ec jump" you will use a built in Toribash command that simply echoes "jump" (which will only be displayed locally, so in multiplayer nobody will see this).

Now to how to use the hooks:
the command hook:
local function executeCommand(cmd)
    -- check if the user typed a command you know
    -- a command without arguments:
    if cmd == "help" then
        -- echo usage
        return true
    -- a command with arguments, like /jump 40 to jump 40 frames
    -- cmd will be "jump 40" so we have to check if the first 4 letters are jump:
    elseif cmd:sub(1, 4) =="jump" then
        -- now check how many fames to jump. this will be the 6th character till the end
        -- also we have to turn the string into a number
        jump(tonumber(cmd:sub(6))
        return true
    else return false
    end
end
add_hook("command", "checkCommand", executeCommand)
If you recognise the command, return true, otherwise false.

The console hook works similar, with the difference that it will execute the function everytime the user recieves a chat message and that it looks a little different:
-- use the same executeCommand function
add_hook("console", "checkCommand", function(message, type)
        -- you could check for the message type to make sure it is not a chat message of someone else
        -- but I'm too lazy to find out which is the correct type :D
        return executeCommand(message)
    end
)
Now that I think about it, if you don't need to check the message type it should be possible to simply do
add_hook("console", "checkCommand", excecuteCommand)
I haven't tested it but it should work.

Bottomline of the story: Both work pretty similar but if you can, use the command hook.
Signature temporarily out of order.
Nice explanation. I understand the second part, but I'm kinda confused on the first part (the explanation about the button).
Originally Posted by psycore View Post
You need to draw a rectangle with a background color and some text"

I might've read this wrong, but how do I put a background color on it? Am I supposed to have 2 rectangles in the same spot(one for the background and one for the normal color) or did you just mean a color in general... which means I can just make it blue with set_color(0,0,1,1). But then I don't understand how this would end up with the button darkening when you mouse over it.
For the onMouseMove(x, y) do I replace x, y with the same draw_quad(x, y) I used or does it pick that up on its own.
Also, there's something wrong with the mouse_move code. Since that 2nd line is a comment, it has the extra end and doesn't work. When I delete the end toribash says I need an end. When I delete the comment and the end it still says I need an end, and when I put the end back without the comment, it still doesn't work. I also tried getting rid of the -- to make it not a comment then toribash says I need a then next to then or something weird like that even though there's one there.
And finally, how do I get the highlights and the shade on my square so it looks more 'buttony' like in worldbuilder. Are those just well placed colored small rectangles or is there a way to draw that on automatically. I've tried reading the worldbuilder.lua to figure it out but I never can find what I'm looking for.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you(or anyone else who knows how to do it) feel like it, can you make me a sample button? then maybe I can look at it in notepad. that'd help alot. I just need it to look nice like the clickable ones in worldbuilder, and when I scroll over it, it darkens, and when I click it... lets make it echo "Click" or something like that.
thanks
Last edited by fett175; Aug 5, 2012 at 06:40 PM.
A very basic script to make some buttons:

lua code:
mouse = { x = 0, y = 0, down = false }

-- Create and return a new button
function newButton(label, x, y, w, h, callback)
return
{
label = label,
x = x,
y = y,
w = w,
h = h,
callback = callback,
down = false,
hover = false,
}
end

-- Check if point (x, y) is in rectangle (rx, ry, rw, rh)
function hitTestRect(x, y, rx, ry, rw, rh)
return x > rx and x < rx + rw and y > ry and y < ry + rh
end

-- Update a button ( calls draw )
function updateButton(button)
if(button.down and button.hover and not mouse.down) then
button.callback()
button.down = false
end

if(hitTestRect(mouse.x, mouse.y, button.x, button.y, button.w, button.h)) then
if(mouse.down) then
button.down = true
else
button.hover = true
end
else
button.hover = false
end

drawButton(button)
end

-- Draws a button
function drawButton(button)
if(button.down) then
set_color(0.1,0.3,0.4, 1)
elseif(button.hover) then
set_color(0.3,0.7,0.9, 1)
else
set_color(0.4,0.4,0.4, 0.8)
end
draw_quad(button.x, button.y, button.w, button.h)
set_color(0,0,0,1)
draw_text(button.label, button.x + 1 + math.floor(button.w / 2 - (#button.label*4)), math.floor(button.y + (button.h / 2) - 9))
set_color(1,1,1,1)
draw_text(button.label, button.x + math.floor(button.w / 2 - (#button.label*4)), math.floor(button.y + (button.h / 2) - 10))
end


-- Set up our hooks for drawing and mouse input
add_hook("draw2d", "gui",
function()
updateButton(test)
updateButton(test2)
end
)

add_hook("mouse_move", "gui",
function(x, y)
mouse.x = x
mouse.y = y
end
)

add_hook("mouse_button_down", "gui",
function(button, x, y)
if(button == 1) then
mouse.down = true
end
end
)

add_hook("mouse_button_up", "gui",
function(button, x, y)
if(button == 1) then
mouse.down = false
end
end
)


-- Create our test buttons!
test = newButton("This is Button 1!", 40, 300, 150, 30, function() echo("Button 1 Press!") end)
test2 = newButton("Button 2, DON'T CLICK ME!!", 40, 335, 240, 30, function() echo("Button 2 Press!") end)
:D
.lua sounds so cool. Can anyone give me a site to some good tutorials so I can get started? I a newb, but I've got some basic python, javascript, bash experience
If you already have some experience with other languages the best thing might be to think of something you want to script ans just start coding.

Anyways, here's a list of tutorials:
Toribash Lua tutorials
Tutorials by lua-users.org
Some translated tutorials. The German ones are not bad but I don't know about the English translations. Looks like they didn't translate all of them.
lmgtfy

Last but not least there are scripting classes in the Unibash section but I guess it's easier to try it yourself and just ask questions here.
Signature temporarily out of order.
You'll have to explain a bit more than that for us to help.
:D
heya

whats mean next error:
Error data/script/startup.lua:47: attempt to index field '?'(a nil value)
Error loading script: bla.lua

but, in spite of this message script works right.