简体   繁体   中英

Iup Lua callback -- what am I doing wrong?

I'm using Lua 5.1 with IUP 3.5, and trying to use a list callback to populate an Address list depending on the Place selected. (The list is an editbox, so I will need to handle that in due course, but let us deal with the basics first). I've clearly got a fundamental misunderstanding of how to do this.

The code:

function MakeAnIupBox
    --make some more elements here
    listPlace = iup.list{}
    listPlace.sort = "YES"
    listPlace.dropdown = "YES" 
    --populate the list here
    --now handle callbacks
    listPlace.action = function(self) PlaceAction(text, item,  state) end
end

function PlaceAction(text, item, state)
    listAddress.REMOVEITEM = "ALL"
    if state == 1 then -- a place has been selected
    --code here to populate the Addresses list
    end
end

The iup documentation describes the action callback for a list as

ih:action(text: string, item, state: number) -> (ret: number) [in Lua]

However, when I run this code I get:

  • text -- looks like some sort of metatable
  • item, state -- both nil

I've also tried coding the callback as

function MakeAnIupBox
    --make some more elements here
    listPlace = iup.list{}
    listPlace.sort = "YES"
    listPlace.dropdown = "YES" 
    --populate the list here
end
function listPlace:action (text, item, state)
    listAddress.REMOVEITEM = "ALL"
    if state == 1 then -- a place has been selected
        --code here to populate the Addresses list
    end
end 

but that fails to run: the error is attempt to index global 'listPlace' (a nil value)

I'd prefer not to embed the callback in "MakeAnIupBox" because I'm hoping to make it (and the other associated callbacks) a resuable component in several Lua programmes that all process similar datasets but from different UIs.

If you do not want to embed the callback function inside your function, you can define it before and later assign it to your specified destination.

function Callback(self, a, b)
   -- do your work ...
end

function CallbackUser1()
    targetTable = { }
    targetTable.entry = Callback
end

function CallbackUser2()
    otherTargetTable = { }
    otherTargetTable.entry = Callback
end

This solution needs the arguments to be always the same.

Note: All following definitions are identical

function Table:func(a, b) ... end
function Table.func(self, a, b) ... end
Table.func = function(self, a, b) ... end

The problem is in Lua usage.

In the first case, remember that this:

function ih:action(text, item, state)

translates into this:

function action(ih, text, item, state)

So it is missing the ih parameter.

In the second case, listCase exists only after MakeAnIupBox is called. You can solve that by declaring the function inside the MakeAnIupBox scope.

Building on the suggestion of Antonio Scuri which was not totally explicit, I have worked out that the code needs to read:

function MakeAnIupBox
    --make some more elements here
    listPlace = iup.list{}
    listPlace.sort = "YES"
    listPlace.dropdown = "YES" 
    --populate the list here
    --now handle callbacks
    listPlace.action = function(self, text, item, state) PlaceAction(listPlace, text, item,  state) end
end

function PlaceAction(ih, text, item, state)
    listAddress.REMOVEITEM = "ALL"
    if state == 1 then -- a place has been selected
    --code here to populate the Addresses list
    end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM