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] I'm confounded by my own code.

September 27 (8 years ago)
So, relatively vague title, and I'm going to provide relatively little to go on cause I don't want to post tons of my amateur code and try to explain whats going on (at least not for now).  

I'm making a kind of puzzle game that involves pieces that move on their own based on certain attributes.  Two of my main functions are a "trackMoves" and a "collisionCheck".  When the movement tracker detects that an object has run out of a certain attribute, it's supposed to erase it and remove it from a master array of "allRealizedObjects" before passing that array to the collision checker to see if any objects occupy the same position.

My problem right now is that the array of objects seems to change when its passed from one function to the next, even though there shouldn't be anything modifying the array in between.  My log looks like this:

"removeSpent allRealized fires"
"allRealizedObj.length at track moves0"
"collisionCheck fires"
"allRealizedObj.length at collisionCheck2"

I don't know what's going on. Hence, I am confounded.  I've tried rejiggering things and if I put the "removeSpent" function at the beginning of "collisionCheck" instead of the end of "trackMoves" then the array turns out correct but there are new problems.  I'm a VERY amateur coder and I'm pretty stuck on this after enjoying some mild success making my scripts work.  Any suggestions are appreciated, I can post more info or direct you to the github that my data-engineer little brother helped me set up!




September 28 (8 years ago)

Edited September 28 (8 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Not going to be able to help you with much without the code, you can always post a link to a gist on GitHub. If you are looking to check for token collisions, I'd recommend using Stephen's tokencollisions script.

From the logs you posted, it looks like your collision detection function is modifying the array though.
September 28 (8 years ago)
The Aaron
Pro
API Scripter
Scott is likely right. In JavaScript, arrays are passed by reference.  For example:
var arr=[1,2,3];
function fun(c){
  log('head is: '+c.shift());
}
fun(arr);
fun(arr);
log('array is: '+JSON.stringify(arr));

Since function get the actual array and not a copy, any manipulation they do will persist across scopes. The above will log:
head is: 1
heaf is: 2
array is: [3]
September 28 (8 years ago)
Ada L.
Marketplace Creator
Sheet Author
API Scripter
If you need to pass the array by value instead of by reference, you can use Underscore's _.clone function.
E.g. myFunc(_.clone(allRealizedObjects));
September 28 (8 years ago)
Thank you guys for your suggestions.  The collision script library looks pretty thorough, gonna take me a while to decipher it though.  I was aware that the actual array was passed.  I've had a hell of a time trying to figure out ways to clone objects and such.  Those underscore functions are new to me, seems like there's a lot of good tools there.

Ermahgerd I ferxed mah curd!   So after a few more hours staring at this stuff I wrote like 6 months ago (projects been on hold for summer of wedding, new job, new city) I found the one dumb little thing I did.  For some reason I named the parameter in my trackMoves function the same exact name of my allRealizedObj array.  I didn't even need a parameter, so deleted it and whatever weird thing it was doing stopped.

Thanks for all your help.  I'll be back with my next problem soon I'm sure.