Jump to content
Sign in to follow this  
Prospero

Variables that won't add...

Recommended Posts

Hi all,

I've noticed the following problem a number of times when programming scripts in OFP. However, it has now become a pressing problem which I need to overcome!

Depending on how you create a variable containing a number value, it seems that some variables cannot be added - you get a scalar bool array string message. I really don't understand this.

For example, let's say we're in a fast loop and we have a variable (which we'll call _myvariable) which is getting updated every cycle with a new value. Now, let's say we need to keep a running total of all the values stored in _myvariable, so we'll have another variable to store the total (we'll call this variable _total).

First off (before entering the loop), we'll assign both _myvariable and _total zero values, to be on the safe side:

_myvariable = 0

_total = 0

And let's display what's going on by using the Hint command, so in our loop we'll have:

hint format ["%1\n%2\n", _myvariable, _total]

So now, in the loop, we'll have this:

_total = _total + _myvariable

Very oddly, _myvariable is displayed just fine, but _total comes out as a scalar bool array string message.

If, however, I try to display something like this...

_myvariable * 5

... this works fine. Anyone know what's going on?

Prospero

Share this post


Link to post
Share on other sites

Try make a new variabled called: valtotal and then:

_valtotal = _total

_total = _valtotal + _myvariable

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (SantaMania @ Oct. 24 2002,14:52)</td></tr><tr><td id="QUOTE">Try make a new variabled called:  valtotal and then:

_valtotal = _total

_total = _valtotal + _myvariable<span id='postcolor'>

Yes, I tried that, along with other stuff, like doing...

_myvariable = _myvariable * 1

... before trying to total _myvariable.

Nothing works!

I also tried doing other stuff to _myvariable, like:

_myvariable = _myvariable % 1

And:

_myvariable = abs(_myvariable)

But in all cases, though I can get _myvariable displayed after carrying out these operations on it (just as I could without performing them), totalling _myvariable just doesn't seem possible!

There must be a solution... some sort of conversion.. or.. well I simply don't understand this. It's mad.

Prospero

Share this post


Link to post
Share on other sites

Have you also tried it without using local script variables?

total instead of _total

Maybe this little difference can make it to work properly -

can't test it for myself right now (@ work)

~S~ CD

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Prospero @ Oct. 24 2002,14:14)</td></tr><tr><td id="QUOTE">Depending on how you create a variable containing a number value, it seems that some variables cannot be added - you get a scalar bool array string message. I really don't understand this.

And let's display what's going on by using the Hint command, so in our loop we'll have:

hint format ["%1\n%2\n", _myvariable, _total]

Very oddly, _myvariable is displayed just fine, but _total comes out as a scalar bool array string message.<span id='postcolor'>

Can you post your real script done showing this problem here? It really sounds like _total is not initialized correctly (probably initialized using another unitialized value somewhere in the expression). Without seeing real script it is hard to guess more.

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (DV Chris Death @ Oct. 24 2002,15:27)</td></tr><tr><td id="QUOTE">Have you also tried it without using local script variables?

total instead of _total

Maybe this little difference can make it to work properly -

can't test it for myself right now (@ work)

~S~ CD<span id='postcolor'>

Yup, nice idea, Chris. Sadly that doesn't work either. Though in my dim and distant memory I seem to recall that some things only start to work when you use globals. Don't know why. Sadly though, this doesn't seem to be one of them.

