Jump to content
Sign in to follow this  
KaRRiLLioN

Updated Nested arrays quickly

Recommended Posts

I've been through this a dozen times with OFP, and I had hoped the find command or something might work with nested arrays, but it apparently doesn't.

Let's say I have a nested array:

mainArray = [[_x,_y,_z],[_xx,_yy,_zz]]

and I want to remove [_xx,_yy,_zz] from that array.

mainArray Find [_xx,_yy,_zz] is -1, i.e. null.

mainArray  = mainArray - [_xx,_yy,_zz] still returns the same array.

So the only way to really update this array is by using the set command.  

mainArray set [(mainArray select 1) "Delete"]

mainArray = mainArray - ["Delete"]

And to use that, I have to find the position in the array that I want to update which requires a loop.  A loop means I need to make another function to take care of that which is a PITA.

So, is there a miracle command I don't know about that can do this in real time, or do I need to make another function to take care of this?

Share this post


Link to post
Share on other sites

Im supossing this:

You want to take ride of null results or objects inside an array.

So, in an original state with 0 objects in your array, you have to start this loop.

1st. To count the size of the array, save it in a variable or so.

2nd. Detect when the new item is null.

3rd. Quit 1 to the variable that is saving the count result.

4rd. Resize array with the value of the count variable.

5th. Loop to 1st step.

Just break it when you wanting.

Share this post


Link to post
Share on other sites

Nope, there's no command you're overlooking... sad_o.gif

Share this post


Link to post
Share on other sites
Quote[/b] ]So the only way to really update this array is by using the set command.  

mainArray set [(mainArray select 1) "Delete"]

mainArray = mainArray - ["Delete"]

And to use that, I have to find the position in the array that I want to update which requires a loop.  A loop means I need to make another function to take care of that which is a PITA.

So, is there a miracle command I don't know about that can do this in real time, or do I need to make another function to take care of this?

It depends on what is associated with your arrays. For example, if it was data linked with say a tank and you want to remove it when the tanks is destroyed, then there are plenty of ways of speeding up the whole process.

If you can expand on what sort of arrays and data your working with, I'm sure I can come up with a faster method than looping. Just about all my scripting works around arrays and making them as efficent as possible.

Share this post


Link to post
Share on other sites

It's an array with several values for territory control that updates over time as the territory value is upgraded.

I'm almost positive, as is apparently raedor, that I'll have to use a function for this.

Here's a sample array-- and keep in mind these are all dynamic.

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_tArr = format ["[[""%2"",1,%1Transport1 + %1Support,""%3""]]",_flagSide,_bldName,_varName]

So it could look like:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

["Flag1 Base Level 1",[["HMMWV","Hummer","vcl",10,2,2,1]],"eastBuildingFlag3"]

Then over time, the base upgrades and changes to level 2:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

["Flag1 Base Level 2",[["M1Abrams","M1A2 Abrams","vcl",10,2,2,1]],"eastBuildingFlag3"]

So really the only thing changing is the vehicles available and the name of the territory base.

I whipped up a function that I haven't tested yet, but I think it'll do what I need.  I don't use them often as I should and haven't used a pure function for ArmA yet, so I'm not even sure my syntax is correct.

Looks like this:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

mainArray = [mainArray,_tArr] call compile preprocessfile "updateArray.sqf"

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

private ["_x","_newArray","_newName","_n"];

_mArray = _this select 0;

_newArray = _this select 1;

_newName = _newArray select 3;

player sideChat format ["%1",_newArray];

_n = count _mArray;

_x = 0;

_set = FALSE;

while {_x < _n} do

{

_name = _mArray select _x select 3;

player groupChat format ["%1 | %2",_newName,_name];

if (_name == _newName) then {_mArray set [_x,_newArray]; _set = TRUE};

_x = _x + 1;

};

if (!_set) then {_mArray = _mArray + [_newArray]};

_mArray

Share this post


Link to post
Share on other sites
Quote[/b] ]I'm almost positive, as is apparently raedor, that I'll have to use a function for this.

While functions offer the reasurance of instant results, the key thing is tapping into the object or instance that instigates the event. I would guess, in this case, your using triggers?

Share this post


Link to post
Share on other sites

Yes, for now. I have a server-side script that keeps count of a trigger which activates on Anything present and counts the sides. I was using nearestobjects, but since that's broken in MP right now, I had to use this instead. I'd hoped to go completely trigger-less, but that'll have to wait for 1.06 I suppose.

So the server-side script that monitors the territories checks for a specific vehicle and then builds a base depending on the support vehicle driven there. The base then upgrades provided the side holds it over time and with it, the units that can be built. For that, this server-side function will update the array and then publicVariable a string which a client-side script will convert to the updated array.

I plugged it into my script and it seems to work pretty well. I'd bet I could use some functions in other places as well once I get around to optimizing.

Share this post


Link to post
Share on other sites

Actually, using a function isn't that bad.

Since "call" stops everything, you'll have full dedication towards searching the array. Even if there was a "miracle function" in the command list it would do nothing more than loop through the array and set the proper value just like your function would.

What you are looking for has been one of those holy grails of programming... which algorithm to use? forward, reverse? etc..

I would look into what you consider would to be fastest search and build your own function.

On the other hand, there is a fast way simply by keeping an index. For example, in SoW we had huge arrays of weapons and such to track all kinds of values and arrays within arrays. Before start, I built an index of a one dimensonal array so I knew it was the fastest way to search. Of course, if you have multiple keys you'll need multiple indexes. However, a good structured array and knowing where to look (index) is going to be the best start.

Share this post


Link to post
Share on other sites
Quote[/b] ]Actually, using a function isn't that bad.

Since "call" stops everything, you'll have full dedication towards searching the array. Even if there was a "miracle function" in the command list it would do nothing more than loop through the array and set the proper value just like your function would.

Yeah, looping through an array using a function is quite fast, if you have a fixed number, say 30 elements in the array then you probably won't see any delay. You could also maintain two arrays, one that holds just a list of triggers and is searched using find. That gives you the index for your array that contains all the other info you want to update.

The problems I faced are having an array of say all the vehicles\objects built during a CTI, if the game goes on for a couple of hours then the array starts to get into the hundreds if not more.

But if you wanted to keep things as efficient as possible there are some things you can do. Unfortunately all the really handy commands like setVariable e.t.c will not work with triggers.

Quote[/b] ]So the server-side script that monitors the territories checks for a specific vehicle and then builds a base depending on the support vehicle driven there.  The base then upgrades provided the side holds it over time and with it, the units that can be built.  For that, this server-side function will update the array and then publicVariable a string which a client-side script will convert to the updated array.

There appears to be two potential lag inducing events here; a trigger that’s constantly checking all the units present, looking for a particular type. The other is having to search through the array each time you want to update the status of a particular area?

Im not sure off hand about the first problem, but you could shift the emphasis onto the base generating vehicles. Get then to detect the proximity of a location instead of getting the location to check for the proximity of the vehicle. If you had say 30 base generating vehicles and 30 locations and 500 other units, then it's 30 x 30 instead of 30 x 530. If you see what I mean?

BTW has anyone experimented with setVehicleId? It looks like it has something to do with activating triggers, but god knows what.

The second problem could be addressed, depending on how you setup the triggers.i.e There a fixed number that don't change during the course of the mission. But if you don't have that many, it might not be worth going to all the trouble of speeding up thier indexing.

Quote[/b] ]I plugged it into my script and it seems to work pretty well.  I'd bet I could use some functions in other places as well once I get around to optimizing.

Functions are great if you want to make sure the results of your script are applied straight away. So searching for an array index or removing an element from an array can all be done in an instant.

Share this post


Link to post
Share on other sites

I am guessing here... but setVehicleID sets the id property of a vehicle.

You can see this by creating a mission and placing a crew-manned vehicle down in the editor. Sync it to a trigger. Then open up the SQM. The vehicle should have an "id = 1;" or some other value. Likewise, the trigger will have a two properties:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">activationBy = "VEHICLE";

idVehicle = {1};

By messing with the vehicle Ids, I can only assume there are odd results and unexpected behavior we are not familiar with yet. I don't know if having two vehicles with the same id will crash the game or it is simply for trigger use only.

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  

×