Possible to dynamically generate setPopup() commands?

Gauroth
Posts: 22
Joined: Tue Sep 07, 2010 2:44 pm

Possible to dynamically generate setPopup() commands?

Post by Gauroth »

Hi, I'm new to Mudlet and Lua as well and was wondering if it is possible to dynamically generate setPopup() commands?

For example, here is a slightly revised version of the documentation on setPopup() :
Code: [show] | [select all] lua
selectString("Hello", 1)
setPopup("main", {[[send("bye")]], [[send("hi")]]}, {"left-click or right-click and do first item to send bye", "click to send hi"})
and here is an idea of what I had in mind (hopefully it's legible - I know there's several of syntax errors there, but don't judge me, I'm new ;) )
Code: [show] | [select all] lua
testVar = {
    ["bye"] = "left-click or right-click and do first item to send bye",
    ["hi"] = "click to send hi" 
}

selectString("Hello", 1)
setPopup("main", {
    for k, v in pairs(testVar)
        [[send(k)]],
    end
    }, {
    for k, v in pairs(testVar)
        [[v]],
    end
})
Just for kicks I also tried pre-constructing the setPopup() commands into a variable and using that in the setPopup() call, but this failed as well. Echoing out the resulting string looked promising, but putting the variable into the setPopup() call proved fruitless.
Code: [show] | [select all] lua
testVar = {
    ["send(\"bye\")"] = "left-click or right-click and do first item to send bye",
    ["send(\"hi\")"] = "click to send hi"
}

testCons = "{"

for k, v in pairs(testVar) do
    testCons = testCons .. "[[".. k .."]], "
end
testCons = testCons .. "}, {"
for k2, v2 in pairs(testVar) do
    testCons = testCons .. "[[".. v2 .."]], "
end

testCons = testCons .. "}"
display(testCons)

selectString("Hello", 1)
setPopup("test", testCons)
So there are my bumbling newbie attempts at this, hopefully someone can point me in the right direction in order to do this correctly or at least let me know that it isn't possible so I can stop wasting time with it.

Thanks in advance.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Possible to dynamically generate setPopup() commands?

Post by Heiko »

This is easy to do by using the Lua run time compilation and execution function loadstring( myCodeToExecuteString ). This allows you to dynamically build your Lua program on the fly so to speak and then execute it.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Possible to dynamically generate setPopup() commands?

Post by tsuujin »

It should probably be noted that the community in general has indicated several speed issues and garbage collection issues resulting from rapid loadstring commands, though.

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Possible to dynamically generate setPopup() commands?

Post by Vadi »

You're confusing content with actual generation... put the setPopup inside each for loop iteration and make it work with the data you have.

loadstring is not necessary here, and really it has very, very niche uses in the runtime code generation area. This isn't it

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Possible to dynamically generate setPopup() commands?

Post by Heiko »

tsuujin wrote:It should probably be noted that the community in general has indicated several speed issues and garbage collection issues resulting from rapid loadstring commands, though.
This is interesting news to me. Do you have anything to back up this claim. If it's a confirmed bug in Lua any idea when is it going to be fixed?

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Possible to dynamically generate setPopup() commands?

Post by Heiko »

Vadi wrote:You're confusing content with actual generation... put the setPopup inside each for loop iteration and make it work with the data you have.

loadstring is not necessary here, and really it has very, very niche uses in the runtime code generation area. This isn't it
Sorry, Vadi, but this is garbage :) loadstring() is a major general language feature and should be used whenever it's appropriate.

I've used this approach for my former Lusternia auto-bot. Whenever it enters a new room it issues an "info here" command that is subsequently parsed by a trigger chain that linkifies each room object into popup menus for certain actions e.g. add to autohunting list, consider etc..

I've attached an xml file of the respective trigger chain as an example.
Attachments
analyseRoomObjects.xml.xml
(5.98 KiB) Downloaded 407 times
dynoPopup.png

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Possible to dynamically generate setPopup() commands?

Post by Heiko »

Vadi is right, of course, that loadstring() is slow and thus should never be used in code that is relevant for the overall performance. As a rule of thumb, speed relevancy is determined by how often the respective code is being run by your system. It's no problem in the example above because you can't move fast enough for it to matter, but using loadstring() constructs in code that is called often like prompt triggers, general event handlers etc. should be avoided like the plague.

Gauroth
Posts: 22
Joined: Tue Sep 07, 2010 2:44 pm

Re: Possible to dynamically generate setPopup() commands?

Post by Gauroth »

First, thank you all for your attention, it really is appreciated.

Now, after trying unsuccessfully to wrap my head around using loadstring() in this manner I came back and saw Vadi's suggestion to put setPopup() inside my "for" loop. This is what I came up with:
Code: [show] | [select all] lua
testVar = {
    ["bye"] = "left-click or right-click and do first item to send bye",
    ["hi"] = "click to send hi"
}

selectString("Hello", 1)
for k, v in pairs(testVar) do
	setPopup("main", {[[send("]].. k ..[[")]]}, {[[]].. v ..[[]]})
end
This works quite well for the first element of the table, but fails for the second - presumably because the loop is trying to call setPopup() in it's entirety twice? How might I take this principle and make it work for all elements in the table?

Heiko, that example is rather interesting to me and I may wind up "borrowing" a few pieces of code from it if you don't mind, but I don't see an instance of loadstring() in it - and I was really hoping for one to help my poor brain along :)

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Possible to dynamically generate setPopup() commands?

Post by Heiko »

Mudlet is using loadstring() internally to compile and run the code that you provide in setPopup(). My example is to illustrate how to use some of the more advanced features of the trigger engine (Lua function condition gate activated auto closing trigger chain) to do something similar to what you were trying to do with your "dynamically generated popup menus approach. I create the popup menus dynamically from unknown content that is provided by the MUD i.e. whatever room object there is, it'll get linked into the popup menu.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Possible to dynamically generate setPopup() commands?

Post by Heiko »

To answer your question: In a first step you fill the 2 tables that you want to pass to setPopup() and then you call setPopup( mytable1, mytable2 ...). No big problem really.

Post Reply