Jump to content
Sign in to follow this  
Graz

Using Foreach and Count Properly

Recommended Posts

Like the title says.

I've had some issues with a small script.

It's designed to check an object (BYBox) and then if the right items are in it it takes them out and runs a vehicle spawning script.

My syntax is rubbish, but I've attempted to run it and am having no luck

BYBox setvehicleLock "LOCKED";

sleep 0.1;

//Build Arrays
private ["_cost", "_inv", "_wheel", "_scrap"];
_cost = [ ["PartWheel", 2], ["PartGeneric", 2] ];
_inv = BYBox call BIS_fnc_invstring;
_wheel = {_x=="PartWheel"} count _inv;  //PROBLEM AREA
_scrap = {_x=="PartGeneric" } count _inv; //PROBLEM AREA

//Check Box
if (_wheel > 2 && _scrap > 2)
then {
    {BYBox removeMagazine _x } forEach _cost;  //PROBLEM AREA
   hint "It works!";
} else {
   hint "you didn't have enough!!";
};
sleep 0.1;
BYBox setVehicleLock "Unlocked";

Is there any reason this isn't working. I think my understanding of Foreach and Count are wrong >_<

Edited by Graz
Updated code

Share this post


Link to post
Share on other sites

something like :

 {BYBox removeMagazine _x} forEach _cost

EDIT : didn't see the count part the first time. but as i dunno what BIS_fnc_invstring does...

Edited by ProfTournesol

Share this post


Link to post
Share on other sites

Why is the condition of your if statement enclosed with curly brackets?

The biki only uses parenthesis in it's examples and I do too.

You might want to have a look at Squint if you haven't already. It's very useful.

http://forums.bistudio.com/showthread.php?105860-Squint-the-sqf-editor-and-error-checker

Share this post


Link to post
Share on other sites

This is without any errors but no idea if it will work as the correct elements might not be in the arrays - you would have to give us more info:

This looks like DAYZ stuff - parts for building a car?

BYBox setvehicleLock "LOCKED";

sleep 0.1;

//Build Arrays
private ["_cost", "_inv", "_wheel", "_scrap"];
_cost = [ ["PartWheel", 2], ["PartGeneric", 2] ];
_inv = BYBox call BIS_fnc_invstring;
_wheel = {_x=="PartWheel"} count _inv;  //PROBLEM AREA
_scrap = {_x=="PartGeneric" } count _inv; //PROBLEM AREA

//Check Box
if (_wheel > 2 && _scrap > 2)
then {
    {BYBox removeMagazine _x } forEach _cost;  //PROBLEM AREA
   hint "It works!";
} else {
   hint "you didn't have enough!!";
};
sleep 0.1;
BYBox setVehicleLock "Unlocked";

Command ref: http://community.bistudio.com/wiki/Category:Scripting_Commands_ArmA2

Share this post


Link to post
Share on other sites
This is without any errors but no idea if it will work as the correct elements might not be in the arrays - you would have to give us more info:

This looks like DAYZ stuff - parts for building a car?

You've got me, I was bored and wanted to see if I could make a script for building cars. I'll give you the run down.

The script:

Checks for the ingredients: 2x Wheels, 2x Scraps

If the BYBox has the ingredients: It removes them and then runs runs a spawning script.

If BYbox doesn't have the parts, the script tells you to get lost.

I wanted to to integrate the create vehicle script into it but I wanted to get this working first.

Any help would be reall appreciated :)

Share this post


Link to post
Share on other sites

Haven't done any DayZ scripting so a bit in the dark - using Arma2 logic this should work:

BYBox setvehicleLock "LOCKED";

sleep 0.1;

//Build Arrays
private ["_inv", "_wheel", "_scrap"];
_inv = BYBox call BIS_fnc_invstring;
_wheel = {_x=="PartWheel"} count _inv;  //PROBLEM AREA
_scrap = {_x=="PartGeneric" } count _inv; //PROBLEM AREA

//Check Box
if (_wheel >= 2 && _scrap >= 2)
then {
    {BYBox removeMagazine "PartWheel";  BYBox removeMagazine "PartGeneric";} forEach [1,2];  //PROBLEM AREA
   hint "It works!";
} else {
   hint "you didn't have enough!!";
};
sleep 0.1;
BYBox setVehicleLock "Unlocked";

Got rid of cost as it didn't make sense to me - those arrays are for addMagazine. I'm assuming you only need 2 of each mag for this to work?

Share this post


Link to post
Share on other sites

Yeah I do only need 2 mags of each. There's a motorbike but it's just 1 engine, 1 fuel tank. I can copy paste and make that without any dramas if I can get this one to work

I'm not sure the BIS function is working properly, is there anyway to get the inventory of BYBox without it?

Share this post


Link to post
Share on other sites

edit: Just tested it and had a look at the functions viewer - BIS_fnc_invstring is the correct function - don't think you are calling it correctly - should be:

_inv = [bYBox] call BIS_fnc_invstring;

Tested it in game with:

inv = [vehicle player] call BIS_fnc_invstring; hint format["%1",inv];

try it in and out of vehicles

or:

_content = getMagazineCargo carName;

http://community.bistudio.com/wiki/Category:Scripting_Commands_ArmA2

http://community.bistudio.com/wiki/getMagazineCargo

Edited by Mattar_Tharkari

Share this post


Link to post
Share on other sites

Excellent, I'll try and crank it up today. I'm fairly excited about this, thank you so much for the help so far!

Share this post


Link to post
Share on other sites

Seems to work, but I placed the items and it was telling me that I didn't have enough.

I'm thinking maybe it wasn't checking the right object. I've named a crate BYBox in the editor, am I referring to it correctly or do I need another way?

Share this post


Link to post
Share on other sites

inv = [bYBox] call BIS_fnc_invstring;

BYBox need to be a player, AI or driveable vehicle - that function doesn't work on objects or ammo boxes;

To add the magazines to a HMWWV put this in it's init (eg three Laserbatteries, change the mag name to whatever):

this addMagazineCargoGlobal ["Laserbatteries",3];

Edited by Mattar_Tharkari

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  

×