Home   Help Search Login Register  

Author Topic: Loops and getpos  (Read 486 times)

0 Members and 1 Guest are viewing this topic.

Benson

  • Guest
Loops and getpos
« on: 01 Apr 2004, 18:35:15 »
Hi all,
I have an question regarding something i'm learning.
In pre, "Yes i know those scripts are out there, no i won't get them, yes i wan't to learn it myself".

So the script i have:

Code: [Select]
#infinite
_lastpos = getpos hoofdpersoon
_cam camsettarget hoofdpersoon
_cam camsetrelpos [1,5,1.7]
_cam camcommit 1
@camcommitted _cam
_newpos = getpos hoofdpersoon
_diff = _lastpos - _newpos
?_diff == 0 : goto "end"
goto " infinite"
#end
endintro = true

(thnx for snypir's tute for learning me this beautiful sort of scripting :D)

The problem is that if the player stands still the loop should be broken by going to " end"
Why isn't this happening ?

[edit]
Another thing :D
Isn't it possible to do this :
_cam camsetrelpos [_lr,_fr,_alt] ??
How must this be solved ?
[/edit]
« Last Edit: 01 Apr 2004, 19:08:29 by Benson »

Offline Artak

  • The old beanbag shaker
  • Former Staff
  • ****
  • You want to talk about it, yes?
    • OFP Team Finlanders
Re:Loops and getpos
« Reply #1 on: 01 Apr 2004, 20:02:31 »
Hi!


Getpos giving an array of three items (x,y,z), I don't think you can use the minus thing on them.
Meaning
_lastpos can be [1000,2000,1]
and _newpos can be [1100,2100,2]
so what would [1000,2000,1] - [1100,2100,2] result be?

try this

Code: [Select]
#infinite
_lastpos = getpos hoofdpersoon
_cam camsettarget hoofdpersoon
_cam camsetrelpos [1,5,1.7]
_cam camcommit 1
@camcommitted _cam
_newpos = getpos hoofdpersoon
_newx = _newpos select 0
_oldx = _lastpos select 0
_diff = _newx - _oldx
?_diff == 0 : goto "end"
goto " infinite"
#end
endintro = true

what this does is it that it selects a single item from the getpos array (x), and thus compares if the first values in both arrays are equal.



_cam camsetrelpos [_lr,_fr,_alt] is possible. You'll just first define _lr, _fr and _alt the way you want them to be. For example

_lr = 5
_fr = -8
_alt = 3
_cam camsetrelpos [_lr,_fr,_alt]
« Last Edit: 01 Apr 2004, 20:04:32 by Artak »
Not all is lost.

Benson

  • Guest
Re:Loops and getpos
« Reply #2 on: 01 Apr 2004, 21:58:59 »
But it is possible to beat it by walking on the x line sin't it ?

_oldpos = [1000,2100,1]
_newpos=[1000,1000,1]

Can that be fixed ?

And thnx for the second answer.

Offline Calamity

  • Former Staff
  • ****
  • Calamity Strikes AGAIN!
    • DataCraft Enterprises
Re:Loops and getpos
« Reply #3 on: 01 Apr 2004, 22:03:41 »
I'll focus on the argument of the comparator since that's where the real problem is.
It is possible to subtract arrays:

From the Command Ref on www.flashpoint1985.com, downloads sections
arrayA - arrayB
Operand types:
    arrayA: Array
    arrayB: Array
Type of returned value:
    Array
Description:
    all elements in arrayB removed from arrayA

Example:
    [0, 1, 2, 4, 0, 1, 2, 3, 4, 5] - [1, 2, 3] , result is [0, 4, 0, 4, 5]


Based upon this definition of Array Subtraction I don't think that a direct array comparison to 0 will yield a "true" condition. Therefore, instead of:

_diff = _lastpos - _newpos
?_diff == 0 : goto "end"

You can try the following example. But the empty values may cause some unpredictable results, since what you are really looking for is an empty array…

_diff = _lastpos - _newpos
? (_diff select 0 == ‘') and (_diff select 1 == ‘') and (_diff select 1 == ‘') : goto "end"

This next example makes for a messier chunk of code but does illustrate how flexible the scripting language is, and I can almost guarantee that it will work.

? ((_lastpos select 0 - _newpos select 0) == 0) and (_lastpos select 1 - _newpos select 1) == 0) and ((_lastpos select 2 - _newpos select 2) == 0) : goto "end"

Now, since you mentioned a dude that has stopped running You might want to simplify the above statement by just using the X and Y values from the array. Like this:

? ((_lastpos select 0 - _newpos select 0) == 0) and (_lastpos select 1 - _newpos select 1) == 0 : goto "end"

Hope this helps

Calamity out…

Offline Artak

  • The old beanbag shaker
  • Former Staff
  • ****
  • You want to talk about it, yes?
    • OFP Team Finlanders
Re:Loops and getpos
« Reply #4 on: 02 Apr 2004, 05:29:19 »
or use the unitready command, which detects when the unit is ready == has stopped moving  :D
Not all is lost.

Offline ACF

  • Members
  • *
  • Llama?? Ain't that French for tanks?
Re:Loops and getpos
« Reply #5 on: 02 Apr 2004, 14:47:25 »
Or, as you're looking for no change:

?(_newpos == _lastpos): Goto "end"

However, this is looking for an absolutely zero difference and I suspect that a stopped unit looking around may result in an negligible but non-zero difference:

? (Abs(_lastpos select 0 - _newpos select 0) < _distdiff) OR Abs(_lastpos select 1 - _newpos select 1) < _distdiff): goto "end"

The value of _distdiff will depend on how fast the loop runs. If you're checking every second, perhaps 0.1 would be OK (i.e. a unit moving less than 0.1 m/s is 'stopped')

I have a few thoughts on the original script which may or may not be relevant:

No exit command

Typo: Goto " infinite" should be Goto "infinite"

You should be able to put _cam camsettarget hoofdpersoon above the #infinite loop as the target is constant.