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 .
×
Our team is currently investigating rolling service outages. For more information, visit our most recent Forum Announcements post.
Create a free account

[Script] - Help with query on Dev instance

Hello all! I know I'm not the most experienced when it comes to API and JavaScript, in general, but I am not sure how to interrpuret these results: --------------------------------------------------------------------------------------------------------------------------------- Script on('chat:message', function(msg) { log("-----BEGIN-----"); if (state.runCount === undefined){state.runCount = 0}; log("run count = " + state.runCount); state.gmnoteTest = [""]; if (msg.type == 'api' && msg.content.indexOf('!CharLoad') !== -1) { var testSheet = findObjs({ name: "Test Character", _type: "character" }); log("calling arrayAssign where testSheet ="); log(testSheet); state.gmnoteTest = arrayAssign(testSheet); log("state.gmnoteTest is now = "); log(state.gmnoteTest); } log("run count = " + state.runCount); log("------END------"); }); function arrayAssign(sheet) { log("Entering array assign"); sheet[0].get("gmnotes", function(gmnotes) { var newArray = gmnotes.split("<br>"); log("inside array assign and newArray = "); for (var i = 0; i < newArray.length; i++) { log(newArray[i]); } state.runCount++; log("exit array assign"); return newArray; }); //need to work with the split array inside of the function it seems //or use states } --------------------------------------------------------------------------------------------------------------------------------- Output "-----BEGIN-----" "run count = 15" "calling arrayAssign where testSheet =" [{"name":"Test Character","bio":"","gmnotes":1400026514992,"archived":false,"inplayerjournals":"","controlledby":"","_id":"-JLnQi_Lj_W_GNCTcVbK","_type":"character","avatar":""}] "Entering array assign" "state.gmnoteTest is now = " undefined "run count = 15" "------END------" "inside array assign and newArray = " "line 1" "line 2" "line 3" "exit array assign" "-----BEGIN-----" "run count = 16" "calling arrayAssign where testSheet =" [{"name":"Test Character","bio":"","gmnotes":1400026514992,"archived":false,"inplayerjournals":"","controlledby":"","_id":"-JLnQi_Lj_W_GNCTcVbK","_type":"character","avatar":""}] "Entering array assign" "inside array assign and newArray = " "line 1" "line 2" "line 3" "exit array assign" "state.gmnoteTest is now = " undefined "run count = 17" "------END------" "-----BEGIN-----" "run count = 17" "calling arrayAssign where testSheet =" [{"name":"Test Character","bio":"","gmnotes":1400026514992,"archived":false,"inplayerjournals":"","controlledby":"","_id":"-JLnQi_Lj_W_GNCTcVbK","_type":"character","avatar":""}] "Entering array assign" "inside array assign and newArray = " "line 1" "line 2" "line 3" "exit array assign" "state.gmnoteTest is now = " undefined "run count = 18" "------END------" --------------------------------------------------------------------------------------------------------------------------------- The intent here is to make sure I know how I am going to work with the gmnotes on the character sheet for my project to load data from the gmnotes to the Character Sheet. This should be possible with the new function available on dev to read the gmnotes from the sheet. The first problem I have, and you will notice that some logs with arrays are on two lines, is that if you include an array in a log, you don't get the proper output. A minor issue resolved by putting the log of the array on a separate line. The second problem, you will see in run 15 that sometimes my order of operations seems to be out of whack. In the example output, I simply did three consecutive runs. Only the last two appear to process the desired order correctly. Generally, this only happens a couple times after a new save, but it's not 100% predictable. Third, I cannot seem to update the state.gmnoteTest array with the called function's newArray value, even though the newArray is working as I intend, show my the looped output inside the function. I would greatly appreciate any insight or direction the community or Roll20 staff might have. Thanks!
1400089912
Lithl
Pro
Sheet Author
API Scripter
I believe setting the gmnotes property is an asynchronous operation. This would explain your output order problem (there are no guarantees about when the callback function will be executed), as well as why gmnoteTest isn't being assigned properly (arrayAssign is returning long before the callback function is called). The only solution is that anything which must occur after arrayAsign (including setting the value of gmnoteTest) will have to be performed within the callback function.
Thanks Brian. That was the same conclusion I was coming to as well regarding the timing. I can work that way if I have to, I just needed to be certain it wasn't my fault or ignorance causing the issue.