Home   Help Search Login Register  

Author Topic: Syntax help in script (PLEASE)  (Read 1222 times)

0 Members and 1 Guest are viewing this topic.

Offline ModestNovice

  • Members
  • *
Syntax help in script (PLEASE)
« on: 27 Sep 2008, 22:33:55 »
Well, although this works, I feel like an idiot because I know its not correctly done, and done like n00b.

Could you help me get a better grasp of how I could write this properly?

Code: [Select]
vehicle_shop_array =
[
["UsedShop", "used", "car", 8, 0, "Used Vehicle Shop"],
["NewShop", "new", "car", 8, 1, "New Vehicle Shop"],
["OffroadShop", "offroad", "car", 8, 2, "Offroad Vehicle Shop"],
["MuscleShop", "muscle", "car", 8, 3, "Muscle Vehicle Shop"],
["PoliceShop", "police_vehicles", "car", 8, 4, "Police Vehicle Shop"],
["BoatShop", "boats", "boat", 8, 5, "Boat Shop"],
["AirShop", "air_vehicles", "air", 8, 6, "Air Vehicle Shop"]
];
{call compile format ["ac%1 = 0", _x select 4]} foreach vehicle_shop_array;


while {alive player} do
{
{
if (call compile format ["((player distance(getPos %1)) <= 7)", _x select 0]) then
{
if (call compile format ["ac%1 == 0", _x select 4]) then
{
call compile format ["va%1 = player addAction [""%2"", ""D_Functions\veh_shop.sqf"", []]", _x select 1, _x select 5];
call compile format ["ac%1 = 1", _x select 4];
}
else
{
};
}
else
{
call compile format ["ac%1 = 0", _x select 4];
call compile format ["player removeAction va%1", _x select 1];
};
} foreach vehicle_shop_array;
Sleep 1;
};

And please don't hold anything against me, I know I am not good, but I am trying to learn guy  :-[
"The road became empty and the people disappeared. The clouds ran away; opened up the sky, and one by one I watched every constellation die."
- Sean "Slug" Daley

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Syntax help in script (PLEASE)
« Reply #1 on: 28 Sep 2008, 00:23:46 »
Most obvious thing is that you can elimitate the dynamic variables entirely by using...arrays.
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline ModestNovice

  • Members
  • *
Re: Syntax help in script (PLEASE)
« Reply #2 on: 28 Sep 2008, 03:17:08 »
enlighten me?  :-[
"The road became empty and the people disappeared. The clouds ran away; opened up the sky, and one by one I watched every constellation die."
- Sean "Slug" Daley

Offline Trexian

  • Members
  • *
Re: Syntax help in script (PLEASE)
« Reply #3 on: 28 Sep 2008, 04:23:20 »
If I may (and certainly follow Spooner's recommendation), ;) your element 4 is just an index, right?  They are indexed in the array like that anyway.

Also, instead of the compiles, I'd consider a switch/case conditional that looked at element 0 in the sub-arrays, and set the addaction text by that.

You want to first sort by distance, right?

You have your vehicle_shop_array.  Foreach in that, get your distance to each one.  If <+ 7 then vehicle_shop_array_display = vehicle_shop_array_display + (vehicle_shop_array select _x) or something like that.  Then a foreach for that array, looking at ((vehicle_shop_array_display _x) select 0) and a switch/case based on that to do the addaction text.

A whole different approach might be possible, too.  Are these markers that have been assigned?  You can make a script that gets the marker locations and creates a trigger based on those locations.

Sorry, I'm pretty new at all this, too. :)
Sic semper tyrannosauro.

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Syntax help in script (PLEASE)
« Reply #4 on: 28 Sep 2008, 13:41:07 »
Well, 99% of times when I see people using dynamic code, they should be using arrays. Dynamic code and dynamic variables have definite uses, but they are an incredibly complex answer to a simple question most of the time.

Using dynamic variables:
Code: [Select]
for "_i" from 0 to 5 do
{
    call compile format ["frog%1 = %2", _i, _i * 10];
};

hint str call compile ["frog%1", 3]; // => 30
(you have 6 variables called frog0 to frog5).

Using arrays:
Code: [Select]
frog = [];
for "_i" from 0 to 5 do
{
    frog set [_i, _i * 10];
};

// frog => [0, 10, 20, 30, 40, 50]

hint str (frog select 3); // => 30
or
Code: [Select]
frog = [];
for "_i" from 0 to 5 do
{
    frog = frog + [_i * 10];
};

// frog => [0, 10, 20, 30, 40, 50]

hint str (frog select 3); // => 30

If nothing else, when you compile a string in your script, rather than using compile preprocessFileLineNumbers on a file, you won't get proper error messages if there is an error. Thus, you should avoid it at all costs anyway!

Incidentally, if you aren't referring to something outside of a script, it is always better to use local variables (_frog) rather than globals (frog). All you need to do is declare all of the locals in a private statement at the start of the script, to avoid scope issues, which I assume is why people avoid locals.
« Last Edit: 28 Sep 2008, 13:48:58 by Spooner »
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline ModestNovice

  • Members
  • *
Re: Syntax help in script (PLEASE)
« Reply #5 on: 28 Sep 2008, 17:35:46 »
ok thanks Spooner.

So that solved, is there a way to do my code better for checking distance and all?

Sry if you are already explaining this, I am a bit slow at some times, or maybe all the time.
"The road became empty and the people disappeared. The clouds ran away; opened up the sky, and one by one I watched every constellation die."
- Sean "Slug" Daley