Timer, Alarm, Event. Easy.
9 posts • Page 1 of 1
Timer, Alarm, Event. Easy.
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...
-
Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
Re: Timer, Alarm, Event. Easy.
Code can be simpler a lot, but I've tryed to optimize it for best performance.
Dream of Algodoo as game development engine...
-
Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
Re: Timer, Alarm, Event. Easy.
That is sweet. Defiantly stealing this, with your permission of course.
-
Mystery - Posts: 2802
- Joined: Thu Sep 03, 2009 1:16 pm
- Location: Southern Australia
Re: Timer, Alarm, Event. Easy.
Mystery wrote::D That is sweet. Defiantly stealing this, with your permission of course.
That's why I made it
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...
-
Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
Re: Timer, Alarm, Event. Easy.
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.
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...
-
Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
Re: Timer, Alarm, Event. Easy.
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.
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
-
Chronos - [Most Active Member 2010]
- Posts: 4457
- Joined: Mon Aug 31, 2009 6:00 pm
- Location: Californania
Re: Timer, Alarm, Event. Easy.
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.
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.
It's an old version of events, you better check v2
http://www.algodoo.com/forum/viewtopic.php?f=13&t=2962
http://www.algodoo.com/forum/viewtopic.php?f=13&t=2962
Dream of Algodoo as game development engine...
-
Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
9 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 7 guests