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

Naming created text objects

1590100905
Joshua S.
Pro
Sheet Author
API Scripter
Basic question: Is it possible to add a 'name' property to a text object? Long Explanation: I am developing API scripts for a game that uses a playmat for each character. There are certain conditions that they keep track of on their character sheet and each character has a playmat on a sheet called "Playmats". If I want to drop a token onto their playmat (or have a script do it), that is easy so long as the token and playmat graphics objects follow consistent naming conventions. I am trying to have a textbox that will update with information from the character's conditions. Basically, I want the script to do something like this:     When Bob updates his Rage condition on his character sheet, register the changed attribute     Find the textbox that corresponds to Bob's Rage on his playmat     Update that textbox with the new information     If there is no textbox corresponding to Bob's Rage on his playmat, create one with the proper information I can't figure out how to create a textbox that I can find and update later on, all of the fields are non-unique (except ID, and I don't have any way to predict what ID will be assigned to the object). How can I specify that this text object is Bob's Rage and not Sam's Rage or Bob's Shroud? Or is there some other way to solve this that I am totally missing?
1590101550
The Aaron
Roll20 Production Team
API Scripter
There's a few ways you could maintain that association. The top couple that come to mind are: 1) create the association in the state object, character is -> text id  2) create an attribute for the character that has the id of the text object in it 3) write it in the gmnotes of the character 4) create a text object on the gm layer which the character id -> text id  5) store it in the text's controlledby. The ids aren't forced to be anything in particular in that list, so you could put the character's is there. Probably the easiest. 
1590101552
Mik H.
Forum Champion
I was literally just coming on the forums to ask the same question. I am a pretty big beginner to javascript. What I've done is place tokens on the GM layer, named the things I want (so name "bobRage" and "samRage"). In my code, I have a "setup" command that iterates through all graphics on that layer, gets their name, makes an object textList, then setting textList[name] = creating a new text object with that token's coordinates. Then I can do  textList["bobRage"].set("text","Bobs Rage") and it works fine for me. The problem I've run into is persistence - I don't understand the state variable at all. I've created state.Mik.textList and tried to use state.Mik.textList = textList; and instead use  state.Mik.textList["bobRage"].set("text","Bobs Rage") Which works at the start, but not through refreshes of the state. 
1590101800
The Aaron
Roll20 Production Team
API Scripter
Hi Mik! &nbsp;The state can only store basic types, not functions or class instances. What you can do is write the ids of the objects to the state and reconstitute your full objects when the script starts up. I do that in quite a few scripts, I'll try to give an example.&nbsp; Here's some details on using the state object. (There's a warning that describes your specific issue):&nbsp; <a href="https://wiki.roll20.net/API:Objects#state" rel="nofollow">https://wiki.roll20.net/API:Objects#state</a>
1590102052
Mik H.
Forum Champion
Ah, thanks! That makes a lot of sense - it didn't even occur to me that those were class instances. I was like "why is this not just considered a json string." I'm pretty sure I can use the ids like that. (I've already been using TokenMod for about a dozen examples, ahah)
1590102257
The Aaron
Roll20 Production Team
API Scripter
Yup. IIRC the state object gets JSON.stringifiy'd on an interval and stored as a text blob in a Postgres database. You can test is something will persist correctly in the state by stringify'ing and then parse'ing it. =D
1590103309
The Aaron
Roll20 Production Team
API Scripter
A good example of reconstituting non-trivial object structures at startup is the initializeRuntimeData() function in TokenCondition.
1590124728
Joshua S.
Pro
Sheet Author
API Scripter
About half of that went over my head, but I tried storing the "name" in the "controlledby" field, and that seems to be working. It feels...weird and wrong somehow, but it works.
1590215324
The Aaron
Roll20 Production Team
API Scripter
Ah, cool. =D. God you got it working.&nbsp;