List selection help

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
dantarith
Cave Troll
Posts: 62
Joined: Wed Jun 01, 2011 6:09 am

List selection help

Post by dantarith »

As you can I'm tell new to this macro/coding thing (but I'm having a blast).
I have what I think is a simple question. I looked but could not find a tutorial for how to use the SELECT option in the list command with textlist.

On my charsheet macro I have a drop down selection list for race. It works fine but is there a way to select the characters race for the SELECT option if the Race variable has previously been selected?

I would like to open the macro to edit other things on the sheet but I have to reselect the race every time otherwise it goes to the 0 selection. It's not really a big deal but I thought I would ask you coding guru's.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: List selection help

Post by wolph42 »

there are many examples in the wiki: Wiki: input()

but here's what you're looking for:

Code: Select all

"targetNum|put,the,list,here|Select Target|LIST|SELECT=3", 
which will select the 4th item in the list.

now you probly want the '3' as a variable (last choice you mentioned). Last choice, means that the 'lastChosenRace' variable needs to be stored somewhere. Typically you do this on a lib:Token.

So create a token which is called 'lib:anythingYouLike'. Now you create

Code: Select all

"race|"+raceList+"|Select Target|LIST|SELECT="+lastChosenRace, 
in your input

Then before the input you need to defined 'lastChosenRace'
Which will be

Code: Select all

[h:lastChosenRace = getLibProperty("lastChosenRace", "lib:anythingYouLike")] 
I hope you'll understand that this will go wrong the first time as that property will be empty so you need to add:

Code: Select all

[h:lastChosenRace = getLibProperty("lastChosenRace", "lib:anythingYouLike")]
[h,if(lastChosenRace==""): lastChosenRace = 0] 
and after the input you'll need:

Code: Select all

[h:setLibProperty("lastChosenRace", lastChosenRace, "lib:anythingYouLike")] 
o and one more thing, with this kind of code it always goes wrong between the returned value or the selected one. input by default returns a number (e.g. '3' meaning 4th item in the list) but in your character sheet you like to see 'elf' not '3'. hence you can use 'VALUE=STRING' option in the input(). BUT then 'elf' is returned and when you store that in lib:anythingYouLike then 'elf' will also be returned... in which case you get 'SELECT=ELF' which will bug out. Sou you will need listFind to solve that.

dantarith
Cave Troll
Posts: 62
Joined: Wed Jun 01, 2011 6:09 am

Re: List selection help

Post by dantarith »

Thanks for the help!

I got it working. it's a bit more code than I anticipated but since I already have a variable on the "Basic" token named "Race" this is how I got it working...

Code: Select all

[h:RaceSelected = getProperty("Race")]
[h:RaceNum = 0]

<!-- Assign RacNum for SELECT option -->
[r, if(RaceSelected=="Dragonborn"), CODE: { [h:RaceNum=0] };{}]
[r, if(RaceSelected=="Dwarf"), CODE: { [h:RaceNum=1] };{}]
[r, if(RaceSelected=="Elf"), CODE: { [h:RaceNum=2] };{}]
[r, if(RaceSelected=="Gnome"), CODE: { [h:RaceNum=3] };{}]
[r, if(RaceSelected=="Half-Elf"), CODE: { [h:RaceNum=4] };{}]
[r, if(RaceSelected=="Half-Orc"), CODE: { [h:RaceNum=5] };{}]
[r, if(RaceSelected=="Halfling"), CODE: { [h:RaceNum=6] };{}]
[r, if(RaceSelected=="Human"), CODE: { [h:RaceNum=7] };{}]
[r, if(RaceSelected=="Tiefling"), CODE: { [h:RaceNum=8] };{}]

<!-- The Selection List -->
[h:status=input(
"InputRace|Dragonborn, Dwarf, Elf, Gnome, Half-Elf, Half-Orc, Halfling, Human, Tiefling|Race|LIST|SELECT="+RaceNum)]
[h:abort(status)]

<!-- Saves the new selection as Text --> 
[r, if(InputRace==0), CODE: { [h:Race="Dragonborn" ] };{}]
[r, if(InputRace==1), CODE: { [h:Race="Dwarf" ] };{}]
[r, if(InputRace==2), CODE: { [h:Race="Elf" ] };{}]
[r, if(InputRace==3), CODE: { [h:Race="Gnome" ] };{}]
[r, if(InputRace==4), CODE: { [h:Race="Half-Elf" ] };{}]
[r, if(InputRace==5), CODE: { [h:Race="Half-Orc" ] };{}]
[r, if(InputRace==6), CODE: { [h:Race="Halfling" ] };{}]
[r, if(InputRace==7), CODE: { [h:Race="Human" ] };{}]
[r, if(InputRace==8), CODE: { [h:Race="Tiefling" ] };{}]
I set the default property of "Race" to "Human" to avoid the first time blank "" problem.
So, does anyone think there would be any problems with this code?

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: List selection help

Post by wolph42 »

No no problems, but extrmely redundant. You don't need the code option and you can stap all the Ifs for a
[switch:]

But much better ad faster is using the listfind as explained in my earlier post.

Also Now that you assignment it thThe current token only instead of a lib token... What exactly is the use of is?

dantarith
Cave Troll
Posts: 62
Joined: Wed Jun 01, 2011 6:09 am

Re: List selection help

Post by dantarith »

Ok I think I got what you said working. Well it works for me anyway. lol
Here is what I did...

Code: Select all

<!-- Get the tokens exsisting Race -->
[h:RaceSelected = getProperty("Race")]

<!-- Change the selected race to a list number so can use SELECT on InputRace --> 
[h:RaceNum = listFind("Dragonborn, Dwarf, Elf, Gnome, Half-Elf, Half-Orc, Halfling, Human, Tiefling",RaceSelected)]

<!-- The selection list -->
[h:status=input(
"InputRace|Dragonborn, Dwarf, Elf, Gnome, Half-Elf, Half-Orc, Halfling, Human, Tiefling|Race|LIST|SELECT="+RaceNum)]
[h:abort(status)]

<!-- Converts selected list number to text and saves it as the new Race -->
[h,switch(InputRace) :
case "0": Race="Dragonborn";
case "1": Race="Dwarf";
case "2": Race="Elf";
case "3": Race="Gnome";
case "4": Race="Half-Elf";
case "5": Race="Half-Orc";
case "6": Race="Halfling";
case "7": Race="Human";
case "8": Race="Tiefling"]

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: List selection help

Post by CoveredInFish »

You might learn something from this ...

Code: Select all

[h: races = "Dragonborn, Dwarf, Elf, Gnome, Half-Elf, Half-Orc, Halfling, Human, Tiefling"]

<!-- The selection list -->
[h:status=input(
"InputRace|"+races+"|Race|LIST|SELECT="+RaceNum)]
[h:abort(status)]

<!-- Converts selected list number to text and saves it as the new Race -->
[h: Race = listGet(races, InputRace)]
You dont have to ... but maybe you want. IMHO in a macro (only) this complex every working code is good enough.

Post Reply

Return to “Macros”