Buggy behaviour with for in loops


#1

In my collision code where I make heavy usage of for in loops to cycle through an array of wall names I have been experiencing some buggy behaviour.

To check collision I check the player object against every wall to see if there is a collision. The array stores the names of the walls so that they can be retrieved from the project object when I want to check collision. Whenever I do this in the loop i get an error.
“Line null: TypeError: project.getObject(…) is null”

Here’s a code excerpt from the player object:

speed = 4;


function update() {
    
    
    prevx = this.x;
    prevy = this.y;
    
    hitHorizontal = false;
    hitVertical = false;
    
    walls = project.getObject("Data").walls
    
    project.getObject("Debug").setText(walls[0])
    
    
    
    
    if(keyIsDown("Up")) {
        this.y -=speed;
    }
    if(keyIsDown("Down")) {
        this.y +=speed;
    }
    
    for (var wall in walls) {
        if (this.hitTest(wall)) {
            hitVertical = true;
        }
        
    }
    
    
    if(hitVertical) {
        this.y = prevy;
    }
    
    if(keyIsDown("Left")) {
        this.x -=speed;
    }
    if(keyIsDown("Right")) {
        this.x +=speed;
    }
    
    for (var wall in walls) {
        if (this.hitTest(wall)) {
            hitHorizontal = true;
        }
        
    }
    
    
    if(hitHorizontal) {
        this.x = prevx;
    }
    
}

and here’s the array in question

project.getObject("Data").walls = ["Wall1"]

And here’s a screenshot of the scene in question


#2

Hmm, I wasn’t able to replicate this.

This issue may be cause by a few things.

Option 1: The issue here might be in the name of the “Data” object. Check the case “Data” object as spelled in the Object Name section of the inspector!
22%20PM

Option 2: Timing. The array in project.getObject("Data") might not be created before your code tries to call it. To ensure that the array is created before you try to access it, you can put the project.getObject("Data").walls = ["Wall1"] code into the function load() { } script of the first frame of your project. This will guarantee that once the first frame of your project is loaded, the array will be created.

Alternatives:

You actually don’t need to store this information inside of an object within the project, you could store this data inside of the project itself! Try this out for instance

function load() {
  project.data = {}; // Create a data object in my project.
  project.data.walls = ["Wall1", "Wall2"]; // Store an array of wall names inside of my data object
  console.log(project.data.walls); // Retrieve my walls array and print to the console! 
}

Let know if option 1, 2, or the alternative fixes this error. If not, we’ll do some more digging!


#3

Hmm. Well thanks for the info on creating global variables but I’m still having trouble with the hittest.
If I attemt to hitTest the returned object from project.getObject(wall) it gives me “Line null: TypeError: otherObj is null”
If I just attempt to hittest the string it gives me “Line null: TypeError: otherObj.regenBounds is not a function”

Does this help or should i upload the project file?