Home   Help Search Login Register  

Author Topic: variable variable or serialize  (Read 723 times)

0 Members and 2 Guests are viewing this topic.

basil_rs2

  • Guest
variable variable or serialize
« on: 11 Jul 2003, 20:49:31 »
hi

is it possible to create variable variable ? something like
format["GLOBAL_%1", name _unit] = [somearray, stuff] ?

I need global variable to pass pass collected data from one script into another. I think a variable variable would keep the script scalar.

another idea is to serialize some data and pass it into one global var. like MOSTGLOBAL = [format["%1|%2",_unit, _collected_data] ]

if _collected_data is an array I could never deserialize it back into a normal array. Something like [_serialized,_delimeter] exec "explode.sqs" would help, but how to explode a sting ?
Anyway, I would have to run all array-entries to find specific point, couse "var select array" or "var select object" wont work.

any workaround ideas ?

Offline Dinger

  • Contributing Member
  • **
  • where's the ultra-theoretical mega-scripting forum
Re:variable variable or serialize
« Reply #1 on: 12 Jul 2003, 03:55:50 »
I'm afraid I don't understand half of what was posted there.

But are you looking for this:

call format ["MyGlobalVariable%1 = %2", _index, _value]

?

You can't take apart strings.
Dinger/Cfit

basil_rs2

  • Guest
Re:variable variable or serialize
« Reply #2 on: 13 Jul 2003, 23:39:04 »
yes this should work fine ;)
thx

basil_rs2

  • Guest
Re:variable variable or serialize
« Reply #3 on: 14 Jul 2003, 19:49:15 »
hmmm what i mean is something like this :

sortbubble.sqf :

by bn880

I removed parameter count check and added "_unit" to keep
numeric value enumeration scalar.

Code: [Select]
private ["_unit", "_array","_eval","_numArray","_j","_k","_tempVal","_tempNum","_element"];

_array = _this select 0;
_eval = _this select 1;
_unit = _this select 2;

_count = count _array;
_numArray = [];
_numArray resize _count;

// ACQUIRE NUMERIC VALUES FROM EVALUATION
_j = 0;
while "_j < _count" do
{
  _element = _array select _j;
  _numArray set [_j,call _eval];
  _j = _j + 1;
};

_j = 0;
// SORT
while "_j < _count -1" do
{
  _k = _j + 1;
  while "_k < _count" do
  {
    if (_numArray select _j > _numArray select _k) then
    {
      _tempNum = _numArray select _j;
      _numArray set [_j,(_numArray select _k)];
      _numArray set [_k,_tempNum];

      _tempVal = _array select _j;
      _array set [_j,(_array select _k)];
      _array set [_k,_tempVal];
    };
    _k = _k + 1;
  };
  _j = _j + 1;
};

init.sqs :

Code: [Select]
bubblesort = preProcessFile "sortbubble.sqf"

now here is a script I run on a unit.

[this, "SOMEID", trigger] exec "near.sqs"

you can use this inside mission editor at unit init row.
the trigger provides units list.
best way would be to pass "this" as _id, but it would
crash the script couse of whitespaces. :(

I found another way to enum a valid unique unit id.
itÂ's not the best way i think, but it works
and it works just as long as you use only ONE unit list trigger:

unique_id.sqf :

Code: [Select]
private ["_units", "_trigger", "_unit", "_i", "_j", "_c"];

_unit    = _this select 0;
_trigger = _this select 1;

_units   = list _trigger;

_id      = "X";

_i = 0;
_j = count _units;

while ("_i<_j") do {

  _c = _units select _i;

  if (_c == _unit) then { _id = _i; _i = _j; } else { _i = _i + 1; };

};

if( format["%1", _id] != "X" ) then { format ["%1",_id] } else { hint "UNIQUE ID ERROR" }

this will return a simple integer. so I used a all units list loop script to start
near.sqs:

[((list _trigger) select _i), [((list _trigger) select _i), _trigger] call uid, _trigger] exec "near.sqs"

where uid = preProcessFile "unique_id.sqf" ; // at init.sqs


now the main script

near.sqs :

Code: [Select]
_unit           = _this select 0
_id             = _this select 1
_trigger        = _this select 2

_nearestrefresh   = 1

; // variable variable string here
_near_array = "GLB_NEAR_"

; // this is why i added "_unit" to bn880 sortbubble code
_eval = "_element distance _unit"

; // variable variable init
; // will set "GLB_NEAR_1 = []" if _id is 1 ...
call format["%1%2 = []", _near_array, _id]

#loo

  ?!(alive _unit): goto "exit"

    _units = list _trigger

    ~0.1

    _i = 0
    _catched = []

    ; // prefilter to sort units
    while "_i < count _units" do { _c = _units select _i; if (("Man" countType [_c]) == 1 and ((_unit knowsAbout _c) > 0) and (_c != _unit) and (alive _c) and !(_c in units (group _unit)) ) then { _catched = _catched + [_c]; }; _i=_i+1; }

    ~0.1

    ?((count _catched) < 1): goto "skip"

    ; // sort
    [_catched, _eval, _unit] call bubblesort

      ~0.1

      ; // set nearunits to variable global var
      call format["%1%2 = _catched", _near_array, _id]
     
      ; // now any other script can find nearest unit to _unit with "GLB_NEAR_1 select 0"
      ; // if _id is 1, second nearest unit is ... select 1 [...]
      ; // just pass _id to other scripts to keep all this stuff scalar. :)

      ~0.1

  #skip

  ?((call format["count %1%2", _near_array, _id]) < 1): _unit globalChat format ["%1: iÂ'm alone here !", name _unit]

  ; // update every 1 - 2 seconds
  _randnext = (random _nearestrefresh) + _nearestrefresh
  ~_randnext

goto "loo"



#exit

  _unit globalChat format ["%1: near exit", name _unit]
 exit



and it works :)

thanks Dinger !

Offline DrStrangelove

  • Members
  • *
  • Mr.Creative
Re:variable variable or serialize
« Reply #4 on: 15 Jul 2003, 18:18:32 »
What does it do ?  ???

basil_rs2

  • Guest
Re:variable variable or serialize
« Reply #5 on: 16 Jul 2003, 22:31:23 »
this sets a globla array containg asc sortet units. first unit is nearest unit to _unit.
for example you can create a grouping behavior of undefined count of units without waypoints. evac chopper, advanced medic units ...
useful for any complex AI bahavior.
« Last Edit: 17 Jul 2003, 16:03:54 by basil_rs2 »