Jump to content
Sign in to follow this  
jedders

Problem with a nesting an if statement

Recommended Posts

The situation with this script is that I have a money system using a variable linked to each player called money. This script uses an addAction from a weapon shop and is trying to buy the TRG. It will check the users name to see where to put the gun in relation to who bought it. It will subtract 500 pounds from the player's money value. However, since I am very new to scripting I am not too sure what is going wrong here. It tells me I'm miss a ; on line 4 but if it is an if statement it shouldn't be closing a line but opening the if statement like I have?

Anyway here is the code in question (it may be pretty bad as I'm really new to scripting anything):

_caller=_this select 1;
_money = _caller getVariable "money";

if (_money > 500)&& _caller=James then 
{
money=_money-500;
_caller setVariable ["money",_money];
JamesLocker addweaponcargo ["arifle_TRG20_F",1];
JamesLocker addMagazinecargo ["30Rnd_556x45_Stanag",5];
Hint "TRG placed in locker with 5 magazines";
} 
else 
{
	 if (_money > 500)&& _caller=Josh then
	 money=_money-500; 
	 _caller setVariable ["money",_money];
	 JoshLocker addweaponcargo ["arifle_TRG20_F",1];
	 JoshLocker addMagazinecargo ["30Rnd_556x45_Stanag",5];
	 Hint "TRG placed in locker with 5 magazines (Josh)";
	 }
	 else
	 {
		 if (_money > 500)&& _caller = Tom then
		 money=_money-500; 
		 _caller setVariable ["money",_money];
		 TomLocker addweaponcargo ["arifle_TRG20_F",1];TomLocker addMagazinecargo ["30Rnd_556x45_Stanag",5];
		 Hint "TRG placed in locker with 5 magazines (Tom)";
		 }
		 else
		 {
			 hint "You do not have the money to buy this."

			 }

			 };

Share this post


Link to post
Share on other sites

if (_money > 500)&& (_caller =[color="#FF0000"]=[/color] James) then 

One = is assignment. Two == is comparing. You'll wanna fix that later for where you have Tom as well.

Anywhere you repeat code though you'll wanna use a function instead. So instead of hard coding Tom and James, just have some code that figures out if it's Tom or James then another part of code that actually puts it in the locker.

Here's a way of writing it so that it'll work for anyone, and you wouldn't have to hard code the script for each player you might have.

/*
Assuming you've set these variables on your players:

this setVariable ["money", 2000];
this setVariable ["locker", TomLocker];
*/

_caller = _this select 1;
_callerName = name _caller;
_callerLocker = _caller getVariable "locker";
_callerMoney = _caller getVariable "money";


// Assign weapon/ammo/cost/count

// Example: this addAction["Buy TRG w/5 Mags", "buyWeapon.sqf", ["TRG", "arifle_TRG20_F", 1, "30Rnd_556x45_Stanag", 5, 500]];

// Set variables
_args = _this select 3;
_availableWeaponName = _args select 0;
_availableWeapon = _args select 1;
_weaponCount = _args select 2;
_availableMagazine = _args select 3;
_magazineCount = _args select 4;
_weaponCost = _args select 5;

// Just to set this before using it.
_postPurchaseMoney = 0;


if (_callerMoney >= _weaponCost) then {
   // Has enough money, lets spend some!
   _postPurchaseMoney = _callerMoney - _weaponCost;
   _caller setVariable ["money", _postPurchaseMoney];

   // Place the weapon and ammo in the correct locker.
   _callerLocker addWeaponCargo [_availableWeapon, _weaponCount];
   _callerLocker addMagazinecargo [_availableMagazine, _magazineCount];

   // Let them know it's in there.
   hint format["%1 placed in locker with %2 magazines! [%3, you have %4 cash left]", _availableWeaponName, _magazineCount, _callerName, _postPurchaseMoney];

} else {
   // Doesn't have enough money.
   hint format["%1 - You do not have the money to buy this!", _callerName];
};

Edited by kylania
fixed script

Share this post


Link to post
Share on other sites

Ah right thanks. Well the bit that figures out who it is comes from the addAction to buy the weapon. But I don't know of a way to choose a locker based on the user other than the if statements is my problem.

Share this post


Link to post
Share on other sites

Easiest way would be to assign the locker based on variable. I edited my post above to show that option. You could expand this even more to use one script for any weapons even.

I changed the code above so that everything is variable, just one script for any number of weapons/mags/cost. Just change the addAction call to change what you're selling.

this addAction["Buy TRG w/5 Mags for 500", "buyWeapon.sqf", ["TRG", "arifle_TRG20_F", 1, "30Rnd_556x45_Stanag", 5, 500]];
this addAction["Buy Mk20 w/15 Mags for 2000", "buyWeapon.sqf", ["Mk20", "arifle_Mk20C_ACO_F", 1, "30Rnd_556x45_Stanag", 15, 2000]];

You could get crazier and pull the weapon picture/name whatever but this should do. :)

Edited by kylania

Share this post


Link to post
Share on other sites

I see now. Yeah that is much more efficient than my code. Never thought of assigning the locker to the players as variables to call them in either. Thank you very much!

Share this post


Link to post
Share on other sites

I was wondering is there anyway to make a hint specific to only one person. Such as with my script tell only the person that bought it that they bought that weapon and that they cannot afford it?

Share this post


Link to post
Share on other sites

The hint will only display for whoever is running the script. So just the buyer since it's an addAction which is a Local effect as is hint.

Share this post


Link to post
Share on other sites

Ok I wasn't sure if that is how it worked or not. Also is there a way to change the text when you hover over a unit or object? Such as if I made a rifleman could I change it to show text saying Rifle Shop instead of Rifleman? Or is this not possible with the editor?

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×