This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ry00000's profile picture

How to make cursors?

Started by ry00000, 07 May 2015 - 11:30 AM
ry00000 #1
Posted 07 May 2015 - 01:30 PM
How to make arrow cursors?
like this

> menu item <

menu item 2

menu item 3

and detect when it's selected?
Lyqyd #2
Posted 07 May 2015 - 03:59 PM
For something like this, you want to have a group of options. You'll probably want to keep your list of options in a table. You'll need to keep track of which item is selected, which you can use a simple number variable for. You'd probably want the number to be 1-n and correspond with which item in the table is currently selected. Then you'll want to create a function that draws the contents of the table to the screen, and when it gets to the selected entry, draws it with the selection markers around it. After that, you just need an event handling loop that adds to or subtracts from the selection variable and calls the redrawing function whenever it loops.

Edit: Twice now, I've deleted posts from this topic wherein someone just threw code at the question asker and said essentially, "here, use this or learn from it." OP isn't asking for a pre-made program/API, they're asking to learn how to do this. It's okay to mention code that already does this well, but that shouldn't be the entire content of an answer post. Code posted as answers to "How do I x?" questions in Ask a Pro should be very well commented at the minimum, or should be thoroughly walked through to explain what's going on at each step of the way. Remember, we are here to assist people in coding and learning to code, not to throw code at people and expect them to understand our optimized uncommented programs.
Bomb Bloke #3
Posted 07 May 2015 - 11:40 PM
Difficult to address this without knowing your level of knowledge.

Are you familiar with tables, basic control structures (loops in particular, both "for" and "while"), and os.pullEvent()? Start out by reading up on those, see what you can write after that, then post a pastebin link to what you end up with if you get stuck with it.
ry00000 #4
Posted 08 May 2015 - 03:04 PM
Yup, I'm familiar with all of that.
ry00000 #5
Posted 08 May 2015 - 03:16 PM
But how do you get the options in the function arguments?


function menu(title, ...)
  print(...)
end
It returns this
m.menu("Hi", "Test", "Test 2")
where m is the filename
TestTest 2
Hi
How do you store the args in a table?
KingofGamesYami #6
Posted 08 May 2015 - 03:28 PM
… represents all the arguments after title, you can put them in a table by placing them within a table constructer, like this:


local exampletbl = {...}
ry00000 #7
Posted 08 May 2015 - 03:30 PM

function test(...)
  testtab = ...
end

Ahhh! Thx a lot!

It worked!!! It worked!!! Thanks! Menu API, I'm COMING!!!
KingofGamesYami #8
Posted 08 May 2015 - 03:31 PM
You forgot the {}, without it it will return the first argument only

function test( ... )
  local testtab = ...
  print( testtab )
end

function correct( ... )
  local testtab = { ... }
  for k, v in pairs( testtab ) do
    print( k .. ": " .. v )
  end
end
ry00000 #9
Posted 08 May 2015 - 03:55 PM
How to run a function in a table like so:

table[3]["fnc"]

Like this?

table = {"Stuff", "More Stuff", {fnc = function() print("Stuff!") end}}

It returns "bios:366: [string "m"]:28: '=' expected"
Edited on 08 May 2015 - 01:53 PM
Bomb Bloke #10
Posted 08 May 2015 - 04:00 PM
()
ry00000 #11
Posted 08 May 2015 - 04:06 PM
Here's the Pastebin for the API: sg4JWBfX
ry00000 #12
Posted 08 May 2015 - 04:14 PM
Pastebin for an errored API: 9H3L0UUq
Please help!
KingofGamesYami #13
Posted 08 May 2015 - 06:12 PM
What, exactly, are you getting as an error? I'd assume you are getting api:11:attempt to index field '?' (a nil value) because you have nothing restricting the itemSelected variable to between 1 and the amount of items in your table.
ry00000 #14
Posted 08 May 2015 - 07:49 PM
The error I posted at the execute function thing
KingofGamesYami #15
Posted 08 May 2015 - 09:37 PM
I'm confused. Link to pastebin, full error please. Preferably with the code using the API as well, you may be misusing it.
Bomb Bloke #16
Posted 09 May 2015 - 08:48 AM
Ah, right. Ditch the quotes.
ry00000 #17
Posted 10 May 2015 - 02:28 PM
9H3L0UUq
and the error is:
bios:366: [string "m"]:28: '=' expected

Link is http://www.pastebin.com/9H3L0UUq
flaghacker #18
Posted 10 May 2015 - 03:30 PM
I'm to too sure, but I think you cant call [] brackets. I suggest changing line 28 to

items[itemSelected].func()

I'm not on a pc right now, so I can't test to make sure.
ry00000 #19
Posted 11 May 2015 - 02:17 PM
And also, the API freezes at times.
flaghacker #20
Posted 11 May 2015 - 03:36 PM
And also, the API freezes at times.

Where does it freeze? Are you playing in a world with a lot of computers (eg. a server)?

I found a little bug, lines 24 and 25 should be

if itemSelected < 0 then
  itemSelected = #items
ry00000 #21
Posted 11 May 2015 - 04:23 PM
Nup

in my CCemuRedux