javascript + Pattr question – storing multiple values of mixed types (string/float)

Dec 7, 2007 at 4:58pm

javascript + Pattr question – storing multiple values of mixed types (string/float)

Hello

So i am in the process of pattrfying one of my javascripts. I have implemented setvalueof and getvalueof, and within my patch my pattrstorage instance properly sees multiple variables.

My data within the client window is such:

“float 0 1 0 Var1 10 0″

(gui mode, minval, maxval, currentvalue, currenttitle, listsize, listtype for those interested).

Now, those variables are global to my JS, and I am attempting to store them within an array – which seems to work at least for SETTING, however recalling does some weird things:

(I am setting an array called currentPattr to hold my ‘multiple’ values – I call pattrUpdate rather than notifyclients within my code to update this currentPattr array, which then calls notifyclients itself)

[code]
// pattr compatibility
function pattrUpdate()
{
// set our array to current global state - have to use an array for pattr since we can only get/set one variable.
currentPattr[0] = mode;
currentPattr[1] = minval;
currentPattr[2] = maxval;
currentPattr[3] = currentval;
currentPattr[4] = currenttitle;
currentPattr[5] = listsize;
currentPattr[6] = listmode;
// currentPattr[7] = menuitems;
// post(currentPattr);

// do our actual pattr updates
notifyclients();
}

function getvalueof()
{
post("storing pattr state");
post();
return currentPattr; // our aggregate array of pattr variables
pattrUpdate();
}

function setvalueof(v)
{
post("recalling pattr state");
post();
currentPattr = v;

// set our values
mode = currentPattr[0];
minval = currentPattr[1];
maxval = currentPattr[2];
currentval = currentPattr[3];
currenttitle = currentPattr[4];
listsize = currentPattr[5];
listmode = currentPattr[6];
// menuitems = currentPattr[7];

pattrUpdate();
resize();
debug();
}

// debug
function debug()
{
post("mode = " + mode);
post();
post("min = " + minval);
post(" max = " + maxval);
post();
}
[/code]

as far as I can tell, setting works (as far as I understand, using getvalueof results in proper values in my pattrstorage window (otherwise, why would I see the full values I see?)),

using my setvalueof to restore client state results in debug printing for example

recalling pattr state
mode = f
min = l max = o
storing pattr state

digging further I’ve iterated over my recalled currentPattr from getvalueof() and note it does not contain all the data I had expected

So, how do I properly pack in the values of mixed string/floats to pattr and recall them?

Do I need to use multidimensional arrays or something?

Forgive me if this is obvious, I feel like I am overlooking something staring me in the face.

Thanks again!

#34925
Dec 7, 2007 at 5:24pm

Better even would be if we could define multiple attributes inside javascript and access them separately with pattr..

Mattijs

#118458
Dec 7, 2007 at 5:31pm

Yeah, totally, I was initially quite confused by the functions, as I did not see how to do exactly that, and then noticed the mention of arrays, and deduced one had to manage this oneself.

#118459
Dec 7, 2007 at 5:59pm

#118460
Dec 7, 2007 at 8:54pm

Hi Emmanuel – thanks for taking the time to look at that

however, there seems to be an issue with your code (the same issue as mine) Here is a modified version of your code:

var currentPattr = new Array();
var mode = “float”
var minval = 0.0;
var maxval = 10.0;
var currentval = 0.0;
var currenttitle = “SOMETITLE”;
var listsize = “10″;
var listmode = “int”;

function pattrUpdate()
{
// set our array to current global state – have to use an array for
currentPattr[0] = mode;
currentPattr[1] = minval;
currentPattr[2] = maxval;
currentPattr[3] = currentval;
currentPattr[4] = currenttitle;
currentPattr[5] = listsize;
currentPattr[6] = listmode;
}
pattrUpdate.local = 1;

function getvalueof()
{
post(“storing pattr state”);
post();
pattrUpdate(); // update the states
return currentPattr; // our aggregate array of pattr variables
}

function setvalueof(v)
{
post(“recalling pattr state”);
post();

// set our values
mode = v[0];
minval = v[1];
maxval = v[2];
currentval = v[3];
currenttitle = v[4];
listsize = v[5];
listmode = v[6];
// menuitems = v[7];

//resize();
debug();
}

// debug
function debug()
{
post(“mode = ” + mode);
post();
post(“min = ” + minval);
post(” max = ” + maxval);
post();
}

// just to test:
function setmode(s)
{
mode = s; // store your new mode

notifyclients(); // update the pattr values
}

running that code code:

step one:
send message “setmode list” to our js
send message “store 1″ to our pattrstorage

note that the clientwindow for pattrstorage properly shows “list” in the data window along side our other data we specified as default in the code

send message “setmode int” to our js
send message “store 2″ to store this second set of data. Both client and storage window reflect these settings.

Now recall setting 1.

Note that debug now outputs :
recalling pattr state
mode = l
min = i max = s
storing pattr state

#118461
Dec 7, 2007 at 8:56pm

I hate this web forum so much.

It cut off my message : here is the rest

which clearly shows us overwriting our array indices with new values (v[0] does not contain “list” it contains “l”) This seems obvious now, but what/where/why is the clientwindow/storagewindow getting its display from? Are we expanding the array somehow?

So I suppose we have to use multidimensional arrays, but I cannot seem to get that to work either

Thanks again for your help.

BTW – I tried this with notifyclients in various places and did not see a difference. Why does notifyclients care what function its in, as long as it is being called when values are changing and pattr needs to be informed?

Thanks you sir!

#118462
Dec 7, 2007 at 9:49pm

#118463
Dec 7, 2007 at 10:01pm

yeah, I called updatePattr() rather than notifyclients to synchronize my arrays first, then notify. Yes that makes sense

I just re-read the manual and am implementing arguments – which totally looks like its going to work. Im glad we both found the right method!

Thanks Emmanuel for the help!

#118464
Dec 7, 2007 at 10:19pm

#118465
Dec 7, 2007 at 10:50pm

so just a heads up – it works 100% SOme psuedocode below that illustrates if you want to handle multiple pattr entries in pattr:

// the variables I want to store
var somefloat = 3.14159;
var somestuff = “some stuff or whatever”;
var someint = 10;

//make an array of items you want to store
var mypattritems = new Array()

function updatePattr()
{
mypattritems[0] = somefloat;
mypattritems[1] = somestuff;
mypattritems[2] = someint;
notifyclients(); // for pattr to notice!
}

// for pattrstorage to store it
function getvalueof()
{
return mypattritems; // this sends the array to pattrland
}

// for pattrstorage to return it to JS
function setvalueof()
{
// note we use ARGUMENTs here, not like in the manual

pi = arguments[0];
stuff = arguments[1];
someint = arguments[2];
}

// getter setter methods for our individual variables
msg_int(v)
{
someint = v;
updatePattr(); // updates our array with the new values and tells pattr
}

function msg_float(v)
{
somefloat = v;
updatePattr();
}

etc. etc.

Thanks again -ej!

#118466
Sep 9, 2013 at 5:49am

Going through this code, I see what’s going on, but I’m getting nothing populated in the client window at all.

Attached are the patch and JS zipped. The patch seems deficient in that it needs additional objects, but the JS contains everything which seems to be required.

Also, note that Emmanuel’s posts in this thread are empty, so I don’t see anything he’s posted.

Can someone update the patch to make it work as a technology demonstrator ??

Attachments:
  1. JS-with-Pattr.zip
#264698

You must be logged in to reply to this topic.