How to stop local variables being overwritten?

Thanks, I’ll have to assume any values that get passed around can be overwritten then

by user 103054507105067008

Yes you have to be careful there unfortunately. I wish there was a way to make that easier / behave like in Unity

let pos = this.initialTarget.clone(); still seems to overwrite this.initialtarget somehow

by user 103054507105067008

Have been reading up on readonly, but can’t get it to work in Typescript https://www.tutorialsteacher.com/typescript/typescript-readonly

by user 103054507105067008

I dont think readonly would help here since it wouldnt prevent anyone from changing “myVec.x” (it prevents anyone from settings myVec = myOtherVec tho)

assigning another vector instance will be prevented by readonly but not changing a value inside of your object.

In a really simple scenario I have a vector3 I set in start that I want to reuse in functions without changing its value :frowning_with_open_mouth:

by user 103054507105067008

For debugging you could try wrapping e.g. x of your vector in a change listener (I havent tried that before in a case like this but it seems you’re a bit lost here)

.clone() seems to keep a reference to the original variable

by user 103054507105067008

Engine utils exports a Watch type. Try wrapping it around x like so:

const watch = new Watch(myVector, "x");
watch.subscribeWrite(args => console.trace(args));

That should log some info for further debugging I hope

Is that the recommended solution or just good for debuggin?

by user 103054507105067008

For debugging

From those logs, same as my own logs for it, it gets changed the moment it gets read basically

by user 103054507105067008

All references here, that this.initial target = this.targetPosition is in start. I would then assume that it always has a reference to this.targetPosition then as this.targetPosition is changed later on
unknown.png

by user 103054507105067008

That did the trick, seems if a value is set to that of another value without using .clone(), it is just a reference to that value

by user 103054507105067008

this.initialTarget = this.targetPosition.clone();

by user 103054507105067008

thats what I said here :slightly_smiling_face:

I missed looking back at where the value was set :slightly_smiling_face: hope this post helps others

by user 103054507105067008