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

[suggestion] Writable "represents" argument in graphic object

December 16 (11 years ago)

Edited December 16 (11 years ago)
Fabio M.
KS Backer
Hello,

I was working on a script that manages various spell effects, and some of them change one or more attributes on the character sheet that the affected token represents.
While this is not a problem when it happens to a player (that typically has only one character sheet), it is when it gets down to the NPCs, that normally share the same character sheet. In fact, in my Campaigns I normally create one character sheet that is my "archetype" for a particular foe, than I create one token that I link to that sheet (linking the three bars as well), copy it the number of times I need, and lastly I unlink their bars.

The problem in this case is that as soon as I change some attributes of one of the tokens, they change for them all. In order to overcome this, I thought of writing a function that checks whether the affected token shares the character with some other token, and if it does, it simply creates another character, copies the contents of the original one, and then assigns the newly created character to the affected token.

This is of course impossible since the last step requires the "represents" attribute of the token object to be writable.

Could it be rendered writable? Is there a particular reason it is read only? I mean, you can change it using the interface, why not from the API?

Thanks

P.S.

It would also allow some nice "effects" like polymorph, lycantrophy and the like

EDIT: Nevermind, I wrote the script and I realized it changes the "represent" argument. Sorry, then you need only to update the Wiki.
December 16 (11 years ago)
Fabio M.
KS Backer
I actually have some problems with this... The property is writable, but as soon as I try to modify something in the newly created character, it throws out an error. Basically I call a function that, if needed, copies a token's linked character, then assigns the copy to the token, then returns the new character's id.
Whit this id I then simply get an attribute from the new character, add a value to it, and put it back. The last operation gives me this:

/home/symbly/www/d20-api-server/node_modules/firebase/lib/firebase-node.js:1 orts, require, module, __filename, __dirname) { function f(a){throw a;}var j=v ^ Error: Firebase.child failed: First argument must be a non-empty string and can't contain ".", "#", "$", "[", or "]". at Error () at Ha (/home/symbly/www/d20-api-server/node_modules/firebase/lib/firebase-node.js:12:204) at G.W.H (/home/symbly/www/d20-api-server/node_modules/firebase/lib/firebase-node.js:126:213) at TrackedObj._doSave (

The value I'm trying to insert is a number.

Here is the function and the few lines of code executed after I use it:

Gist

December 16 (11 years ago)
Gauss
Forum Champion
Moved to the API forum.
December 17 (11 years ago)
Fabio M.
KS Backer
@Gauss
Thanks for moving it.

I did some other tests. It has something to do with the fact the character object has just been created... If I execute the code on the old character it all works. As soon as I create the new one, everything works up to the point where I get the attribute value and add some number to it:

var attrib = parseInt(oAttrib.get('current')); attrib += effectValue;
attrib += effectValue;

attrib is definitely a number, before and after the addition, and when I try to put it back into the Attribute, it fires up the error I pasted above. Can somebody please try to replicate it to see whether it is a bug?
December 17 (11 years ago)
Fabio M.
KS Backer
@Gauss

Maybe change the title to [bug] instead of suggestion? I'm not sure it is, but it could better get the attention of some devs...
Thanks!
February 01 (11 years ago)
Chad
Sheet Author
I'm having the same problem. Near as I can figure, a newly created object doesn't actually "exist" immediately. Anyone find a fix for this yet?
February 03 (11 years ago)

Edited February 03 (11 years ago)
Fabio M.
KS Backer
Tried just now creating a graphic object. As soon as I try to do something with it after its creation (change any property) the usual error is thrown. It takes a sandbox restart to have it available, so it is definitely a bug, and it is not limited to the "represents" property.
February 03 (11 years ago)

Edited February 03 (11 years ago)
Alex L.
Pro
Sheet Author

Fabio M. said:

Tried just now creating a graphic object. As soon as I try to do something with it after its creation (change any property) the usual error is thrown. It takes a sandbox restart to have it available, so it is definitely a bug, and it is not limited to the "represents" property.


I would guess its a simple asynchronous system, your script tells the API to create the object so it does but the object is being created simultaneously in the background.

The correct way to handle object creation in an asynchronous enviroment would be to listen to the object created event.
February 03 (11 years ago)

Edited February 03 (11 years ago)
Fabio M.
KS Backer
Yep, I thought so, but the error given in that case would be misleading. I would expect the object to be undefined. And I didn't find anything to listen to the creation of an object. The closest thing is the event add:graphic but it doesn't seem to work with created objects.

By the way, the wiki says "createObj will return the new object, so you can continue working with it.", so it should allow you to do something with it straight away.

Edit: Stupid me, events are not triggered by scripts, that's why add:graphic doesn't catch it.
February 05 (11 years ago)
Alex L.
Pro
Sheet Author

Fabio M. said:

Yep, I thought so, but the error given in that case would be misleading. I would expect the object to be undefined. And I didn't find anything to listen to the creation of an object. The closest thing is the event add:graphic but it doesn't seem to work with created objects.

By the way, the wiki says "createObj will return the new object, so you can continue working with it.", so it should allow you to do something with it straight away.

Edit: Stupid me, events are not triggered by scripts, that's why add:graphic doesn't catch it.


the best way to deal with it would be get create the object then get it, the id of the object should be in the object that createObj passed back (ie obj.id).
February 05 (11 years ago)
Fabio M.
KS Backer
It is exactly what I'm doing (look at the code pasted above) and it doesn't work.
If you create a graphic object the error is the same. This is the code I used to create a token for my tests:

var newToken = createObj("graphic", {
name: "newtoken",
imgsrc: "https://s3.amazonaws.com/files.d20.io/images/2569788/3wMeuzlwq_yLodLGffZXJg/thumb.gif?13878626195",
width: 70,
height: 70,
left: -280,
top: -280,
pageid: Campaign().get("playerpageid"),
layer: "objects"
});

Then I tried to modify something with the following (for example its position):

newToken.set({
'left': 300,
'top': 300,
});

and it crashes with the error pasted above. So it seems it happens at least for journal and graphic objects (I didn't try the other types, yet).


February 19 (11 years ago)
Fabio M.
KS Backer
Any news on this?
March 29 (11 years ago)
I'm hitting this same problem, but with Attributes that I've created on Character objects. I've tried using the object id to re-fetch the object, rather than using the instance returned by createOjb(), but that doesn't help either.
March 29 (11 years ago)
The key to using multiple tokens that represent the same character sheet is to NOT link the bars on the token and then set that token (with the unlinked bars) as the default token for the character sheet. Then, every time you drag out a token onto the map with that character sheet... the bars will not be linked, but it will have the correct stats you set for it.
March 29 (11 years ago)
I have no issues changing properties on a newly created object in my Monster / Character Importer scripts as shown in the snippet below...

var Character = createObj("character", {
	avatar: Token.get("imgsrc"),
	name: MonsterName,
	gmnotes: Token.get("gmnotes"),
	archived: false
});

// GET LEVEL, ROLE, & XP
var Level = parseInt(StatBlock.match(/<Level>(.*?)<\/Level>/g)[0].match(/\d+/g)[0]);
var Role = StatBlock.match(/<Role(.*?)<\/Role>/g)[0].match(/<Name>(.*?)<\/Name>/g)[0].split(">")[1].split("<")[0];
var XP = parseInt(StatBlock.match(/<Experience FinalValue(.*?)'>/g)[0].match(/\d+/g)[0]);
AddAttribute("Level", Level, Character.id);
AddAttribute("Role", Role, Character.id);
AddAttribute("XP", XP, Character.id);
March 30 (11 years ago)
Hey HoneyBadger, your code example doesn't show changing properties on any newly created objects. After creating the Character instance, you aren't calling the Character.set() method (which is what is causing the issue for us). You seem to be calling a custom made AddAttribute function, and although I don't know the contents of that function, I'm pretty sure it wouldn't trigger the bug we're encountering. Attributes are also top level objects, they merely reference carry a foreign key to reference a character.
March 30 (11 years ago)
It's just a createObject functioned wrapped in my own function. I'll add a .set to my script and see what happens.
March 30 (11 years ago)
FYI: I've found a workaround (though quite hacky) and described it here: https://app.roll20.net/forum/post/733277/api-firebase-dot-child-failed-error-help-please/#post-745926