Home   Help Search Login Register  

Author Topic: Strange OFP behaviour  (Read 661 times)

0 Members and 1 Guest are viewing this topic.

Offline Kuro

  • Former Staff
  • ****
  • 2./FschJgBtl 251 Green Devils
    • VBS2 Mission Download Site
Strange OFP behaviour
« on: 05 Jul 2005, 04:24:14 »
Hallo,

i did a script (and it works perfect):
(KUR_VICTIMS is a array filled with enemies)
"nahe" is another part of the script which is not interesting

_counter = 0
#loop
_seitlich = ((getPos Player select 0) - ((getPos (KUR_VICTIMS select _counter)) select 0))
_entfernung = Player distance (KUR_VICTIMS select _counter)
_rad = (((getPos Player select 1) - ((getPos (KUR_VICTIMS select _counter)) select 1)) / _entfernung)
? _seitlich<0: _winkel = 90 + asin(_rad)
? _seitlich>=0: _winkel = 270 + asin(-_rad)
Hint Format["%1", _winkel]
_Genauigkeit = (_entfernung / (getDir Player - _winkel)^2)
? _Genauigkeit > 0.05 && _entfernung < 40: goto "nahe"
_counter = _counter + 1
? _counter < count KUR_VICTIMS: goto "loop"
exit

the Hint command correctly displays the _winkel
now i have rewritten the script:

_counter = 0
#loop
_seitlich = ((getPos Player select 0) - ((getPos (KUR_VICTIMS select _counter)) select 0))
_entfernung = Player distance (KUR_VICTIMS select _counter)
_rad = (((getPos Player select 1) - ((getPos (KUR_VICTIMS select _counter)) select 1)) / _entfernung)
if (_seitlich<0) then { _winkel = 90 + asin(_rad))} else {_winkel = 270 + asin(-_rad)}
Hint Format ["%1", _winkel]
_Genauigkeit = (_entfernung / (getDir Player - _winkel)^2)
? _Genauigkeit > 0.05 && _entfernung < 40: goto "nahe"
_counter = _counter + 1
? _counter < count KUR_VICTIMS: goto "loop"
exit

Result is that the Hint displays a "scalar ..." error message

Does anybody have an explanation ? For me the two scripts are equal.

Greetings Kuro

Offline Fragorl

  • Coding Team
  • Former Staff
  • ****
Re:Strange OFP behaviour
« Reply #1 on: 05 Jul 2005, 04:51:53 »
:Grins: variable scope.

I recently posted a comment here about this.

It's to do with when you use an 'if....then' instead of an '?...:'

When you use ?...: and initialise a variable (such as _winkel in your case), everything works fine and your variable persists long after the statement has ended.

However when you use if...then, and initialise a variable in between the curly braces, for example
if (_seitlich<0) then { _winkel = 90 + asin(_rad))} else {_winkel = 270 + asin(-_rad)}
the variable _winkel is destroyed as soon as the if statement is complete. In the first script, _winkel is defined outside the if. In the second it is not, simple as that.

To get around this problem, you have to predefine _winkel, like this:

_winkel = false
if (_seitlich<0) then { _winkel = 90 + asin(_rad))} else {_winkel = 270 + asin(-_rad)}

then it should work ok

Offline Kuro

  • Former Staff
  • ****
  • 2./FschJgBtl 251 Green Devils
    • VBS2 Mission Download Site
Re:Strange OFP behaviour
« Reply #2 on: 05 Jul 2005, 05:11:20 »
Wow, thanks.
As a Master of Computer Science i should have thought about this  :-[
I never thought that OFP handles the variable scopes (except local and global).

Offline Fragorl

  • Coding Team
  • Former Staff
  • ****
Re:Strange OFP behaviour
« Reply #3 on: 05 Jul 2005, 05:13:20 »
not a prob, I'm sure you knew that anyway, I only found at that it was applicable by falling into the same trap