Home   Help Search Login Register  

Author Topic: Life time of a script  (Read 4338 times)

0 Members and 1 Guest are viewing this topic.

Offline bardosy

  • Honoured Contributor
  • ***
  • campaign designer
    • CartooDiv
Life time of a script
« on: 12 Jul 2005, 12:42:36 »
If I write en exit command at the end of the script, the script terminated and his lifetime finish.
But if I make infinite loop in a script and I never call the exit command when terminate this script? If the mission - witch call this script - end the script and too?

I made a script without exit and I test the mission lot of times and once I feel the lag and fps drop low... Is it because my script have no exit point?
Fix bayonet!

CrashnBurn

  • Guest
Re:Life time of a script
« Reply #1 on: 12 Jul 2005, 13:18:09 »
Make sure you have a delay before looping the script. Even a short delay of about 0.1 will help. A script with no delay will cause problems if it runs too long. Think about what your script is checking, and use the biggest delay possible. This can help prevent over usage of the cpu and allow you to run other scripts at the same time. It's all a big balancing act.

Offline bardosy

  • Honoured Contributor
  • ***
  • campaign designer
    • CartooDiv
Re:Life time of a script
« Reply #2 on: 12 Jul 2005, 13:36:42 »
But I can't use any little delay...
My script simulate a hanged man and if I put any small delay (~0.001) the man started jumping on the rope and it's a little bit horror... ;D

(he jump because in the delay time the gravity pull down and after the delay I setPos back up)
« Last Edit: 12 Jul 2005, 13:37:04 by bardosy »
Fix bayonet!

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Life time of a script
« Reply #3 on: 12 Jul 2005, 14:41:20 »
This is how I simulated a hanging (actually two) in the intro to my current mission.  You will see that the hanging starts only when I set a variable (StamenovScene) to true elsewhere and ends when that variable is set to false.  Otherwise the lag was just too much.

The idea of using paradead I got from macguba's Un-Impossible mission, the periodic swinging of the corpse is mine.

Code: [Select]
_start1 = getDir victim1
_start2 = getDir victim2

;Size of swing from the centre in degrees
_swing1 = 15
_swing2 = 10

;time for one complete swing in seconds
_period1 = 10
_degreespersec1 = 360/_period1
_period2 = 12
_degreespersec2 = 360/_period2

@StamenovScene
#loop

~0.001
victim1 setDammage 0
victim2 setDammage 0

victim1 setpos [getpos victim1 select 0, getpos victim1 select 1, 1.2]
victim1 switchMove "paradead"
victim2 setpos [getpos victim2 select 0, getpos victim2 select 1, 1.2]
victim2 switchMove "paradead"


victim1 setDir (_start1 + _swing1 * sin (_degreespersec1 * _time))
victim2 setDir (_start2 + _swing2 * sin (_degreespersec2 * _time))

if not StamenovScene then {exit}

goto "loop"

I hope this helps.

Offline Baddo

  • Former Staff
  • ****
  • Reservist Jaeger
Re:Life time of a script
« Reply #4 on: 12 Jul 2005, 16:18:09 »
Just to add:

You do not need to write an exit command to the end of scripts. OFP will for sure notice when the end of the file has been reached. The only sensible use for exit is when you need to exit a script somewhere from the middle of it.

In other words for those who did not get it yet, writing exit to the last line of your scripts is totally pointless. It very likely increases the work OFP has to do to process through a script file, because OFP has to think like this:

"Hey wait... there's something on that line... What command is it, or is it a command at all? Oh yeah, it's the exit command! Maybe he wants me to close this file now... Closing now."

instead of thinking like this:

"End Of File reached. Closing now."

Which way you think is faster?

:P

Offline Mikero

  • Former Staff
  • ****
  • ook?
    • Linux Step by Step
Re:Life time of a script
« Reply #5 on: 12 Jul 2005, 17:13:47 »
>Which way you think is faster?

the one that doesn't call the script at all

<slap>
Just say no to bugz

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Life time of a script
« Reply #6 on: 12 Jul 2005, 18:47:02 »
An alternative:

Script one:

Oh here is an exit, right I know what to do.   Release all the memory used by the variables and the script and pass the program pointer back to who ever needs it.

Script two:
Shit, an EOF, now what?  Do I release the memory being used by the local variables, and by the script?  Well the script writer couldn't give a shit why should I.




Offline Baddo

  • Former Staff
  • ****
  • Reservist Jaeger
Re:Life time of a script
« Reply #7 on: 12 Jul 2005, 19:11:39 »
An alternative:

Script one:

Oh here is an exit, right I know what to do.   Release all the memory used by the variables and the script and pass the program pointer back to who ever needs it.

Script two:
sh*t, an EOF, now what?  Do I release the memory being used by the local variables, and by the script?  Well the script writer couldn't give a sh*t why should I.

 ;D

I believe that BIS programmers are smart enough to avoid that kind of nonsense.

Hmmm... Lemme think...

BIS programmers ARE smart enough to avoid that kind of nonsense.
« Last Edit: 12 Jul 2005, 19:12:08 by Baddo »

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Life time of a script
« Reply #8 on: 12 Jul 2005, 19:20:51 »
I logged back in to delete my post - but I see you saw the joke.  Good.

