Dialog box of checks from a list, returning the list of only those checked.

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
Shadow Wizard
Giant
Posts: 183
Joined: Mon Apr 11, 2011 8:11 pm

Dialog box of checks from a list, returning the list of only those checked.

Post by Shadow Wizard »

Wow, this is completely over my head.. So I am gonna need a lot of help writing this one. So far I have:

Code: Select all

[h:allplayers=getAllPlayerNames()]
[h:selectedplayers=getLibProperty("Data3", "lib:Wizard")]
Yea.. So here is what I need to do. I need to display a dialog box, that hast a list of each item in the "allplayers"
It will then automatically check each player that is in the "selectedplayers" list.
I will then check, and uncheck as I please.
It then needs to return a list off all the checked players from that dialog box.
And I don't know how to do any of it...
Yea, I know, I am asking a lot, but once I see/am able to make the code for this, it opens op a whole slew of other possibilities for me...

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

Re: Dialog box of checks from a list, returning the list of only those checked.

Post by aliasmask »

Do you mean all the PC tokens. all the player are the people who are logged in. Or is there some correlation between the 2 you want.

You'll need to loop through your list of whatever to build your input list for the dialog, then after the input loop through again making a new list of the selected. Here's a generic example:

Code: Select all

[H: playerNames = getAllPlayerNames("json")]

[H: checkboxTemplate = "player%{index}|1|%{playerName}|CHECK"]

[H: input.list = "[]"]
[H, foreach(playerName,playerNames), code: {
   [H: index = roll.count]
   [H: input.list = json.append(input.list,strformat(checkboxTemplate))]
}]

[H: abort(input(json.toList(input.list,"##")))]

[H: select.list = "[]"]
[H, foreach(playerName,playerNames), code: {
   [H: index = roll.count]
   [H: selected = eval("player"+index)] 
   [H, if(selected): select.list = json.append(select.list,playerName)]
}]

[R: select.list]

Shadow Wizard
Giant
Posts: 183
Joined: Mon Apr 11, 2011 8:11 pm

Re: Dialog box of checks from a list, returning the list of only those checked.

Post by Shadow Wizard »

So first off, to clarify, we are working with players, physically connected computers. Yes, I know, may be confusing as I said we used just one TV in the middle of a table before, but we are evolving, giving each player there own tablet as well.. Allows for multiple people to input data at the same time (More then 1 player can take damage at the same time without needing to wait for each player to finish with the one computer) But anyway..

Wow, this is a bit mind boggling... But I think I may have kind of figured 1/2 of it out..
In the first foreach loop, it automatically selects the checkbox of every player. What we need to do is only select if the player is in the "selectedplayers" list in my previous example.
This I need to add: (please note the ???? indicates I am not 100% sure what variable to put in there)
[h, if((listContains(selectedplayers, playerName????) >=1 ), CODE:{
shouldicheckthebox=1
};{
shouldicheckthebox=0
}]
and then the 1 in this line:
[H: checkboxTemplate = "player%{index}|1|%{playerName}|CHECK"]
needs to be replaced with the variable shouldicheckthis in each loop.

Secondly, the second loop has an error in it that I think I know what it is.. But can squash the bug. One of the players names has a space in it...
Finally.. Since we will be storing the selectedplayers from Data3 on lib;Wizard in what seems to be a json format, will listcomtains work? Or do I need a json equivalent?
I know I am almost asking you to write this code for me, but I haven't not worked with json lists before, and am starting to see the power of them, so I would really like to learn more.

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

Re: Dialog box of checks from a list, returning the list of only those checked.

Post by aliasmask »

Something like this?

Code: Select all

[h:allplayers=getAllPlayerNames()]
[h:selectedplayers=getLibProperty("Data3", "lib:Wizard")]


[H: playerNames = getAllPlayerNames("json")]
<!-- not good practice to manually put lib name, especially if both macros are on same lib 
   You either don't need the lib name or put getMacroLocation().-->
[H: selectedPlayers = getLibProperty("Data3","lib:Wizard")] 

[H: checkboxTemplate = "player%{index}|%{isSelected}|%{playerName}|CHECK"]

[H: input.list = "[]"]
[H, foreach(playerName,playerNames), code: {
   [H: index = roll.count]
   [H: isSelected = json.contains(selectedPlayers,playerName)]
   [H: input.list = json.append(input.list,strformat(checkboxTemplate))]
}]

[H: abort(input(json.toList(input.list,"##")))]

[H: select.list = "[]"]
[H, foreach(playerName,playerNames), code: {
   [H: index = roll.count]
   [H: selected = eval("player"+index)]
   [H, if(selected): select.list = json.append(select.list,playerName)]
}]

[R: select.list]
This will autoselect only tokens in the Data3 property, but allow you to select other player names as well. The second loop was fixed. I do it this way so the first input doesn't just have to be values and things like spaces in names won't matter (here at least). You could add fancy html to make it look better for example.

If you need to convert a list to json, use json.fromList() and a json to list json.toList.

Shadow Wizard
Giant
Posts: 183
Joined: Mon Apr 11, 2011 8:11 pm

Re: Dialog box of checks from a list, returning the list of only those checked.

Post by Shadow Wizard »

Yes, this seems to work perfectly, thank you very much.
I can work with this to get what I need done, and can use it as reference in the future.
When I examine the code closely, I may have some questions about how specific things work, and I shall just post them here.

Shadow Wizard
Giant
Posts: 183
Joined: Mon Apr 11, 2011 8:11 pm

Re: Dialog box of checks from a list, returning the list of only those checked.

Post by Shadow Wizard »

I have managed to take this and add pieces of it to several other macros! Thank you. Now in doing so, I have one question. In this line:
[H: abort(input(json.toList(input.list,"##")))]
According to what I am reading in the docs, you are setting the Delimiter to ##. However when I just post the output so I can see it, I don't see ## in there anywhere.. Is ## a special character of some sort? or am I missing something?

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

Re: Dialog box of checks from a list, returning the list of only those checked.

Post by aliasmask »

There are 2 ways to put multiple entries for input. One way is

Code: Select all

input("one|1|A number|label","two|2|a number|label")
The other is

Code: Select all

input("one|1|A number|label ## two|2|a number|label")

Post Reply

Return to “Macros”