Suma: this is all to do with my ProSphere Controller program (there's another topic on this board describing it). The Controller basically outputs a global array called PS made up of six elements. Here is the script (PSs.sqs) where PS is set up:

----------

; There is no spoon.

PSop = [0, 0, 0, 0]

PSfp = [0, 0, 0, 0]

PSin = [0, 0, 0, 0, 0]

PSr = [0, 0, 0, 0, 0]

PS = [0, 0, 0, 0, 0, 0]

PSc setpos PSo

PSc setdir 180

[] exec "PS.sqs"

_ot = Time

#lp

~0.0001

_nt = Time

_dt = (_nt - _ot) / acctime

?(_dt == 0): goto "lp"

PSin = [-(velocity PSc select 0), -(velocity PSc select 1), (velocity PSc select 2), (getdir PSc) - 180, PSop, PSfp]

_psop = PSin select 4

_psfp = PSin select 5

_z = ((_psfp select 3) - (_psop select 3))

_ad = sqrt(((_psfp select 0) - (_psop select 0))^2 + ((_psfp select 1) - (_psop select 1))^2)

?(_ad == 0): goto "sb"

_b = atan(_z / _ad)

#sp

PSr = [PSin select 0, PSin select 1, PSin select 2, (PSin select 3) / _dt, _b / _dt]

PSc setpos PSo

PSc setdir 180

_ot = _nt

goto "lp"

#sb

?(_z == 0): _b = 0; goto "sp"

?(_z > 0): _b = 90; goto "sp"

_b = -90; goto "sp"

----------

Edit: Oops! And here is the script (PS.sqs) where PS is loaded:

----------

; /--------------------------------------------------/

; /**************************************************/

; / Default ProSphere Controller Lite configuration. /

; /**************************************************/

; /--------------------------------------------------/

; Turn ProSphere ON or OFF. (This is only the default setting. It can be changed at any time).

; 0 is OFF. 1 is ON.

PSon = 0

; Display the ProSphere Widget when ProSphere is ON.

; 0 if OFF. 1 is ON.

PSwid = 1

; Display the ProSphere debugging data window when ProSphere is ON.

; 0 is OFF. 1 is ON.

_dbg = 0

; Set up the behaviour of the MoveLock Camera for when MoveLock is ON. A six element array is used.

; Position 0 - Relative X.

; Position 1 - Relative Y.

; Position 2 - Relative Z.

; Position 3 - Transition time.

; Position 4 - Name.

; Position 5 - Position.

PScam = [0, -3, 1, 0, "internal", "back"]

; Specify the Deadzone for each axis.

; Typical range is 0 (none) to 1 (large).

_dzX = 0.01

_dzY = 0.01

_dzZ = 0.01

_dzA = 0.01

_dzB = 0.01

_dzC = 0.01

; Specify the Self-Centering Spring Force for each axis.

; Typical range is 0 (no Self-Centering) to 1 (high).

_sfX = 0.5

_sfY = 0.5

_sfZ = 0.5

_sfA = 0.5

_sfB = 0.5

_sfC = 0.5

; Specify Deflection Speed for each axis.

; Typical range is 0 (zero Deflection Speed) to 1 (high).

_dsX = 0.5

_dsY = 0.5

_dsZ = 0.5

_dsA = 0.5

_dsB = 0.5

_dsC = 0.5

; /------------------------------------------------------------------------------------------

------/

; /******************************************************************************************

******/

; / End of default ProSphere Controller Lite configuration. No further configuration is necessary. /

; /******************************************************************************************

******/

; /------------------------------------------------------------------------------------------

------/

_x = 0

_y = 0

_z = 0

_a = 0

_b = 0

_dx = 0

_dy = 0

_dz = 0

_da = 0

_db = 0

_dc = 0

_dd = 0

_jx = 0

_jy = 0

_jz = 0

_ja = 0

_jb = 0

_jc = 0

_jd = 0

_nt = Time

_dt = 0

_cl = 0

_h = [0, 0, 0]

_pspml = 0

_ga = 0.1

_gb = 60

_gc = 0

_rgX = 4

_rgY = 4

_rgZ = 4

_rgA = 4

_rgB = 4

_rgC = 4

_sgX = 2

_sgY = 2

_sgZ = 2

_sgA = 2

_sgB = 2

_sgC = 2

_dsX = _dsX * _sgX

_dsY = _dsY * _sgY

_dsZ = _dsZ * _sgZ

_dsA = _dsA * _sgA

_dsB = _dsB * _sgB

_dsC = _dsC * _sgC

_sfX = _sfX * _ga

_sfY = _sfY * _ga

_sfZ = _sfZ * _ga

_sfA = _sfA * _ga

_sfB = _sfB * _ga

_sfC = _sfC * _ga

_ot = Time

#lp

~0.0001

_nt = Time

?(PSon == 0): PS = [0, 0, 0, 0, 0, 0]; _jx = 0; _jy = 0; _jz = 0; _ja = 0; _jb = 0; _jc = 0; _jd = 0; _h = [0, 0, 0]; PSwi = 0; _pspml = 0; _dt = (_nt - _ot) / acctime; _cl = _cl + _dt; _ot = _nt; goto "lp"

_dt = (_nt - _ot) / acctime

_cl = _cl + _dt

_gc = _gb * _dt

_psr = PSr

_x = (_psr select 0) + 60

_y = (_psr select 1) + 60

_z = (_psr select 2) + 60

_a = (_psr select 3) + 180

_b = (_psr select 4) + 180

_x = (_x + 0.5 - ((_x + 0.5) % 1))

_y = (_y + 0.5 - ((_y + 0.5) % 1))

_z = (_z + 0.5 - ((_z + 0.5) % 1))

_a = (_a + 0.05 - ((_a + 0.05) % 0.1))

_b = (_b + 0.05 - ((_b + 0.05) % 0.1))

_dx = (_x - 60) * _dt * _dsX

?(abs(_x - 60) == 4): _jx = _jx + _dx

_dc = 4 * _dt * _dsC

?(_x > 64): _jc = _jc + _dc

?(_x < 56): _jc = _jc - _dc

_dy = (_y - 60) * _dt * _dsY

?(abs(_y - 60) == 4): _jy = _jy + _dy

_dd = 4 * _dt

?(abs(_y) > 64): _jd = _jd + _dd

_dz = (_z - 60) * _dt * _dsZ

_jz = _jz + _dz

_da = ((_a - 180) / 20) * _dt * _dsA

_ja = _ja + _da

_db = ((_b - 180) / 20) * _dt * _dsB

_jb = _jb + _db

_h = [_jd] + _h

_h resize 3

?(_h select 0 != _h select 1) && (_h select 1 == _h select 2): goto "sb1"

#rn1

?(_x == 60): goto "sb2"

#rn2

?(_y == 60): goto "sb3"

#rn3

?(_z == 60): goto "sb4"

#rn4

?(_a == 180): goto "sb5"

#rn5

?(_b == 180): goto "sb6"

#rn6

?(_jx >= _rgX): _jx = _rgX

?(_jx <= -_rgX): _jx = -_rgX

?(_jc >= _rgC): _jc = _rgC

?(_jc <= -_rgC): _jc = -_rgC

?(_jy >= _rgY): _jy = _rgY

?(_jy <= -_rgY): _jy = -_rgY

?(_jz >= _rgZ): _jz = _rgZ

?(_jz <= -_rgZ): _jz = -_rgZ

?(_ja >= _rgA): _ja = _rgA

?(_ja <= -_rgA): _ja = -_rgA

?(_jb >= _rgB): _jb = _rgB

?(_jb <= -_rgB): _jb = -_rgB

PS = [_jx / _rgX, _jy / _rgY, _jz / _rgZ, _ja / _rgA, _jb / _rgB, _jc / _rgC]

?(_dbg == 1): hint format [" ProSphere Controller Lite\n\n X-Axis  %1\n Y-Axis  %2\n Z-Axis  %3\n A-Axis  %4\n B-Axis  %5\n C-Axis  %6\n\n Dt  %7\n Clock  %8\n Time  %9\n\n", PS select 0, PS select 1, PS select 2, PS select 3, PS select 4, PS select 5, _dt, _cl, Time]

_ot = _nt

goto "lp"

#sb1

?(_pspml == 0): _pspml = 1; showcinemaborder false; PSv cameraeffect [PScam select 4, PScam select 5]; goto "sp1"

_pspml = 0

PSv cameraeffect ["terminate", PScam select 5]

#sp1

goto "rn1"

#sb2

?(abs(_jx) > _dzX): _jx = _jx - _sfX * _jx * _gc; goto "sp2"

_jx = 0

#sp2

?(abs(_jc) > _dzC): _jc = _jc - _sfC * _jc * _gc; goto "rn2"

_jc = 0

goto "rn2"

#sb3

?(abs(_jy) > _dzY): _jy = _jy - _sfY * _jy * _gc; goto "rn3"

_jy = 0

goto "rn3"

#sb4

?(abs(_jz) > _dzZ): _jz = _jz - _sfZ * _jz * _gc; goto "rn4"

_jz = 0

goto "rn4"

#sb5

?(abs(_ja) > _dzA): _ja = _ja - _sfA * _ja * _gc; goto "rn5"

_ja = 0

goto "rn5"

#sb6

?(abs(_jb) > _dzB): _jb = _jb - _sfB * _jb * _gc; goto "rn6"

_jb = 0

goto "rn6"

----------

I discovered the problem when trying to keep running totals on the six elements of PS (which I had not attempted to do prior to releasing ProSphere).

(I'm hoping that OFP Info will put ProSphere Controller on their site today for download)

I can just see lots of people asking about this when they do get hold of the software, so I'm trying to find a fix before everyone shouts at me!wink.gif

Prospero

Share this post


Link to post
Share on other sites

Thank you for posting the script. Now if you would also tell me at which particular point in the script you have the problems, it might be easier for me to see what can cause it.

Share this post


Link to post
Share on other sites

Hi Suma,

1) I'm sure this is all my own fault! I'm just waiting for someone to point out to me the really obvious thing I've done wrong wink.gif

2) The only way I can see of showing you this is by sending you the complete functioning script (set of scripts). Sadly, OFP Info have not posted it yet, so... I'm not sure what to do. My email is awebber@tiscali.co.uk. If there's a way I might send it to you...?

3) If this is possible, the problem can be easily recreated by simply attempting to total any of the elements in PS in an (external) script running in parallel.

Yes, it's no doubt all my fault, but I'm not sure what else to do.

Prospero

Share this post


Link to post
Share on other sites

One note that might help: undefined values have tendency to propagate themselves.

If you will do something like

_a = _b + _c + _d + _e

if any variable in this expression is undefined, _a will be undefined as well.

Share this post


Link to post
Share on other sites

Just a question

a = 100000000

or

_(same)

No problem with scientific notation or something else?

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Suma @ Oct. 24 2002,21:15)</td></tr><tr><td id="QUOTE">One note that might help: undefined values have tendency to propagate themselves.

If you will do something like

_a  = _b + _c + _d + _e

if any variable in this expression is undefined, _a will be undefined as well.<span id='postcolor'>

Of course you were right, Suma!

And yet again I continue to amaze myself with my stupidity. So pleased was I to finish the ProSphere thing, that then, when running the totalling script in parallel with it, I omitted to give ProSphere sufficient time to fire up. This meant the first few values being totalled were undefined, and hence, so was the total.

Solved.

Thanks Suma;)

Prospero

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×