Home   Help Search Login Register  

Author Topic: Can't get "if" to work :S [ignore, decided to use spon money instead :oP]  (Read 1884 times)

0 Members and 2 Guests are viewing this topic.

Offline danturn

  • Members
  • *
Basically, it is a way of buying a new weapon. The player uses an addaction to buy an M4, if he has the correct magazine to trade in for the M4 then the script will remove the magazine from the player and add the M4 to a nearby ammo crate. I have worked out (sort of) what to put in the script, but for some reason it isn't detecting the magazine and just giving the hint for it not being detected, where have i gone wrong?

Code: [Select]
if ((magazines player select 0) in ["rpo_obj3"]) then {ammobox1 addweaponcargo ["m4",1]};
if ((magazines player select 0) in ["rpo_obj3"]) then {player removemagazine "rpo_obj3"};
if not ((magazines player select 0) in ["RPO_obj3"]) then {hint "Not enough cash"};

Thanks if anyone can help.
« Last Edit: 29 Oct 2008, 01:15:07 by danturn »

Offline hoz

  • OFPEC Site
  • Administrator
  • *****
Re: Can't get "if" to work :S
« Reply #1 on: 27 Oct 2008, 18:17:09 »
Code: [Select]
if ((magazines player select 0) in ["rpo_obj3"]) then {ammobox1 addweaponcargo ["m4",1]};
Pretty sure your missing an inner ;

Code: [Select]
if ((magazines player select 0) in ["rpo_obj3"]) then {ammobox1 addweaponcargo ["m4",1];};
Xbox Rocks

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Can't get "if" to work :S
« Reply #2 on: 27 Oct 2008, 18:34:17 »
Code: [Select]
if (!((magazines player select 0) in ["RPO_obj3"])) then {hint "Not enough cash"};

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Can't get "if" to work :S
« Reply #3 on: 27 Oct 2008, 18:57:47 »
@hoz
Code: [Select]
if ((magazines player select 0) in ["rpo_obj3"]) then {ammobox1 addweaponcargo ["m4",1]};
is exactly the same as:
Code: [Select]
if ((magazines player select 0) in ["rpo_obj3"]) then {ammobox1 addweaponcargo ["m4",1];};
since the last semi-colon in a block {} is optional.

@Mandoble
Code: [Select]
if (!((magazines player select 0) in ["RPO_obj3"])) then {hint "Not enough cash"};
Is exactly the same as:
Code: [Select]
if not ((magazines player select 0) in ["RPO_obj3"]) then {hint "Not enough cash"};
because ! is the same as not and the brackets around the if condition are optional.

@danturn
What you actually want to do is take advantage of the way SQF works. It is obvious that you are still scripting in SQS style, if not SQS syntax. You should be checking whether the player has the class in his inventory, rather than checking whether the first item in your inventory is the correct item, which will fail if the first item is of a different class, or if you have nothing in your inventory. Oh, and you need to ensure that you have the correct capitalisation for class names or you can't use them in comparisons like == or in:
Code: [Select]
if ("RPO_obj3" in (magazines player)) then
{
     ammobox1 addweaponcargo ["m4",1];
     player removemagazine "RPO_obj3"; // Capitalisation doesn't matter here.
}
else
{
    hint "Not enough cash";
};
« Last Edit: 27 Oct 2008, 18:59:42 by Spooner »
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline danturn

  • Members
  • *
Re: Can't get "if" to work :S
« Reply #4 on: 28 Oct 2008, 01:45:03 »
Ahh excellent. Not 100% sure on the differences between the two but that works great cheers.

One more thing based on this. Is it possible to have a script running constantly so, for example, "rpo_obj4" is equal to two of "rpo_obj3". Is it possible to have a constant running script, so that  when the player has two "rpo_obj3" in their inventory, it will automatically change to 1 "rpo_obj4"?

Cheers

Offline ModestNovice

  • Members
  • *
Re: Can't get "if" to work :S
« Reply #5 on: 28 Oct 2008, 04:37:20 »
Untested.

money_check.sqf
Code: [Select]
_obj1 = [];

while {alive player} do
{
for [{_i=0},{_i < count magazines player},{_i=_i+1}] do
{
_curMag = magazines player select _i;

if (_curMag == "rpo_obj3") then
{
_obj1 = _obj1 + [_curMag];
};
Sleep 0.2;
};

if (count _obj1 == 1) then
{
player removeMagazine "rpo_obj3";
player removeMagazine "rpo_obj3";
player addMagazine "rpo_obj4";
_obj1 = [];
};
Sleep 2;
};
waitUntil {alive player};
[] execVM "money_check.sqf";
« Last Edit: 28 Oct 2008, 04:40:49 by DaChevs »
"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 danturn

  • Members
  • *
