Cool VL Viewer forum

View unanswered posts | View active topics It is currently 2025-04-29 09:36:18



Reply to topic  [ 4 posts ] 
Help with a defining a Lua function utilising a callback 
Author Message

Joined: 2011-09-27 11:18:31
Posts: 234
Reply with quote
Hello! I am trying to define a function that takes as argument the full path of an inventory item, calls the PickAvatar with the "true" parameter, then sends the item to the results of the OnPickAvatar() callback… but I fail.

Code:
function ShareInventory (inventory)
    PickAvatar(true)
    local avatarlist = OnPickAvatar(avatars)
    for key,value in avatarlist
    do GiveInventory(inventory, key)
    print("Sent " .. inventory .. " to " .. value)
    end
end

function OnPickAvatar(avatars)
    return avatars
end


If I try to run it with ShareInventory("test"), where test is an empty notecard in the root of the inventory, the Lua console opens the avatar picker, but then tells me [string "function ShareInventory (inventory)..."]:4: attempt to call a nil value (for iterator 'for iterator'). Could you help?

I know, I could put the sending part inside the callback, but I want to keep it clean for other uses. (I want to make a function that invites a mass of people to a goup, for example.) All I want from the callback is to give me its result, so I can use it otherwise.


2025-04-26 16:22:03
Profile

Joined: 2009-03-17 18:42:51
Posts: 5976
Reply with quote
Err... The OnPickAvatar() Lua callback for PickAvatar() gets only called after the avatar picker floater is closed...
PickAvatar() simply opens the floater and immediately returns. You get the result (the avatar the user picked) in OnPickAvatar(), whenever the pick avatar floater is closed.

Code:
inventory_to_share = ""
function ShareInventory(inventory)
    inventory_to_share = inventory
    PickAvatar(true)
end

function OnPickAvatar(avatars)
    if avatars == nil or inventory_to_share == "" then
        return
    end
    for id,name in avatars do
        GiveInventory(id, inventory_to_share)
        print("Sent " .. inventory_to_share .. " to " .. name)
    end
end


2025-04-26 19:40:40
Profile WWW

Joined: 2011-09-27 11:18:31
Posts: 234
Reply with quote
Ah, I see. It has to be there. Thanks, I'll try it!


2025-04-27 10:12:15
Profile

Joined: 2009-03-17 18:42:51
Posts: 5976
Reply with quote
g0rd0ngrfr33mailgr wrote:
Ah, I see. It has to be there. Thanks, I'll try it!
The whole idea behind a callback function, is that it is there to implement asynchronous operations (among which event observers, which is not the point here, but definitely is for callbacks such as OnReceivedChat(), for example): some functions (such as PickAvatar()) just cannot return a result immediately (synchronously), since they depend on external events (a user input, a server reply to a request sent by the function, etc). PickAvatar()) cannot wait for the user input (which would freeze the whole viewer), so it returns immediately and you get the result whenever it arrives, in the OnPickAvatar() callback.


2025-04-27 10:41:22
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.