Jump to content
Sign in to follow this  
csj

array = array ?

Recommended Posts

How do I work out if

unit1 pos = another object pos or unit pos

I have been trying to use getpos but keep getting err's

help.gif

Share this post


Link to post
Share on other sites

You can compare two arrays using format command to turn the position arrays into strings first. OFP's condition lines accept only number, string, object, side, group and boolean type of variables but not arrays.

Example condition:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">format ["%1",getPos obj1] == format ["%1",getPos obj2]

But I think you will not get very good results by comparing positions, because it is very unlikely that they will have exactly the same values even if the objects look like they are at the same position. They need to be exactly the same if you are going to make your condition to work. Checking if one position is close to another position should work a lot better. You can use the distance command to check distance between two objects. If you measure distance in multiplayer, do it only in the server.

Share this post


Link to post
Share on other sites

Well I take it from the title that you have tried:

if (getPos unit1 == getPos unit2) then {...}

and that has not worked.  Bear in mind that arrays work a bit differently from other things.  If a is an array say:

a = [1,2,3]

then  the instruction b = a does not make a new array, it makes a new pointer to the same array so if you then did:

b = b + [4]

a would then also be: [1,2,3,4]

To make a new array you would need to put a = +b

So what is the point of this?  It is that we should not asusme arrays work the same way as other variables.  I have not tested: if (a == b) then {...} but I take it you have.

Okay so you have tried it with arrays and it does not work you will need to check each element of the array, but now you hit another problem.  These are real numbers and so may be close enough not to make any difference but may still be different.  For example are the following two numbers different:

2.999999

3.000001

?

Well yes they are but do you care?

So you would need to check each element of the array with a tolerance, are they close enough.  Now with positions there is a quick way to do this and that is to use the command distance.  

I suggest you try some thing like:

if (unit1 distance unit2 < 0.5) then {...}

I also suggest you look at:

http://www.ofpec.com/editors/comref.php?letter=D#distance

In fact the whole document is well worth a read

EDIT:

I see Baddo and I were writing at the same time.   smile_o.gif

Share this post


Link to post
Share on other sites

lol

dah! Yeah, the distance thing will do

Time for a sherbet .. I think.

Thanks ppls

Share this post


Link to post
Share on other sites

Or, if you still need it though:

fArrayCompare.sqf

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">private["_a1","_a2","_r","_i"];

_r=true;

if((count _a1)!=(count _a2))then{_r=false}

else{

_i=0;

while{_i<count _a1 && _r}do{if((_a1 select _i)!=(_a2 select _i))then{_r=false}};

};

_r

Usage:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_ar1 = [1,2,3]

_ar2 = [1,2,3]

? ([_ar1,_ar2] call fArrayCompare) : hint "_ar1 equals _ar2"

Share this post


Link to post
Share on other sites

hardrock, doesn't that line I posted do the same thing a bit easier...? Just convert arrays to strings and then compare. Works for all kinds of arrays.

Share this post


Link to post
Share on other sites

hardrock: That is unlikely to be a reliable method for comparing arrays that have real numbers or arrays as their elements.  It should work well though for arrays that contain integers and objects.

EDIT:

Baddo and I should enter a synchronised typing competition.

Share this post


Link to post
Share on other sites
hardrock, doesn't that line I posted do the same thing a bit easier...? Just convert arrays to strings and then compare. Works for all kinds of arrays.

Correct, only that strings have length limitations. With larger array of objects you'll soon hit the limit.

@THobson: Now that you say it, I notice it too. Just copied this (a bit older) function, but maybe I should revise it.

Share this post


Link to post
Share on other sites
hardrock, doesn't that line I posted do the same thing a bit easier...? Just convert arrays to strings and then compare. Works for all kinds of arrays.

OFP has a limit on how big it can make strings (1024 or something?). If you try to 'format' an array and the resulting string is longer than that, it will crash the game. So for arrays that you know are going to be of a small enough size, format works fine. But otherwise you should use the posted function.

Share this post


Link to post
Share on other sites
hardrock, doesn't that line I posted do the same thing a bit easier...? Just convert arrays to strings and then compare. Works for all kinds of arrays.

OFP has a limit on how big it can make strings (1024 or something?). If you try to 'format' an array and the resulting string is longer than that, it will crash the game. So for arrays that you know are going to be of a small enough size, format works fine. But otherwise you should use the posted function.

I think the limit is (or should be) 2048 characters per string.

I did a test like this:

1. increment string to hold 2048 characters

2. call format ["%1",string] ---> OK, no errors

3. increment string up to 2055 characters, ---> OK no errors with call format ["%1",string]

4. increment string to 2056 characters, error message displayed on screen, but no CTD still...

5. increment string up to 2059, same error message but no CTD

6. increment string to 2060, finally a CTD smile_o.gif with call format ["%1",string]

I tested this a couple of times and everytime the result was same.

Hmmm... what to say from this... I would say don't exceed 2048 chars in a string. It is possible that when you use some other functions which take a string as argument, you can get different results for the accepted string length until error messages or CTD appear. I only tested format so that's only a guess.

But yes, for comparing two position arrays format ["%1",getPos obj1] == format ["%1",getPos obj2] is an ideal solution as I see it. Of course BIS could overload the == operator also for comparing arrays so we could compare arrays and especially long arrays more easily... if it would be CPU intensive then so what? It's the scripter who uses it excessively, not the programmers of the game right?

smile_o.gif

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  

×