I have to say I have a lot of respect for BIS programmers, some of the stuff is amazing (but some of it isn't - or it is amazing for the wrong reason).   The point I was trying to make - perhaps not well - is that exit is a documented instruction so not using exit could leave me open to undocumented features - and I have already had enough of those recently.  On the speed side - that never occurred to me.  My concern at the moment is can I add back 33 @ commands - or would 33 loops be be better, or should I just live without that functionality.  
« Last Edit: 12 Jul 2005, 19:21:20 by THobson »

Offline Baddo

  • Former Staff
  • ****
  • Reservist Jaeger
Re:Life time of a script
« Reply #9 on: 12 Jul 2005, 19:36:20 »
:)

Well I would say that go ahead and put the exit there if you want to, no harm done to anyone. There's obviously much more worse things you can do in a script to slow it down - but technically speaking, exit as the last command in a script file is wrong.

 :P

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re:Life time of a script
« Reply #10 on: 12 Jul 2005, 22:35:59 »
Emm, how exactly you think OFP knows that there's a end of a script??
You can have pages of space between two lines of code and the script doesn't exit during that space...
(haven't tested how many pages, but anyway...)

So to me having a 'exit' at the end of a script sounds much more sane/less 'heavy' than having the engine search the script for a while after the last line until it terminates it thinking it has ended...
(much like THobsons example :P)

And it has to do it like that, how else it would know a script had ended?

Suma said somewhere that OFP does not have a script compiler/parser/whatever so OFP doesn't seem to read through the script before executing it...

But again, I can be wrong...
As I usually am... ::)
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

qqqqqq

  • Guest
Re:Life time of a script
« Reply #11 on: 12 Jul 2005, 22:57:19 »
Quote
exit as the last command in a script file is wrong

What evidence do you have for that statement?  

Quote
It very likely increases the work OFP has to do

Well, does it or doesn't it?

Quote
OFP will for sure notice when the end of the file has been reached

Well, does it or doesn't it?    I know you are trying to be helpful but your post is speculation not information.

There are a lot of unfinished corners in ofp scripting and this could be one of them.   THobson's joke might not be a joke.   Ending with exit could significantly speed things up.   We do know that ofp can cope when a script does not have exit at the end but maybe it copes better if it does.


Quote
writing exit to the last line of your scripts is totally pointless

Wrong.    This is a significant expansion on your original unsupported assertion that writing exit merely slows the game down.    Even if it does slow the game down it indicates to anybody editing the script - including yourself six months later - that the script is finished and there aren't 20 more lines that have been deleted by Windows for no reason and without a trace.    It also makes a comment line "this is the end of the script" superfluous.     It also allows you to add at the end of the file comments or other information useful for explaining, editing or generalising the script.    It is also aethestically more pleasing, thereby making the world a better place.  It also has a pschological benefit since it bestows a sense of completion on the proceedings.    


Quote
instead of thinking like this:

"End Of File reached. Closing now."

How does it know the end of the file has been reached?  The calculations for this may be significant.  

Your assertion is not logically sound.


The large savegame bug could possibly be used to test this although I'm not going to do it.   I will continue to write exit.


Edit
@HateR_Kint you are correct.
« Last Edit: 12 Jul 2005, 22:59:44 by qqqqqq »

Offline Mikero

  • Former Staff
  • ****
  • ook?
    • Linux Step by Step
Re:Life time of a script
« Reply #12 on: 13 Jul 2005, 00:23:12 »
@qqqqqq Q  think you're being a bit harsh on baddo there to make your points

@Hater
>OFP does not have a script compiler

correct. It parses each command (not line) on first come basis

>OFP doesn't seem to read through the script before executing it...

absolutely correct. 'errors' of syntax are not discovered till processed. Goto labels cause the entire script to be searched again from the beginning for 1st occurence of the label.

---
Parsers in general have a pig of a time with EOF. Particularly this

LastCommand<EOF>

LastCommand<Newline>
<EOF>

exit<EOF> is therefore a saving grace.


Just say no to bugz

qqqqqq

  • Guest
Re:Life time of a script
« Reply #13 on: 13 Jul 2005, 00:33:43 »
Quote
qqqqqq Q  think you're being a bit harsh on baddo there to make your points

Apologies.   Harshness not intended.  

Offline Planck

  • Honoured
  • Former Staff
  • ****
  • I'm never wrong ....I'm just not always right !
Re:Life time of a script
« Reply #14 on: 13 Jul 2005, 01:18:56 »
It seems pretty simple to me.

The script is read line by line.

Each line is checked for correct syntax and then the commands or instructions on the line are executed if there were no errors.

Each line is dealt with in a similar fashion.

When there are no more lines to read the script ends.

Alternately, the script ends when it finds an 'exit' command.

I'm not sure whether it starts a search for a goto label from the next line after the goto and once it reaches the end of the file, it starts from the top again........or.......IF it immediately starts from the top looking for the first instance of the label.

I would suspect the latter.


Planck
I know a little about a lot, and a lot about a little.