Jump to content
genesis92x

Optimization Question

Recommended Posts

Hello everyone, I have a question that sounds rather straight forward but...now I am not sure.

 

I have written a function that is supposed to find the furthest or closest pair of things from two arrays. I have not tested this at all, I just wrote it up now.

 

Dis_AComp

//This function will compare two arrays and find the objects that are closest to one another.
//This function will aim to be as optimized as possible but should not be over-utilized as it will do MANY calculations in a short period of time.
//For the return variable _this select 0 will be the distance, _this select 1 will be the object from array1, and _this select 2 will be the object from array 2.

private ["_List1", "_List2", "_Array1", "_Array2", "_Order", "_Array1L", "_O", "_object", "_pos2", "_ND", "_DistanceArray", "_order"];
//[_List1,_List2,false] call dis_ADistC;
_Array1 = _this select 1;
_Array2 = _this select 2;
_Order = _this select 3;


_Array1L = [];
{
	//Pull and remember the first object from array1
	_O = _x;
	
	//Find out if the object in the array is an OBJECT, STRING, or ARRAY. Then grab its position
	if (TypeName _O isEqualTo "OBJECT") then {_pos = getposWorld _object;} else { if (TypeName _O isEqualTo "STRING") then {_pos = getMarkerPos _O} else {_pos = _O}; };
	
	//Now we need to check EVERY OTHER OBJECT in the 2nd array.
	{
		//Find out if the object in the array is an OBJECT, STRING, or ARRAY. Then grab its position
		if (TypeName _x isEqualTo "OBJECT") then {_pos2 = getposWorld _object;} else { if (TypeName _x isEqualTo "STRING") then {_pos2 = getMarkerPos _x} else {_pos2 = _x}; };
		
		//Find the distance between the 2 points
		_ND = _pos distance _pos2;
		
		//Put them into an array
		0 = _Array1L pushback [_ND,_O,_x];
	} count _Array2;
	
} count _Array1;

//Since the distance is the first in the array, it will sort based on that. False = shortest on top. True = furthest on top.
_DistanceArray sort _order;

//Finally lets spit out the final array. With this, we will return the whole array. This may be useful in the future for finding "close enough" targets or groups.
_DistanceArray

 

It is not the lightest of functions as it needs to figure out if what in the array is a marker, object, or something else... and it does the dreaded { {} foreach X } foreach X

 

I have two main questions:

1) Do you see anyway to make this faster/more optimized?

2) For my mission I will be using this function to figure out which blufor unit/building/marker is closest to which opfor unit/building/marker. Instead of having me execute the function every time I need it like...

 

_Rawr = [_Array1,_Array2,false] call Dis_AComp;

So maybe, on the heavy side, the function gets ran 4-5 times every 60 seconds as scripts needs it.

 

Would it be better to make it non-broadcasted global variable instead? Like creating a loop that runs every 30 seconds or so (it does not need to be precise).

 

Rawr = [_Array1,_Array2,false] call Dis_AComp;

So then every time a script needed that information they could just pull the variable "Rawr" instead of computing the function again?

 

I am actually unaware of how demanding it is to constantly be re-writing a global variable with an array that could be potentially...huge.

 

 

Summed up:


Is it better/faster to have a function run _locally several times, or as needed. Or to have it be a non-broadcasted global variable that gets redefined every 30 seconds or so.

It seems like it would actually be more FPS saving to have it ran only once every 30 seconds and be set to a global variable than to run it as needed _locally.

 

Thoughts?

Share this post


Link to post
Share on other sites
45 minutes ago, fn_Quiksilver said:

 

Oh wow, that's new. Thank you! That's one way to optimize it :) My original question still stands :<

I wonder how you could get it to work with objects though...

 

Edit: 

Oh wait. Objnull works for testing if an object is an object. Man I don't understand that.

Share this post


Link to post
Share on other sites
22 minutes ago, genesis92x said:

 

 

Oh wait. Objnull works for testing if an object is an object. Man I don't understand that

 

Learn OOP.

 

 

Share this post


Link to post
Share on other sites
2 minutes ago, djotacon said:

 

Learn OOP.

 

 

I can probably learn a few basic things...but I am not a programmer, I do this in my spare time. So I will have to pass for now.

