Timer, Alarm, Event. Easy.

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

Timer, Alarm, Event. Easy.

Postby Kilinich » Sun Jan 17, 2010 11:23 pm

Rating: rated 6.7
Filesize: 1.94 kB
Comments: 7
Ratings: 5
download


Key feature is every-frame evend handler "Scene.my.event" that can be attached to any global property.
For example:
Code: Select all
App.Background -> {
    cloudOpacity = {Scene.my.event(0.94999999)};
    ...};


And this is all the rest:

Code: Select all
Scene.my.event := (n)=>{Scene.my.evHandle; n};
Scene.my.newAlarm := (aTime, aWhat)=>{
    Scene.my.events = Scene.my.events ++ [[aTime, 0, 1]];
    Scene.my.eventsCount = Scene.my.eventsCount + 1;
    Scene.my.actions = Scene.my.actions ++ [aWhat];
    scene.my.eventsCount-1
};
Scene.my.newEvent := (eTime, eRepeat, eCount, eAction)=>{
    Scene.my.events = Scene.my.events ++ [[eTime, eRepeat, eCount]];
    Scene.my.eventsCount = Scene.my.eventsCount + 1;
    Scene.my.actions = Scene.my.actions ++ [eAction];
    scene.my.eventsCount-1
};
Scene.my.newTimer := (tTime, tWhat, tRepeat)=>{
    Scene.my.events = Scene.my.events ++ [[tTime + sim.time, tTime, tRepeat ? 9999999 : 1]];
    Scene.my.eventsCount = Scene.my.eventsCount + 1;
    Scene.my.actions = Scene.my.actions ++ [tWhat];
    scene.my.eventsCount-1
};
Scene.my.delEvent := (i)=>{
    newActions = [];
    newEvents = [];
    Scene.my.eventsCount = Scene.my.eventsCount - 1;
    for(i, (n)=>{
        newActions = newActions ++ [Scene.my.actions(n)];
        newEvents = newEvents ++ [Scene.my.events(n)]
    });
    for(Scene.my.eventsCount - i, (n)=>{
        newActions = newActions ++ [Scene.my.actions(n + i + 1)];
        newEvents = newEvents ++ [Scene.my.events(n + i + 1)]
    });
    Scene.my.events = newEvents;
    Scene.my.actions = newActions
};
Scene.my.eventsCount := 0;
Scene.my.events := [];
Scene.my.actions := [];
Scene.my.evHandle := {
    sim.running ? {
        DelEvents = [];
        DelCount = 0;
        Scene.my.frame = Scene.my.frame + 1;
        for(scene.my.eventsCount, (ev)=>{
            scene.my.events(ev)(0) < sim.time ? {
                Scene.my.actions(ev)();
                Scene.my.events(ev)(2) > 1 ? {
                    newEvents = [];
                    for(ev, (n)=>{newEvents = newEvents ++ [Scene.my.events(n)]});
                    newEvents = newEvents ++ [[scene.my.events(ev)(1) + sim.time, scene.my.events(ev)(1), scene.my.events(ev)(2) - 1]];
                    for(Scene.my.eventsCount - ev - 1, (n)=>{newEvents = newEvents ++ [Scene.my.events(n + ev + 1)]});
                    Scene.my.events = newEvents
                } : {
                    DelEvents = DelEvents ++ [ev];
                    DelCount = DelCount + 1
                }
            } : {}
        });
        for(DelCount, (ev)=>{scene.my.delEvent(DelEvents(ev))})
    } : {}
};
Scene.my.timers := [];
Scene.my.frame := 0;
Last edited by Kilinich on Fri Jun 04, 2010 8:26 pm, edited 2 times in total.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Timer, Alarm, Event. Easy.

Postby Kilinich » Sun Jan 17, 2010 11:32 pm

Code can be simpler a lot, but I've tryed to optimize it for best performance.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Timer, Alarm, Event. Easy.

Postby Mystery » Fri Jan 22, 2010 12:37 pm

:D That is sweet. Defiantly stealing this, with your permission of course. :lol:
User avatar
Mystery
 
Posts: 2802
Joined: Thu Sep 03, 2009 1:16 pm
Location: Southern Australia

Re: Timer, Alarm, Event. Easy.

Postby Kilinich » Fri Jan 22, 2010 9:16 pm

Mystery wrote::D That is sweet. Defiantly stealing this, with your permission of course. :lol:

That's why I made it :thumbup:
Recently, I've use events for debugging (stop sim at particular time) and time traveling (app.step and than undo). More than that - it's possible to automatically start/stop sim by events (using app.time).
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Timer, Alarm, Event. Easy.

Postby Kilinich » Sat May 22, 2010 6:35 am

To make another events by yourself just call (type in console or on collision) one of functions:

To make a timer:
scene.my.newTimer(countdown, {{action}}, repeat);
for example: scene.my.newTimer(1, {{sim.running = false}}, true) will stop simulation every second.

To make an alarm:
scene.my.newAlarm(time, {{action}});
for example: scene.my.newAlarm(10, {{scene.camera.pan = [0,0]}}) will move focus to 0,0 at 10 second of sim.

To make custom event:
scene.my.newEvent(time, countdown, count, {{action}});
for example: scene.my.newEvent(7, 1, 10, {{scene.addCircle({pos=[0,0]})}}) will spawn 10 circles one by one every 1 sec after 7 sec sim time.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Timer, Alarm, Event. Easy.

Postby InfinityPlayer » Sat Mar 26, 2011 8:58 pm

Kilinich, where in Algodoo do i put all that code? I don't, as yet, fully understand these codes that people put up, cos they don't tell you where to put them. Unless you've put it in the above, i'm just not seeing it. :(
I am still learning, not an expert yet. I'm just here to have some phun.
InfinityPlayer
 
Posts: 40
Joined: Thu Dec 09, 2010 7:57 pm

Re: Timer, Alarm, Event. Easy.

Postby Chronos » Sat Mar 26, 2011 9:19 pm

InfinityPlayer wrote:Kilinich, where in Algodoo do i put all that code? I don't, as yet, fully understand these codes that people put up, cos they don't tell you where to put them. Unless you've put it in the above, i'm just not seeing it. :(


He said where. ;)

Kilinich wrote:(type in console or on collision)
TheWinkits wrote:They both looks of cuking amazing
User avatar
Chronos
[Most Active Member 2010]
 
Posts: 4457
Joined: Mon Aug 31, 2009 6:00 pm
Location: Californania

Re: Timer, Alarm, Event. Easy.

Postby InfinityPlayer » Sat Mar 26, 2011 9:22 pm

Chronos wrote:
InfinityPlayer wrote:Kilinich, where in Algodoo do i put all that code? I don't, as yet, fully understand these codes that people put up, cos they don't tell you where to put them. Unless you've put it in the above, i'm just not seeing it. :(


He said where. ;)

Kilinich wrote:(type in console or on collision)


Thanks, Chronos. I think it's funny that the first person to reply to this request is a person named Chronos, which is Latin for time, in a post about timers, etc. :D :thumbup: :clap: :lol:
I am still learning, not an expert yet. I'm just here to have some phun.
InfinityPlayer
 
Posts: 40
Joined: Thu Dec 09, 2010 7:57 pm

Re: Timer, Alarm, Event. Easy.

Postby Kilinich » Sat Mar 26, 2011 11:25 pm

It's an old version of events, you better check v2
http://www.algodoo.com/forum/viewtopic.php?f=13&t=2962
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 0 guests

cron