Variable values being changed in script


#1

Can someone help please?
I am working with entity positions and movement and I hit a strange problem.
I dont know if there is something wrong with my code or if it is a bug in the system.

Simple script, object is created at “startPoint” and moved to an "endPoint"
I have not applied any action on the variable startPoint I simply referred to it,
I applied action on the model properties.position.x
Yet as the model moves and its x value increments, the x value of startPoint also moves in lock step.

The expected outcome is to be able to get to the endPoint with the values at startPoint as they were when I set them. StartPoint shouldnt change.

Script is set up for instant debug, just run the script, the consol results will show the startPoint incrementing
Thanks in advance for any help, if it turns out to be a bug I can describe it.

var	startPoint = {x: 8018, y: 202.0, z: 8218}; 
var	endPoint = {x: 8068, y: 202.0, z: 8218}; 

var originalProperties = {
	type: "Model",
	position: startPoint,
	radius : 13,
	color: { red: 0, green: 255, blue: 0 },
	modelURL: "http://business.ozblog.me/objects/monkey.fbx",
	dimensions: {x: 4, y: 4, z: 4},
	lifetime: 60,
	};
	
	var newProperties = originalProperties;	
	var entityID = Entities.addEntity(originalProperties);
			
	function move(){
		print ("startPoint.x beginning = "+startPoint.x); //debug
		
		while (newProperties.position.x < endPoint.x){			
			newProperties.position.x = newProperties.position.x + 0.01;	
			Entities.editEntity(entityID, newProperties);
			print ("startPoint.x runtime= "+ startPoint.x);	

		}
		print ("startPoint.x should be 8018** actual = " + startPoint.x);  //debug			
		print ("new properties.x  " + newProperties.position.x );  //debug 

		Script.stop();
	}
	
	move();

#2

Your while loop where not working correct, i changed it. and for me this works.
I would btw do long movements in different way a while loop keeps the system more busy. editModelsExample.js is goiod one for that.

var    startPoint = {x: 8018, y: 202.0, z: 8218}; 
var    endPoint = {x: 8068, y: 202.0, z: 8218}; 

var originalProperties = {
    type: "Model",
    position: startPoint,
    radius : 13,
    color: { red: 0, green: 255, blue: 0 },
    modelURL: "http://business.ozblog.me/objects/monkey.fbx",
    dimensions: {x: 4, y: 4, z: 4},
    lifetime: 60,
    };

    var newProperties = originalProperties;    
    var entityID = Entities.addEntity(originalProperties);
    var i = 0;
    var step = 0.005;


    function move(){
        print ("startPoint.x beginning = "+startPoint.x); //debug

        while (i < (endPoint.x-newProperties.position.x) ){            
            newProperties.position.x = newProperties.position.x + step;    
            Entities.editEntity(entityID, newProperties);
            print ("startPoint.x runtime= "+ startPoint.x);    
            i=i+step;
        }
        print ("startPoint.x should be 8018** actual = " + startPoint.x);  //debug            
        print ("new properties.x  " + newProperties.position.x );  //debug 

        Script.stop();
    }

    move();

Just made a small correction to make the stepping easy anmd correct.


#3

I suspect i’s working as intended. What I think is happening is that startPoint is the position property of the model so when you add the new properties after the increment, startPoint should increment to represent the new position of the model.


#4

Thanks @Richardus_Rayma but that still gives me the same result.

But I will look at better ways of updating entities without loops, sounds like good advice.


#5

@Ciaran_Laval thats it, thank you, by putting a variable in the properties I get the latest properties.

Thanks again :slight_smile:


#6

Strange, just tried my script on your domain. and i see it moveing fine.
I see what you want, yes thats a nicer way.


#7

Yeah the object moves ok, all the info is in the consol, it tells me the variable is changing.
This part of the script works but the next part didnt because of what happens in this part.

All working good now thx :slight_smile: