Log In  

Cart #29283 | 2016-09-24 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

Guys, do any of you know how to make the onboard menu plugin command work properly in this code ?

I want the text, "confirmed" to only appear WHEN you select the menu item. From here, don't press [A] as that will only exit the program.

Instead press [ENTER] and select, "test." You will see that it does not run the function associated with it.

-- broken menu
-- you can test this in splore
-- or online

function main()

menuitem(1,"test",doit())
getkey("check menu now")

end

function doit()
  print"confirmed !"
end

-- simple get and wait key [a]
function getkey(t)
  if t!=null then
    print""
    print(t.." - press [a]")
  end
  repeat
    flip()
  until btn(4)==false
  repeat
    flip()
  until btn(4)
end--getkey

main() -- nifty put at top
P#29284 2016-09-24 12:31 ( Edited 2016-09-25 15:29)

In the line:

menuitem(1,"test",doit())

you are calling the funtion 'doit' instead of providing a reference to it.

To provide the reference, just use the function name, without the parenthesis:

menuitem(1,"test",doit)
P#29291 2016-09-24 13:56 ( Edited 2016-09-24 17:56)
:: dw817

Okay, let me change the code to yours.

...

It still doesn't work. Here is the proof.

Cart #29294 | 2016-09-24 | Code ▽ | Embed ▽ | No License

P#29295 2016-09-24 14:06 ( Edited 2016-09-24 18:06)

The system menu runs outside of the normal code, and I assume using flip() also suspends execution. (from the manual: "Flip the back buffer to screen and wait for next frame" emphasis mine)

I put in an empty _update() and that gave it time/space to execute the function call from the menu. (after an extra press to get out of the flip() loop.)

edit:

function main()

menuitem(1,"test",doit)
getkey("check menu now")

end

function doit()
  print"confirmed !"
end

-- simple get and wait key [a]
function getkey(t)
  if t!=null then
    print""
    print(t.." - press [a]")
  end
  repeat
    flip()
  until btn(4)==false
  repeat
    flip()
  until btn(4)
end--getkey

main() -- nifty put at top

function _update()

end
P#29298 2016-09-24 14:53 ( Edited 2016-09-24 18:55)
:: dw817

Hi Tyroney. You are very close. It works only AFTER I press (A). I want to have it work BEFORE or AFTER you press (A).

Basically, I'd like the menu to work no matter what function it is in, what loop it is in, or anything else that might be happening. A true interrupt. If this is impossible, I understand and can develop my own menu system around this.

P#29300 2016-09-24 15:33 ( Edited 2016-09-24 19:35)

Your code is stuck in a loop, leaving no time for the menu callback.

Here's some code that demonstrates when the callback fires. (in this case in _update after its first if statement, and before the second if statement.)

function pause(a)
    print(a)
    repeat
    until btn(4)
end

u=0
flag=true
menuitem(1,"test",function() print("done") flag2=true end)
cls()
pause("a")
pause("b")
pause("c")

function _update()
    u+=1
    if (flag) print("update "..u) flag=false
    if (flag2) print("ding") flag2=false
end

edit: I don't know exactly how the callback gets inserted into execution, but no matter how many pause()s I run, it still doesn't happen until _update() gets run.

P#29305 2016-09-24 16:10 ( Edited 2016-09-24 20:21)
:: dw817

Seems to me it's a fault (or shortcoming) of PICO itself. Interrupts are pretty amazing things to write in code, and sometimes the language just won't accept it, or it can't be done.

Let me try your latest code. ...

Yeah, it's still got the same problem. When I run it from SPLORE, and the first thing I do is hit [ESC] and try out the TEST menu, it doesn't work.

Beginning to believe the menu can only be active when not inside another loop or function. :(

Would be a real advantage though if it could. (Penning my own notes ... need to make sure something like this will work in my own game maker).

Reflecting. It's really not that difficult, and if multitasking is the problem, just FREEZE whatever code was running, then give priority to the code from the menu, then return back.

P#29308 2016-09-24 16:24 ( Edited 2016-09-24 20:27)

The idea of interrupts isn't required for the Pico8 because it isn't simulating actual hardware.

Like JavaScript, you can write code which use callbacks to simulate interrupts.

P#29361 2016-09-25 08:26 ( Edited 2016-09-25 12:26)
:: dw817

I'm good. I'm making my own menu system now, better than the default that can be activated anywhere by pressing both LEFT and RIGHT. That's how you win at this. :)

Thanks for all your help and patience though guys !

P#29366 2016-09-25 11:29 ( Edited 2016-09-25 15:29)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2019-12-12 12:00 | 0.023s | 4194k | Q:45