0

I have a problem which i suppose must be very common and most of you would have faced it. I have written a program in lua, say main.lua which on receiving key event should modify the coordinates and display the geometry figure. This lua code calls reg.c, where it kind of registers. Now in reg.c i have a function engine which receives the key pressed and passes it to the lua function responsible for key handling. But by the time key event comes, lua code is done with the registration and exits, thus the call from engine() becomes illegal memory access leading to segmentation fault.

Also i suppose we can't have lua call hanging in reg function, and call engine function from somewhere else.

Then what should be the solution, please guide me through this.


@jacob: here is the prototype of what i am trying to achieve:

function key_handler() //this function will get the latest key pressed from some other function
{
     draw.image();
     draw.geometry();
     ...
     ...

     while(1)
     {
         //draw Points until some condition goes wrong      
     }

}

Now, once entered into key_handler, while he is busy drawing the points unless and until the failing condition occurs, i am unable to receive key pressed till that time.

I hope this explanation is much simpler and have made my point, and will help others to understand the problem. I am really sorry, but i am not good at expressing or making others understand.

One more thing, i ahve followed the C syntax to explain, however this is completely implemented in lua

lhf
  • 70,581
  • 9
  • 108
  • 149
ashutosh
  • 79
  • 3
  • 8
  • It's hard to see your setup and what you are trying to accomplish from your question (eg. no one knows what `reg.c` does or is supposed to do, same for `engine()`). Please elaborate, and give a minimal code sample to demonstrate what does not work. – jpjacobs Jun 27 '12 at 13:10
  • @jpjacobs : I have updated the problem to the best of my efforts, kindly see if you can recommend me some solution to the problem – ashutosh Jul 03 '12 at 12:56
  • I have tried using coroutines, but it didn't help – ashutosh Jul 03 '12 at 12:58
  • @jpjacobs I have updated the problem to the best of my efforts, kindly see if you can recommend me some solution to the problem – ashutosh Jul 04 '12 at 05:35

1 Answers1

0

Your code snippet is still largely non-informative (ideally one should be able to just run your code in a stock Lua interpreter and see your problem). If you're describing a Lua problem, use Lua code to describe it.

However I'm beginning to see where you want to go.

The thing you need to could do is have a coroutine that's called in your key handler, which passes an argument back to your handler:

function isContinue() --just to simulate whatever function you use getting keypresses.
-- in whatever framework you're using there will probably be a function key_pressed or the like.
    print('Initialize checking function')
    while true do
        print('Continue looping?')
        local ans = io.read():match('[yY]')
        local action
        if not ans then
            print('Do what instead?')
            action = io.read()
            if action:match('kill') then -- abort keychecker.
                break
            end
        end
        coroutine.yield(ans,action)
    end
    print('finalizing isContinue')
    return nil,'STOP' -- important to tell key_handler to quit too, else it'll be calling a dead coroutine.
end

function key_handler()
    local coro = coroutine.create(isContinue)
    local stat,cont,action
    while true do
        print'Draw point'
        stat,cont,action = coroutine.resume(coro)
        if not stat then
            print('Coroutine errored:',cont)
        elseif not cont then
            print('isContinue interrupted keyhandler')
            print("We'll "..action.." instead.")
            break
        end
    end
    print('finalizing key_handler')
end

key_handler()
-- type something containing y or Y to continue, all else aborts.
-- when aborting, you get asked what to do instead of continuing, 
--- with "kill" being a special case.

This should be self explanatory. You should probably take a good look at Programming in Lua, chapter 9: Coroutines.

The big difficulty (well, if you're not accustomed to collaborative threading) is that a coroutine should yield itself: it's not the calling function that's in charge of returning control.

Hope this helps you.

jpjacobs
  • 9,359
  • 36
  • 45