leroidangleterre wrote:Hello everyone,
I'm quite new at scripting with Thyme, and I have a problem with the syntax. I am having fun with the "onCollide" function (which is cool, I also built a water source), and I would like to create sort of a "speed controller"; it would be an object (like a box, or a circle), and whatever object that would hit that box would have its speed set to a specific value.
My question is: how do you change the speed of the other object when a collision occurs?
I tried this:
Scene.addBox {
<code about the size, the color or the position of the box>
onCollide = (e)=>{e.other.vel:=[1, 0];}
};
and it doesn't work.
Obviously, I something is wrong in my code but the object is still loaded and collides with other objects (so it means the computer understands what I tell him to do, except it is not what I want it to do...)
If you know how to do that, it would be great! Thanks!
I think you're on the right track- try swapping the "=" and ":=".
Also, Scene.addBox is a function, so you need parentheses around its argument, which is itself surrounded by curly braces.
Thus, this untested code
- Code: Select all
Scene.addBox({
//commented code about the size, the color or the position of the box
onCollide := (e)=>{e.other.vel = [1, 0]} //You also don't need a semicolon immediately before a right brace
})
should spawn a box that sets the velocity of anything that touches it to 1 meter per second to the right. Try increasing the values in the array- you may not even see this happen.
Thyme has two assignment operators, "=" and ":=". Generally, they work the same, but you have found one instance where they don't.
":=" is normally used for creating variables- it will always create a variable if one of the appropriate name doesn't exist in the exact location where the := exists- so, in spawning scripts, it is used to set inintial values of variables in the object being spawned.
"=" works similarly, but (to my experience- correct me if I'm wrong) seems to look around a little before creating variables. This is no problem when the variable already exists, but if you try to use "=" to set a value in an object in the process of being spawned, it will instead go to whatever's doing the spawning.
Thus, your script will not actually set any properties of the box being spawned, but instead mess with the thing spawning it.