How do I set specific values in the input() function?

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
User avatar
Shadowfireomega
Giant
Posts: 218
Joined: Thu Dec 23, 2010 12:12 am
Location: San Antonio, Texas

How do I set specific values in the input() function?

Post by Shadowfireomega »

I've been reading through the input() function on the wiki, but I cannot seem to find out how to specify specific values for the choices. An example of what I am trying to do:

Choose one: Weapon1,Weapon2, or Weapon3.
I want the vales for them to be something like:
Weapon1=Wep1, Weapon2=Wep2, Weapon3=Wep3

I think I have seen this somewhere, but I cannot find the macro I saw it in, nor can I find it on the wiki.
"I love being wrong, for if I was always right, there would be nothing left to learn, and that would be a world I would not want to live in."
-Benjamin Fisher
"Knowledge is knowing a tomato is a fruit; wisdom is not putting it in a fruit salad."
-Miles Kington
"Space is his favorite thing in the world!"
-Kayla Kros
"Anything times 1 is 1"
-Kayla Kros

Ditto
Giant
Posts: 179
Joined: Thu Jul 28, 2011 1:06 pm

Re: How do I set specific values in the input() function?

Post by Ditto »

Shadowfireomega wrote:I've been reading through the input() function on the wiki, but I cannot seem to find out how to specify specific values for the choices. An example of what I am trying to do:

Choose one: Weapon1,Weapon2, or Weapon3.
I want the vales for them to be something like:
Weapon1=Wep1, Weapon2=Wep2, Weapon3=Wep3

I think I have seen this somewhere, but I cannot find the macro I saw it in, nor can I find it on the wiki.
You want the LIST option of the input function:

here:
http://lmwcs.com/rptools/wiki/input

an example:

Code: Select all

[h: chk = input ( "weaponchoice | Weapon1, Weapon2, Weapon3 | Weapon | LIST | SELECT=STRING VALUE=NUMBER" )]
Then, to get that to "Wep1", "Wep2", etc., you should probably use a SWITCH statement to map it:

Code: Select all

[h, SWITCH ( weaponchoice ) :
      case "Weapon1": { [mappedchoice = "Wep1" ] };
      case "Weapon2": { [mappedchoice = "Wep2" ] };
      case "Weapon3": { [mappedchoice = "Wep3" ] };
      default: { [mappedchoice = "" ] }]

MeMeMe
Dragon
Posts: 256
Joined: Tue Mar 31, 2009 7:44 pm

Re: How do I set specific values in the input() function?

Post by MeMeMe »

I don't have enough information to know exactly what you're tring to do. It would help to know what the function is for, or to post the code you have already tried.

That said, I know one thing that confused me initially when using inputs was the fact that the value returned is not the selected item, but the selected item's position in the list of items.


For example, here's a simple input:

Code: Select all

<!-- first, create a list of weapons that will be shown to the user -->
[h:weaponList = "Pistol, Shotgun, Assault Rifle"]

<!-- now display that form.
when the user selects a weapon, the variable indexOfWeapon will be set: 
it will equal the weapons position in the list. 
So if the user picks pistol, indexofWeapon = 0; if user pics Assault Rifle, indexOfWeapon = 2 -->

[h:status=input(
"indexOfWeapon|"+weaponList+"|Which Weapon?|RADIO|SELECT=0"
)]
[h:abort(status)]
<!-- 

<!-- now need to use the numerical index to find the name of the weapon chosen. -->
[H: chosenWeaponName = listGet(weaponList,whichWeapon)]
MapTools Forks: Thread

MeMeMe
Dragon
Posts: 256
Joined: Tue Mar 31, 2009 7:44 pm

Re: How do I set specific values in the input() function?

Post by MeMeMe »

Ninja's by Ditto :)

Note that if you use the LIST option

Code: Select all

[h: chk = input ( "weaponchoice | Weapon1, Weapon2, Weapon3 | Weapon | LIST | SELECT=STRING VALUE=NUMBER" )]
You can change the VALUE=NUMBER to VALUE=STRING, and the variable will be saved with the text value of the chosen item. You don't need to then do the "get from list" method I used in my last post. Generally though, I think using that list method I suggest is a bit more robust - it's easier to keep updated with code changes, and make the code portable.
MapTools Forks: Thread

User avatar
Shadowfireomega
Giant
Posts: 218
Joined: Thu Dec 23, 2010 12:12 am
Location: San Antonio, Texas

Re: How do I set specific values in the input() function?

Post by Shadowfireomega »

@Ditto: This will work thanks!
@MeMeMe: This is what I tried:

Code: Select all

[h:input("bang|Hi, low, Medium|How big?|list|value=8, 1, 4")]
[bang]
That is just a quick scratch macro, but in the same format I tried. I abandoned this method for something else for the macro I was making so I don't have the original. But if there is a way to do it with just the input, that would be good to know for the future ^.^
"I love being wrong, for if I was always right, there would be nothing left to learn, and that would be a world I would not want to live in."
-Benjamin Fisher
"Knowledge is knowing a tomato is a fruit; wisdom is not putting it in a fruit salad."
-Miles Kington
"Space is his favorite thing in the world!"
-Kayla Kros
"Anything times 1 is 1"
-Kayla Kros

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: How do I set specific values in the input() function?

Post by aliasmask »

I often will use 2 arrays. The first array is the output I want to show for the selection. The second array is the value I want to use for the selection. They are connected by their position in the array. This is useful to do when I have fancy formatting for my input. Another method is to make it a single json object where the key is what is displayed and the value is what I want to save. In that case, I would want to return the value with VALUE=STRING and then use json.get to get the value.

Code: Select all

<!-- method 1 -->
[H: inputOptions = "dagger,shortsword,longsword"]
[H: inputValues = "1d4,1d6,1d8"]

[H: abort(input(strformat("weaponChoice|%{inputOptions}|Choose a weapon|LIST")))]
[H: result = listGet(inputValues,weaponChoice)]

[H: damage = eval(result)]

[R: strformat("%{result} = %{damage}")] 

Code: Select all

<!-- method 2 -->
[H: inputObj= json.set("{}","<html><b>dagger</b>: 1d4</b></html>","1d4",
   "<html><b>shortsword</b>: 1d6</b></html>","1d6",
   "<html><b>longsword</b>: 1d8</b></html>","1d8"
)]
[H: inputOptions = json.fields(inputObj)]

[H: abort(input(strformat("weaponChoice|%{inputOptions}|Chose a weapon|LIST|VALUE=STRING")))]
[H: result = json.get(inputObj,weaponChoice)]

[H: damage = eval(result)]

[R: strformat("%{result} = %{damage}")] 
edit: fixed code in method 2

Ditto
Giant
Posts: 179
Joined: Thu Jul 28, 2011 1:06 pm

Re: How do I set specific values in the input() function?

Post by Ditto »

MeMeMe wrote: You can change the VALUE=NUMBER to VALUE=STRING, and the variable will be saved with the text value of the chosen item. You don't need to then do the "get from list" method I used in my last post. Generally though, I think using that list method I suggest is a bit more robust - it's easier to keep updated with code changes, and make the code portable.
I thought of that, but didn't see any real benefit ... (see aliasmask's post, though .. having 2 arrays could make it simpler to make them ... ) :) But just showing the principal of how to use the LIST option in input .. I'll stick with my initial example :lol:

Post Reply

Return to “Macros”