Problem with a simple function

About advanced scenes, and the Thyme scripting language used in Algodoo.

Problem with a simple function

Postby Xray » Sat Jul 27, 2013 9:28 pm

This was the first time I tried writing a function that includes a timed event, and it doesn't work. The function, scene.my.flash(t), has only one argument which is the time duration of the flash. My test scene is shown below. The function that I call during onCollide is:

scene.my.flash:= (t)=> {sim.time > scene.my.timer + t ? {scene.my.lamp = 0}:{}}

Prior to calling the function, I set scene.my.lamp = 1 so that it turns on the lamp (makes the box turn white), I set scene.my.timer to sim.time, and then the function is supposed to turn the lamp off after time "t" (in this case, 0.1 seconds). I think what's happening is that the function does not continue to execute during the timed event. I believe it just executes for one simulation cycle and then returns. Can someone please take a look at it and tell me what I'm doing wrong? And also show me a better way to do what I am trying to do!

Thanks
Attachments
Flash Test.phz
(11.09 KiB) Downloaded 29 times
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Problem with a simple function

Postby tatt61880 » Sun Jul 28, 2013 3:32 am

That is because your Scene.my.flash function will be called only when the box is collided.

Move Scene.my.flash(0.1) to the box attribute named "update".
i.e.
Code: Select all
onCollide = (e)=>{
    scene.my.lamp = 1;
    scene.my.timer = sim.time
}
update = (e)=>{
    scene.my.flash(0.1)
}
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan

Re: Problem with a simple function

Postby Xray » Sun Jul 28, 2013 4:28 am

Tatt - I made that change, and now it works like I wanted it to! THANKS VERY MUCH! :thumbup:

But, could you please explain something about it that has me puzzled? electronicboy suggested a similar fix when we chatted over Skype, and I commented that if you put the call to the function in a continuous loop, it probably would not work because the argument that gets passed (0.1) would keep resetting the timer, and would never allow it to time out. But apparently, that does not happen. So please explain why continually calling the function won't continually keep setting the timer with the argument (0.1).

EDIT: NEVER MIND! I figured out myself the answer to my question. The argument does not reset the timer. The argument is simply a constant that gets added to the timer for the "if" check. I should have realized that before asking my dumb question! :lol:
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 5 guests