Home   Help Search Login Register  

Author Topic: Dynamic Var/Array Names  (Read 1294 times)

0 Members and 2 Guests are viewing this topic.

Offline XCess

  • Former Staff
  • ****
Dynamic Var/Array Names
« on: 06 Sep 2006, 20:37:13 »
I'm making a script that will check the vehicles in an unknown number of groups. I want to create an array of vehicles for each  group, but since I don't know how many groups there are I need to assign a dynamic name to the array, if it's the third group I want the array name to be groupVehicles3 and if its the first, gropVehicles1.
Is there a way to add a chaning number to an array name?

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re: Dynamic Var/Array Names
« Reply #1 on: 06 Sep 2006, 23:16:53 »
Quote
if it's the third group I want the array name to be groupVehicles3 and if its the first, gropVehicles1.
Why?

Why not create an array of the groups, that way you can access the nth group via the select command?

I know this does not do what you need but I think I know you well enough to know that you should be able to figure out what you want to do from this.  The following code creates an array of groups of footsoldiers from a trigger's list of units:

Code: [Select]
;Create an array of living loons
_livingloons = []
{{if (alive _x) then {_livingloons = _livingloons + [_x]}} forEach crew _x} forEach list _TrigName

;Create a list of footsoldiers
_footsoldiers = []
{if ((vehicle _x) == _x) then {_footsoldiers = _footsoldiers + [_x]}} forEach _livingloons

;Create a list of groups of footsoldiers
_footGroups = []
{if (_x == leader group _x) then {_footGroups = _footGroups +[group _x]}} forEach _footsoldiers

Thereafter I use the select command to select each group as follows:

Code: [Select]
if (count _footGroups < 1) then {exit}
_i = 0
#loop
_grp = _footGroups select _i
.
.
.
_i = _i + 1
if (_i < count _footGroups) then {goto"loop"}

Then you don't even need to worry about how many groups you have, the code takes care of it all.

Offline XCess

  • Former Staff
  • ****
Re: Dynamic Var/Array Names
« Reply #2 on: 06 Sep 2006, 23:42:54 »
I think you misunderstood what I'm trying to do THobson, although getting the living loons from a trigger list will be a much better idea than adding them to an array via the exec command ::)

What I'm trying to do is check how many vehicles are in each group, and from there add a rating to those vehicles so the script will then decide the actions of each group and that of the enemy groups depending on squad ratings, positions and ofcourse, the overall count.. I'll also be factoring in variables such as defined defensive positions and perhaps unit losses (morale).

Making a list of all groups isn't the problem,. I want to create an array of all the vehicles in a group, therefore I want a different array for each group. Which means dynamicly naming the arrays since I do not know which groups will have vehicles, and how many groups there will be.

If you're wondering why the hell I'm doing this, I'm just seeing how far I can go with my knowledge of the scripting command as far as AI control goes.

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re: Dynamic Var/Array Names
« Reply #3 on: 07 Sep 2006, 08:51:38 »
I can't write it for you  - I have to rush off now, but here are some script samples I have used that might help you:

Code: [Select]
_vehListE = list trigAllEast
_vehListW = list trigAllWest
_vehListC = list trigAllCivil
_vehListR = list trigAllRes
_vehListALL = +_vehListE + _vehListW + _vehListC + _vehListR
_landVehicles = []
_tanks = []
_air = []
_MG = []
{if ("LandVehicle" counttype [_x] > 0) then {_landVehicles = _landVehicles + [_x]}} forEach _vehListALL
{if ("tank" counttype [_x] > 0) then {_tanks = _tanks + [_x]}} forEach _landVehicles
{if ("M2StaticMG" counttype [_x] > 0) then {_MG = _MG + [_x]}} forEach _landVehicles
{if ("M2StaticMGE" counttype [_x] > 0) then {_MG = _MG + [_x]}} forEach _landVehicles
{if ("air" counttype [_x] > 0) then {_air = _air + [_x]}} forEach _vehListALL
.
.
.
_police = []
_mBike = []
_bike = []
{if ("JeepPolice" counttype [_x] > 0) then {_police = _police + [_x]}} forEach _landVehicles
{if ("Jawa" counttype [_x] > 0) then {_mBike = _mBike + [_x]}} forEach _landVehicles
{if ("Kolo" counttype [_x] > 0) then {_bike = _bike + [_x]}} forEach _landVehicles

trigAllEast etc. are the names of triggers that cover the map and trigger on ... Present.

 

Offline Dinger

  • Contributing Member
  • **
  • where's the ultra-theoretical mega-scripting forum
Re: Dynamic Var/Array Names
« Reply #4 on: 07 Sep 2006, 16:21:50 »
You can also have arrays of arrays, you know.
"Metavariables" aka "Dynamic Variable Names" aka "Self-Modifying Code"

_stem = "MyVariable";
_index = 0;
_varName = "";
;_Targets is the an array of things you want to set up variables for.
_targets = _This select 0

{_varName = format ["%1%2", _stem, _index]; call format ["%1 = _x", _varName, _x];} ForEach _targets;

But generally speaking, you don't want to use this trick unless you absolutely have to. Why? 'cos self-modifying code is _bad_, as you have to think of the script at different levels of abstraction simultaneously.
Dinger/Cfit

Offline XCess

  • Former Staff
  • ****
Re: Dynamic Var/Array Names
« Reply #5 on: 07 Sep 2006, 18:10:50 »
Thanks for that dinger, should be exactly what I'm looking for. Do you know if it works with publicVariable? Cos I don't think the command supports strings..

Also thanks THobsons, code shold be useful :)

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re: Dynamic Var/Array Names
« Reply #6 on: 07 Sep 2006, 18:47:26 »
I still suggest you create an array of arrays.  Something like (not tested):



Code: [Select]
_grps = []
{if not (group _x in _grps) then {_grps = _grps + [group _x]}} forEach list Triggername
;_grps should now be an array of the groups detected by the trigger

if (count _grps < 1) then {exit}

grpVeh = []
_i = 0
#loop
_grp = _grps select _i
_landVehicles = []
{if ("LandVehicle" counttype [_x] > 0) then {_landVehicles = _landVehicles + [_x]}} forEach units _grp
grpVeh = grpVeh + [_landVehicles]
_i = _i + 1
if (_i < count _grps) then {goto"loop"}

grpVeh should now be an array of arrays.  The Nth element of grpVeh is an array of the land vehicles in the Nth group.  Note that this element of grpVeh will be an empty array if the goup has no land vehicles in it.

This way you are not creating dynamically named variables that you have to keep track of.  Just one array that has an array as each element.