Help with a defining a Lua function utilising a callback
Author |
Message |
g0rd0ngrfr33mailgr
Joined: 2011-09-27 11:18:31 Posts: 243
|
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. 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 |
|
 |
Henri Beauchamp
Joined: 2009-03-17 18:42:51 Posts: 5998
|
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.
|
2025-04-26 19:40:40 |
|
 |
g0rd0ngrfr33mailgr
Joined: 2011-09-27 11:18:31 Posts: 243
|
Ah, I see. It has to be there. Thanks, I'll try it!
|
2025-04-27 10:12:15 |
|
 |
Henri Beauchamp
Joined: 2009-03-17 18:42:51 Posts: 5998
|
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 |
|
 |
g0rd0ngrfr33mailgr
Joined: 2011-09-27 11:18:31 Posts: 243
|
Henri, I researched and couldn't find what is wrong. I put your code in my automation script, but, when I call the function with ShareInventory("test"), I get . The "test" exists in the root of my inventory. All I found in the web is that a function must be defined before its result is used, but that is the case.
|
2025-05-06 17:05:31 |
|
 |
Henri Beauchamp
Joined: 2009-03-17 18:42:51 Posts: 5998
|
Where do you call this function from ?... Obviously, being defined inside the automation script, you cannot call it from outside the automation script itself... If you need to call it from somewhere else (the command line, the Lua console, a LSL script, a Lua floater lua_command), then you need to call the function from within a callback (OnAutomationMessage() for example, and then using AutomationMessage() to send the request to share, or from the Lua floater callback for a lua_command).
|
2025-05-06 18:41:15 |
|
 |
g0rd0ngrfr33mailgr
Joined: 2011-09-27 11:18:31 Posts: 243
|
I did not know that! I'll try it, thanks. That is because it is not a built-in function, but one defined in the automation script. Right.
|
2025-05-06 19:04:28 |
|
 |
g0rd0ngrfr33mailgr
Joined: 2011-09-27 11:18:31 Posts: 243
|
I did it! But now I have the obvious problem: What if I want to use the OnAutomationMessage() callback in order to call another custom function? I tried using the SetLuaFloaterCommand to also name a global variable automationpurpose = "share" and then have the OnAutomationMessage() check that first… but it silently failed.
|
2025-05-08 22:52:57 |
|
 |
Henri Beauchamp
Joined: 2009-03-17 18:42:51 Posts: 5998
|
What string you pass with AutomationMessage() is your decision and your own convention. For example, pass "share_inventory", and then, when receiving it in OnAutomationMessage(), execute ShareInventory(GetLuaFloaterValues("inventoryext","inventory1")[1]). However your code is wrong: SetLuaFloaterCommand() should be called from OnLuaFloaterOpen(), not from OnLuaFloaterAction(), and if you use the latter, then you do not need at all SetLuaFloaterCommand(), neither OnAutomationMessage()You could for example use: In which case you do not even need to set a command for button2 and do not need at all OnAutomationMessage() or AutomationMessage() since the OnLuaFloaterAction() is executed within the context (the "Lua state") of the automation script, in which your ShareInventory() function is itself defined and thus known...
|
2025-05-09 08:19:11 |
|
 |
g0rd0ngrfr33mailgr
Joined: 2011-09-27 11:18:31 Posts: 243
|
Thank you for the trick! I'm not sure I understand the distinction between the two callbacks. I understand the form (Action has control and value, while Open has parameter), but I don't get the semantics. I thought Action is to be used when you interact with the floater while Open is only when it first opens, but apparently not.  |  |  |  | Quote: and if you use the latter, then you do not need at all SetLuaFloaterCommand(), neither OnAutomationMessage(). You could for example use: In which case you do not even need to set a command for button2 and do not need at all OnAutomationMessage() or AutomationMessage() since the OnLuaFloaterAction() is executed within the context (the "Lua state") of the automation script, in which your ShareInventory() function is itself defined and thus known... |  |  |  |  |
I'll try that. I also don't understand why Action is executed within the context and Open is not. Because you made them that way, but what is the difference that I fail to see?
|
2025-05-09 11:28:33 |
|
|
Who is online |
Users browsing this forum: No registered users and 3 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
|
|