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

[Help] State

1369461270
Konrad J.
Pro
API Scripter
I need to integrate State into my script now, need to have a persistent variable. Could someone give me a little help on using State?  I'm not quite sure on its use.  Do you simply use it like this... state.savedvariable = variabletosave; How would you put an existing object into state?  Say something like this... var fwGlobal = {    rotationEnable : true,    moveEnable : true,    sizeEnable : true }; Would you simply do this... state.fwGlobal = fwGlobal; And then to get it back again.... fwGlobal = state.fwGlobal; Thanks for your help!
1369463647
Konrad J.
Pro
API Scripter
I tried this as a test.  No errors, but the "if (fwGlobal in state)" never happens?  I do a "log(JSON.stringify(state))" and it shows fwGlobal in state. var fwGlobal = {     rotationEnable : true,     moveEnable : true,     sizeEnable : true }; on("change:graphic", function(obj, prev) {     fwGlobal.rotationEnable = false;     state.fwGlobal = fwGlobal;     log(state.fwGlobal.rotationEnable); }); on("ready", function() {     if (fwGlobal in state) {         log("hello");         fwGlobal = state.fwGlobal;     } else {         state.fwGlobal = fwGlobal;     }     log(fwGlobal.rotationEnable); });
1369463854
Konrad J.
Pro
API Scripter
on a related subject, I find after some testing that I've put some other variables into state.  How would you reset state back to nothing? :)
1369467257
Alex L.
Pro
Sheet Author
state = {}; DO NOT DO THIS IN A NORMAL SCRIPT EVER!!!!!
1369467506
Konrad J.
Pro
API Scripter
Alex L. said: state = {}; DO NOT DO THIS IN A NORMAL SCRIPT EVER!!!!! Hmm, I tried that and I thought it gave an error.  I'll have to try that again. I see why you should not do it in a script others might use, you would erase objects other scripts might have stored.  So that brings up another question. How would you just delete objects your script created?  Sort of like a cleanup or a reset command.
1369478008
Alex L.
Pro
Sheet Author
Konrad J. said: Alex L. said: state = {}; DO NOT DO THIS IN A NORMAL SCRIPT EVER!!!!! Hmm, I tried that and I thought it gave an error.  I'll have to try that again. I see why you should not do it in a script others might use, you would erase objects other scripts might have stored.  So that brings up another question. How would you just delete objects your script created?  Sort of like a cleanup or a reset command. delete state.myObj; That should work, but you might want to consider namespaceing your state so have state.konrad = {}; then use state.konrad in place of state
1369498942
Konrad J.
Pro
API Scripter
Alex L. said: Konrad J. said: Alex L. said: state = {}; DO NOT DO THIS IN A NORMAL SCRIPT EVER!!!!! Hmm, I tried that and I thought it gave an error.  I'll have to try that again. I see why you should not do it in a script others might use, you would erase objects other scripts might have stored.  So that brings up another question. How would you just delete objects your script created?  Sort of like a cleanup or a reset command. delete state.myObj; That should work, but you might want to consider namespaceing your state so have state.konrad = {}; then use state.konrad in place of state Ya, all my globals are namespaced with fwGlobal, I guess some other script could use that same name possible.  Maybe I'll make it a little more distinct.  Perfect I'll try that delete.  THanks! Any ideas on the code above why "if (fwGlobal in state)" never seems to come out false?  I'm trying to make sure the fwGlobal exists in state, if not then I create it.  It always comes up false so it always recreates it, writing over all the variables with defaults, "state.fwGlobal = fwGlobal".  It should only come up false once, the rest should be true so it can read all the globals from state.  Its gotta be something stupid I'm doing?
1369499670
Konrad J.
Pro
API Scripter
Never mind, its was something stupid.  THe in command works off of a string.  I just need to put in quotes what I was searching for. :) Now I can get back to business!
1369499686
Alex L.
Pro
Sheet Author
Konrad J. said: Alex L. said: Konrad J. said: Alex L. said: state = {}; DO NOT DO THIS IN A NORMAL SCRIPT EVER!!!!! Hmm, I tried that and I thought it gave an error.  I'll have to try that again. I see why you should not do it in a script others might use, you would erase objects other scripts might have stored.  So that brings up another question. How would you just delete objects your script created?  Sort of like a cleanup or a reset command. delete state.myObj; That should work, but you might want to consider namespaceing your state so have state.konrad = {}; then use state.konrad in place of state Ya, all my globals are namespaced with fwGlobal, I guess some other script could use that same name possible.  Maybe I'll make it a little more distinct.  Perfect I'll try that delete.  THanks! Any ideas on the code above why "if (fwGlobal in state)" never seems to come out false?  I'm trying to make sure the fwGlobal exists in state, if not then I create it.  It always comes up false so it always recreates it, writing over all the variables with defaults, "state.fwGlobal = fwGlobal".  It should only come up false once, the rest should be true so it can read all the globals from state.  Its gotta be something stupid I'm doing? should be if ("fwGlobal" in state)
1369499791
Konrad J.
Pro
API Scripter
Alex L. said: Konrad J. said: Alex L. said: Konrad J. said: Alex L. said: state = {}; DO NOT DO THIS IN A NORMAL SCRIPT EVER!!!!! Hmm, I tried that and I thought it gave an error.  I'll have to try that again. I see why you should not do it in a script others might use, you would erase objects other scripts might have stored.  So that brings up another question. How would you just delete objects your script created?  Sort of like a cleanup or a reset command. delete state.myObj; That should work, but you might want to consider namespaceing your state so have state.konrad = {}; then use state.konrad in place of state Ya, all my globals are namespaced with fwGlobal, I guess some other script could use that same name possible.  Maybe I'll make it a little more distinct.  Perfect I'll try that delete.  THanks! Any ideas on the code above why "if (fwGlobal in state)" never seems to come out false?  I'm trying to make sure the fwGlobal exists in state, if not then I create it.  It always comes up false so it always recreates it, writing over all the variables with defaults, "state.fwGlobal = fwGlobal".  It should only come up false once, the rest should be true so it can read all the globals from state.  Its gotta be something stupid I'm doing? should be if ("fwGlobal" in state) Thanks!  I just figured that out before you posted! :)