That's like me asking my clients to learn the entirety of developmental therapy before I can help them. 

Share this post


Link to post
Share on other sites
Just now, genesis92x said:

I can probably learn a few basic things...but I am not a programmer, I do this in my spare time. So I will have to pass for now.

 

Good for you, the ignorance is a great idea.

 

 

Share this post


Link to post
Share on other sites
4 minutes ago, djotacon said:

 

Good for you, the ignorance is a great idea.

 

 

 

I am sorry, you might be misunderstanding. I am just communicating that learning OOP is not something essential to my life nor my hobby, and is not what I asked in the thread. I am not asking you to explain it to me, I am asking you to actually post helpful information or post nothing at all.

 

I was looking for a helpful and friendly lesson/discussion...not to be told to go learn something that people spend their own careers learning.

  • Like 2

Share this post


Link to post
Share on other sites

Instead of arguing learn OOP and you can have more fun creating your scripts but without OOP you are lost.

 

Share this post


Link to post
Share on other sites
2 minutes ago, djotacon said:

Instead of arguing learn OOP and you can have more fun creating your scripts but without OOP you are lost.

 

 

Oh! You are just a troll. My bad - I won't respond anymore. Have a nice weekend!

  • Like 2

Share this post


Link to post
Share on other sites

Without OOP you are using only the 50% of the SQF - or even more - but dont listen to me and live you life as you want.

 

Wait a minute are you insulting me because you are asking help about a command that need learn the basic of the OOP to understand how to work?

 

I forgot why the career students are arrogant and fools, my bad too.

 

Enjoy you weekend too.

Share this post


Link to post
Share on other sites
2 hours ago, genesis92x said:

 

Oh wow, that's new. Thank you! That's one way to optimize it :) My original question still stands :<

I wonder how you could get it to work with objects though...

 

Edit: 

Oh wait. Objnull works for testing if an object is an object. Man I don't understand that.

 

They are both of the "Object" data type. ObjNull just represents an object that doesn't exist in the game world.

 

Essentially OOP is where data structures are organized in a hierarchical pattern with each entity inheriting the properties of those that came before it

 

Object --> ObjNull

           --> CAManBase

           --> AllVehicles

 

All are type objects because they inherit from an object.

The config viewer is a great way to observe this in-game.

 

Would recommend some light googling on OOP if that confuses you. The concept is rather simple once you grasp what it is, and it does help explain how some things work in Arma.

Share this post


Link to post
Share on other sites

genesis92x

// Usage: [_array1, _array2, _distanceFunc] call FindClosestObjects;
FindClosestObjects = {
	params ["_arx", "_ary", "_fnc"];
	private _res = [];
	private _dst = 1e39;	
	{	private _y = _x;
		{	private _dsc = call _fnc;
			if (_dsc < _dst) then {
				_res = [_x, _y];
				_dst = _dsc
			}
		} forEach _arx
	} forEach _ary;
	_res
};

 

Tests:

AssertEquals = {
	params ["_obx", "_oby", "_tsn"];
	private _res = _obx isEqualTo _oby;
	systemChat format ["Test %1 - %2 (%3)", _tsn, ["FAILED", "SUCCEEDED"] select _res, _obx];
	_res	
};

Test = {
	private _a = [1000, 3000, 5000, 7000, 9050];
	private _b = [1030, 3020, 5010, 7040, 9000];
	
	[[_a, _b, {abs(_x - _y)}] call FindClosestObjects, [5000, 5010], "A"] call AssertEquals;
	[[_b, _a, {abs(_x - _y)}] call FindClosestObjects, [5010, 5000], "B"] call AssertEquals;
};

// Output (call Test):
// Test A - SUCCEEDED ([5000, 5010])
// Test B - SUCCEEDED ([5010, 5000])

 

Share this post


Link to post
Share on other sites

In your case distance function can be (cover all argument types: objects, markers, coordinate arrays):

GetDistance = {
	private _psx = if (_x isEqualType "") then {getMarkerPos _x} else {_x};
	private _psy = if (_y isEqualType "") then {getMarkerPos _y} else {_y};
	_psx distance _psy
};
// Usage:
// private _closestPair = [_array1, _array2, GetDistance] call FindClosestObjects;

 

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

×