Using if block and coordinates


#1

Hi - I can’t seem to work out why the rocket won’t move when I try to run the following code. The coordinates are definitely 0,0,0. Sorry if I’m missing something obvious!


#2

Hi Ronan,

thanks for pointing it out. The logic is correct and I think it should work in Blockly. However, a comparison like this doesn’t work in the generated code behind the scenes. But this is something we need to have a look at. Please use this workaround for now:


#3

OK thanks Benny! I will use that.


#4

Hi Benny,

I had another look and it seems the issue persists? I suppose the best option is to code in javascript so these issues don’t arise?

Thanks


#5

Hi Ronan,

even when using JavaScript you will run into the same problem when comparing non-primitive datatypes (objects). Have a look at this example:

const rocketPos = Scene.createItem('LP_Rocket', 0, 0, 0).getPosition();
const personPos = Scene.createItem('LP_Wom', 0, 0, 0).getPosition();

Space.log(rocketPos); // {x: 0, y: 0, z: 0}
Space.log(personPos); // {x: 0, y: 0, z: 0}
Space.log(rocketPos === personPos); // false

Here we are creating two objects at the same location but when we try to compare the positions using === we get false. Even though the values of the objects properties are the same, JavaScript will treat them as different objects because they are pointing to different locations in memory.

So one way to get around this problem (in this particular example) is to convert the objects to String data types and compare them instead.

Space.log(rocketPos.toString() === personPos.toString()); // true

This is because in JavaScript primitives like strings and numbers are compared by their value, while objects like arrays, dates, and plain objects are compared by their reference.


#6

Great - thank you Benny - appreciate your response!