Home   Help Search Login Register  

Author Topic: Generating a variable. Problem...Solved!!!  (Read 1199 times)

0 Members and 1 Guest are viewing this topic.

Offline L!nk

  • Members
  • *
Generating a variable. Problem...Solved!!!
« on: 17 Mar 2008, 14:47:27 »
Here is my script.

Lets say:
Code: [Select]
PlayerSide = 1;
PlayerGroup = 6;
_price = 2000;

Code: [Select]
(call compile format["ArrMoney_Si%1_%2", PlayerSide,PlayerGroup]) = (call compile format["ArrMoney_Si%1_%2", PlayerSide,PlayerGroup]) - _price;
If the variable isn't generated, it should look like this:
Code: [Select]
ArrMoney_Si1_6 = ArrMoney_Si1_6 - _price
Solution:
Code: [Select]
if (PlayerSide == 0) then {ArrMone_Si0 set [PlayerGroup,(_money - _price)]};
if (PlayerSide == 1) then {ArrMone_Si1 set [PlayerGroup,(_money - _price)]};
« Last Edit: 17 Mar 2008, 16:19:30 by L!nk »

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Generating a variable. Problem!
« Reply #1 on: 17 Mar 2008, 15:29:16 »
Well, you seem to sometimes be using PlayerSide and PlayerGroup and sometimes be using LinkCTI_PlayerSide and  LinkCTI_PlayerGroup. This could be causing inconsistent results, but I think that is probably a copy-and-paste error, so I assume that isn't the problem you are asking about.

Your confusion stems from believing that you can have variable references in SQF, which you can't. The code,
Code: [Select]
call compile format["ArrMoney_Si%1_%2", PlayerSide,PlayerGroup]
resolves to the value of ArrMoney_Si1_6, not to a reference to the variable itself. If it was a reference, then you would have been able to perform the assignment that you are attempting.

Anyway, the overall code should, most simply, be:
Code: [Select]
call compile format["ArrMoney_Si%1_%2 = ArrMoney_Si%1_%2 - _price", PlayerSide, PlayerGroup];

Incidentally, you could use normal arrays for this to avoid the use of code-generation altogether (always a good idea, since code-generation is slower and more prone to run-time errors):
Code: [Select]
(ArrMoney select PlayerSide) set [PlayerGroup, ((ArrMoney select PlayerSide) select PlayerGroup) -_price];
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline L!nk

  • Members
  • *
Re: Generating a variable. Problem...Solved!!!
« Reply #2 on: 17 Mar 2008, 16:21:59 »
Thx alot.
Figured out the array technique a while ago.
Thx alot for the other way, that will help in the future!

Thx,
L!nk