Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

API method for user inputs

1723424982

Edited 1723425087
Hello, I'm currently working on a number of scripts, the short description of them would be to create and modify objects. Is there a way to prompt the user for text input via the API and additionally a prompt for a selection from a drop-down list? Basically I'm trying to use a text input prompt to allow users who call the script to enter a name for the created object. Something along the lines of createObj("graphic",{name: getString()});  for the dropdown, I'm trying to populate a list of objects of a particular type, and use it to create a parent-child relationship for the created object. for example, to create a card for a deck, something like: var deck = dropdown(findObjs({_type:"deck"})); //in theory call a dropdown list function, populated by an array of decks by findObjs. Should return the selected object to the variable. createObj("card",{deckid=deck.id}); An alternative use case for the dropdown would be to assign the created object a controller from a dropdown list of players. If any of that makes sense. Is this something possible somehow?
1723429539
GiGs
Pro
Sheet Author
API Scripter
Mod scripts can only accept string inputs. But you can create dropdown queries in the macro that launches them, then turn into input into an arguments object. For instance you could have a macro, like !my_script --name:?{Enter name:} --card|?{Choose a card|card1|card2|card3} Then in your script, you check check that the input is for your script, and create an arguments array which includes the values people chose. if ( 'api' === msg . type && msg . content . toLowerCase (). startsWith ( '!my_script' )) { let args = msg . content . split ( "--" ); Then you might process that array to extract the name and card variables or test that they exist. However, you can't do this: in theory call a dropdown list function, populated by an array of decks by findObjs. You can't present a query to the user after that first line of the script, and can't use findObjs to populate the query shown to the user. You'd need to manually create the card deck in the calling macro (as I have done with card1, card2, and card3 above).
I appreciate the response and explanation. I'll give that method a shot. If I'm understanding the explanation correctly, I think I can make it work for the functionality I'm looking for. All things considered, the prompt doesn't necessarily need to run in the middle of the script, so if I can use a macro to pass arguments as the script is invoked, I could just store those values in a variable and use them later on, when they make sense. Although not being able to populate a dropdown list dynamically is unfortunate, but it's what we've got. Thanks again
1723432777
The Aaron
Roll20 Production Team
API Scripter
You can prompt in the middle of a run, you just need to set your code up so you can resume after a given command is received. Then as you're processing, you issue a chat message with a button that has the prompt built into it. 
1723433413

Edited 1724282953
GiGs
Pro
Sheet Author
API Scripter
After I wrote that reply, I realised you could prompt in the middle. The method I'm thinking of is scripting a fake roll, and collecting the input, without sending anything to chat. As Aaron suggests (and he's the expert here!), you do have to build your code in a different way. It's not straightforward, and very few scripts do it so it's not as easy to find examples to copy / learn from. 
I've done this before using Scriptcards and its reentry feature.  
GiGs said: Mod scripts can only accept string inputs. But you can create dropdown queries in the macro that launches them, then turn into input into an arguments object. For instance you could have a macro, like !my_script --name:?{Enter name:} --card|?{Choose a card|card1|card2|card3} Then in your script, you check check that the input is for your script, and create an arguments array which includes the values people chose. if ( 'api' === msg . type && msg . content . toLowerCase (). startsWith ( '!my_script' )) { let args = msg . content . split ( "--" ); Then you might process that array to extract the name and card variables or test that they exist. However, you can't do this: in theory call a dropdown list function, populated by an array of decks by findObjs. You can't present a query to the user after that first line of the script, and can't use findObjs to populate the query shown to the user. You'd need to manually create the card deck in the calling macro (as I have done with card1, card2, and card3 above). I tried this out, and so far it's worked very well. I can probably get what I'm looking for by really pushing this method. I appreciate all the other input too, and any more that trickles in. It's always nice to have more tools in the tool belt.