Re: Can't get "if" to work :S
« Reply #6 on: 28 Oct 2008, 18:21:42 »
Ahh cheers. Shall give this a try

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Can't get "if" to work :S
« Reply #7 on: 29 Oct 2008, 00:42:34 »
Erm, remove the "Sleep 0.2;" or you will occasionally generate extra money! Really ;P

Also, remember that all class names start with RPO_, not rpo_ or it might not recognise them. I explained why earlier, I think
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline ModestNovice

  • Members
  • *
Re: Can't get "if" to work :S
« Reply #8 on: 29 Oct 2008, 01:06:16 »
ahh ok.

I always added the Sleeps so it wouldnt lag.
"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
There are times when adding sleeps prevents lags. There are other times when adding them actually adds to lag; other times, like this, when it causes bugs. Just adding sleep in every loop without really understanding why you are doing it will cause a lot of problems. On the other hand, your "sleep 2" is vital and does what you want: prevents the script locking up.

As a rule of thumb, any script that would go on forever, or at least for an extended period, like while {true} or while { alive player } requires some sort of sleep in it or you will freeze the machine while it continually runs the script and doesn't take a break to update the game engine (graphics, physics, etc). If a script is finite (for "_i" from 1 to 10 do or with a forEach) then it probably shouldn't have a sleep, at least not to stop the game freezing. Obviously, there are lots of special cases, such as when the loop contains "slow" commands like createVehicle which, if you performed several times at once you would cause lag.

If in doubt, run without the sleep. If you get lag or freezing, add a sleep. Don't just put sleep in every loop for the sake of it.

OK, here is one of several scenarios that will break it: I am carrying ["RPO_obj3", "handgrenade"] and I pick up a "RPO_obj3" so I have ["RPO_obj3", "handgrenade", "RPO_obj3"],. The script comes around and looks at magazines select 0 and sees I have a "RPO_obj3". All well and good. It then waits 0.2 s, in which time I drop one of my first "RPO_obj3", which leaves me with ["handgrenade", "RPO_obj3"]. The script then checks magazines select 1, which is an "RPO_obj3" and adds that to the result. It then removes 2 obj3s and gives me an obj4, giving me: ["RPO_obj4"] and leaving a "RPO_obj3" on the ground that I pick up again. Net gain is one "RPO_obj3"!

Before you think, "but that would never happen!", believe me that it would do it occasionally and really confuse your players and you. More importantly a player could easily exploit it to generate sacks of money. In my example, the loop takes 2 + 0.2 + 0.2 = 2.4s. Therefore, if you repeatedly pick up and drop these objects in the right order you get a free object 0.2/2.4 = 1/12th of the time!

Well, removing that sleep will make the script work fine, but an overall better solution would be to use:
Code: [Select]
while {true} do
{
if (alive player) then
count _obj1 == 1) then
{
player removeMagazine "rpo_obj3";
player removeMagazine "rpo_obj3";
player addMagazine "rpo_obj4";
_obj1 = [];
};

       Sleep 2;
};

EDIT: Pasted in wrong script...corrected in later post.
« Last Edit: 29 Oct 2008, 02:51:00 by Spooner »
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline ModestNovice

  • Members
  • *
ahhh ok, will keep this in mind thanks Spooner.

Though one thing in your post?

did you mean:
Code: [Select]
while {true} do
{
if (alive player) then
       {
               if (count _obj1 == 1) then
      {
  player removeMagazine "rpo_obj3";
  player removeMagazine "rpo_obj3";
  player addMagazine "rpo_obj4";
  _obj1 = [];
};

 Sleep 2;
};
      };
"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
No, I didn't mean either what I posted or what you said ;P Sorry, I cut-and-pasted the wrong script or just generally messed up in the simple task of entering a post... :whistle:

Code: [Select]
while {true} do
{
if (alive player) then
{
// Merge any lower value objects here.

// Merge all obj3 objects into obj4s.
while { ( { _x == "RPO_obj3" } count (magazines player)) >= 2) do
{
player removeMagazine "RPO_obj3";
player removeMagazine "RPO_obj3";
player addMagazine "RPO_obj4";
};

// Merge any higher value objects here.
};

sleep 2;
};

Of course, all a bit moot, since he's decided not to build his own shopping system ;P
« Last Edit: 29 Oct 2008, 02:52:19 by Spooner »
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)