Home   Help Search Login Register  

Author Topic: Counting time since adding EH  (Read 679 times)

0 Members and 1 Guest are viewing this topic.

Lean Bear

  • Guest
Counting time since adding EH
« on: 29 Jun 2005, 19:07:51 »
OK. I've got this fairly basic script with a "fired" EH.

What I want to do is get certain points in the time since the EH was added. And set up conditions around these times.

For example:

Code: [Select]
player switchMove "Animtation1"

? player switchMove "Animation1" : goto "Loop"

#Loop

player addEventHandler ["fired",{if (_time < 0.63 && _time > 0.61) then (goto "Loop2") else (goto "Loop")}]
~0.01

goto "Loop"

#Loop2

player switchMove "Animation2"
~2.52

goto Loop

How would I go about this?

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re:Counting time since adding EH
« Reply #1 on: 29 Jun 2005, 23:01:46 »
huh? not sure i understandy quite, but you seem to be saying that you a) want to add an event handler to a unit and b) want things to happen at specified times after the event handler was added...

so.

in the unit's init field put

Code: [Select]
time_count = unit_name addEventHandler ["fired",{[] exec "counter.sqs"}]

'counter.sqs' might go as follows...

Code: [Select]
;count time since event handler was added...

_current = _time

#loop1
?_time >=10 : put commands here ; goto "loop2"
~0.1
goto "loop1"

#loop2
?_time >=20 : put commands here ; goto "loop3"
~0.1
goto "loop2"

#loop3
?_time >=20 : put commands here ; goto "end"
~0.1
goto "loop3"

#end
unit_name removeeventhandler ["fired",time_count]

exit

that what you're after?
« Last Edit: 29 Jun 2005, 23:03:30 by bedges »

Lean Bear

  • Guest
Re:Counting time since adding EH
« Reply #2 on: 29 Jun 2005, 23:11:44 »
I think it may well be.. :)

Adding the EH isn't really the problem. It's setting the commands in parrallel with the time that I was having trouble with.

The only bits I don't understand about your script are:

_current = _time // Isn't _time a reserved variable, and also why do you use _current as I don't see it used aywhere else in the script.

And...

unit_name removeeventhandler ["fired",time_count] // What's with the time_count variable?

umm, also, I don't quite get how it links...

Is it that: when the "fired" EH is triggered (eg. by the unit firing :P) the "counter.sqs" runs. But what determines the time? Is the _time variable one of those that does stuff different to other variables? Then, depending on the time, the script cycles through the various loops until it reaches the corect one. Or none, in whihc case the script ends.

Thanks for your help. That is pretty much the framework of what I was after. If you could elaborate on those points afore mentioned, I would be most grateful. :)

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:Counting time since adding EH
« Reply #3 on: 30 Jun 2005, 01:45:49 »
Okay, I don't think I quite understand what you are after. If I am reading it correctly, then would this work?

Code: [Select]
_unit addeventhandler ["fired", {whatever}]
~30
;stuff to take place 30 seconds after EH is added
~5
;stuff to take place 35 seconds after
~etc

Is that what you are trying to do? Simple enough, right?

Or, perhaps you mean, you want the EVENT HANDLER script to do different stuff depending on how long it has been since adding it? So, for example, it makes the unit poop every time he shoots for the 1st 30 seconds, then it makes him throw boulders for the next 30, etc?

In that case, it is more complicated. I'd suggest "passing" the time that the EH was added into the EH code. Some tricky use of the format command will do the job:

_unit addeventhandler["fired", format[{%1 exec "script.sqs"}, time]

   script.sqs
? time - _this > 60 : goto "60secondspassed"
 ? time - _this > 30 : goto "30secondspassed"
goto "lessthan30secondspassed"

Note that the (time since mission start) has been 'hard coded' into the EH code as soon as the EH is added. Then the script checks the current time vs when the EH was added to see how much time has passed.

HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re:Counting time since adding EH
« Reply #4 on: 30 Jun 2005, 07:21:16 »
aye, sorry, hard day at work yesterday, the script was incomplete.  :-[

it should have gone something like

Code: [Select]
;count time since event handler was added...

_current = _time

#loop1
put commands here
~0.1
?not (_time >=(current+10)) : goto "loop1"

#loop2
put commands here
~0.1
?not (_time >=(current+20)) : goto "loop2"

#loop3
put commands here
~0.1
?not (_time >=(current+30)) : goto "loop3"

unit_name removeeventhandler ["fired",time_count]

exit

as you say, that will loop through commands until the time is however many seconds past the time the event handler was first fired. if you need to keep it at a certain point (without repeating commands) until the time has progressed to a certain point, use

Code: [Select]
@not (_time >=(current+30))
that will pause the script at that point until 30 seconds has passed from the start of the script. note that the _current+10, _current+20 can be any time, not just 10 or 20.

Lean Bear

  • Guest
Re:Counting time since adding EH
« Reply #5 on: 02 Jul 2005, 18:08:23 »
Hey guys, thanks for all your help :)

Using elements from both your scripts (the "passing time" from GB's and the original time co-ordinating from Bedges' script) I managed to come up with this:

Init_Script.sqs

Code: [Select]
_unit addEventHandler ["fired", format[{%1 exec "script.sqs"}, time]]
; commands here ;

Script.sqs

Code: [Select]
? (time - _this) > 2.45 : goto "End"
? ((time - _this) > 2.2) && ((time - _this) < 2.45) : goto "Commands"
? (time - _this) < 2.2 : goto "End"
goto "End"

#Commands

; Commands Here ;

~2.52

#End

_unit removeAllEventHandlers "fired"

exit

Fairly simple at the moment - but it is a great start :D

The first script adds the event handler and gives the inital commands.

When the second script is called, the time since the EH is activated (_this) is minused from the time since the mission start (_time).

Depending on the time, various commands can be issued. Not just generally as was first suggested (eg. < 30 etc.), I needed something more specific - which is why I put lines in like:

? ((time - _this) > 2.2) && ((time - _this) < 2.45)

Which means, if the time is between 2.2s and 2.45s - then the command is run - otherwise, the script will exit.

Again thank you both, expect to see this in use more often very soon ;)

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:Counting time since adding EH
« Reply #6 on: 02 Jul 2005, 22:55:25 »
Alright, I hate to be too anal, but you should avoid the "removeALLeventhandler" command whenever possible. What if an EH was added by another script? Then you just erased it.

Unfortunately, EH indexes don't work the same way that actions do, so they can be a real pain in the arse to remove. The problem is, if you add 3 EH's, their indexes might look like this:

EH1 -> 0
EH2 -> 1
EH3 -> 2

If you remove the second EH, then their indexes would look like this:

EH1 -> 0
EH3 -> 1

So the index of EH3 just dropped, meaning you can't reliably store its idx in a global variable. Sux, don't it? However, if you are only adding the EH for a short period of time, you might be able to assume that no other EHs will be removed in that time.

The other problem is that the EH doesn't pass it's idx to it's code like with actions. This can be solved with the same little trick you used to pass "time" to the EH:

Code: [Select]
;find out the next EH's index with a dummy EH
_idx = _unit addeventhandler ["fired", {}]
;remove the dummy EH
_unit removeeventhandler ["fired", _idx]
;add the real EH. It will have the index we found above
;pass the index we found into the EH we add via the "format" command
_unit addeventhandler ["fired", format[{[_this select 0, %1] exec "blah.sqs"}, _idx] ]

Then in you have the EH's index passed to its code. Within that code, you can then use that index to remove that EH only, without removing any others that might exist:

blah.sqs
Code: [Select]
? time > 30 : (_this select 0) removeeventhandler ["fired", _this select 1]
...
HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

Lean Bear

  • Guest
Re:Counting time since adding EH
« Reply #7 on: 03 Jul 2005, 16:36:05 »
Quote
However, if you are only adding the EH for a short period of time, you might be able to assume that no other EHs will be removed in that time.

Luckily, I am :)

From the various scripts that will be running around this, only one EH will be added by the scripts I am using at a time - and last no longer than about 30secs. That should be short enough, right?

So, if I were just to use the removeEventHandler command instead and do it by the EH's idx - I should be ok, right?

eg. _unit removeEventHandler ["fired",0]