Jump to content
  • 1

Quick Question.


Leigham

Question

Im working on a script, and Im so Close to finishing, just this one thing Keeps getting stuck Ill explain Below.

If I have an array.

UG_VehicleList = [
["Mosquito_Epoch",[""],false,0],
["Mosquito_Epoch",[""],true,0],
["Ebike_Epoch",[""],false,0],
["Ebike_Epoch",[""],true,0]
];

 

 

and I wanted to do

 

if(_CraftingItems == "")then{exitwith {};}else{
{
_CraftingItems = _x select 2;
if(_CraftingItems in (magazines player))then{

}foreach UG_VehicleList;
}

I cant get that to check for multiples of the same item, I want it to check if the player has all the items in the array before it takes it out. 

Link to comment
Share on other sites

16 answers to this question

Recommended Posts

  • 1

in both cases your syntax is wrong.

1) you can only use ELSE together with THEN... exitWith should always be on its own.

if (boolean) then {} else {}; //correct

if (boolean) exitWith {return}; //correct

2) You can not use forEach inside IF statement brackets, lol

{

} forEach []; //correct

if (statement) then {
    {
    
    }forEach [];
}; //correct

To access nested elements:

_arr = 
[
    ["Mosquito_Epoch",[""],false,0],
    ["Mosquito_Epoch",[""],false,0],
    ["Mosquito_Epoch",[""],false,0],
    ["Mosquito_Epoch",[""],false,0]
];


{
    _string = _x select 0; //"Mosquito_Epoch"
    _array = _x select 1; // [""]
    _bool = _x select 2; // false
    _int = _x select 3; // 0
} forEach _arr;

And finally, vehicles are not and can not be part of magazines, derp.

Link to comment
Share on other sites

  • 1
//-----------------fill your LB like this in your first script:
_display = findDisplay 7770;
_listBox = _display displayCtrl 7779;

_listBoxItems = ["CircuitParts","CircuitParts","CircuitParts"];

{
	_idx = _listBox lbAdd (_x call UG_GetClassnameVehicle); //Add displayName to listbox, not the classname, which makes more sense
	_listBox lbSetData [_idx,_x]; //save classname in data field instead (invisible, but retrievable later using lbData command)
}forEach _listBoxItems;

//------------- read LB, second script:
disableSerialization;
_display = findDisplay 7770;
_listBox = _display displayCtrl 7779; //if you save control, then use it, use alternative syntax on wiki

_index = lbCurSel _listBox; //index is required to access data
_selectionText = _listBox lbtext _index; //text displayed on selection
_selectionData = _listBox lbData _index; //className we saved earlier in a data field

This is a better way of doing the listbox as it allows you to display full name of item instead of classname, which players shouldn't be reading normally. Assuming that UG_GetClassnameVehicle function actually works.
For the rest of your code, look at syntax I posted earlier and correct it appropriately, me giving away full code wouldn't be fun now, would it? :)

Suggestions:
Don't save _x into value, that's a bad habit, just use it as is (_Class).
Only save variable if it contains command and it's used more than once.
If you have code block that repeats itself, put it in a function.
Please try to do proper code formatting (tabbing), it makes it easier to read for others.

Link to comment
Share on other sites

  • 1
_search = "apple" find "b" //-1 not found
_search = "apple" find "a" // 0 first element
_search = "apple" find "p" // 1 second element
_search = "apple" find "l" // 3 fourth element
_search = "apple" find "apple" // 0 first element

_search = "apple" find "banana" //-1 not found

 

Link to comment
Share on other sites

  • 0

The list box is showing the Display name already, im onto the actual spawning part of the dialog, after clicking the spawn button. just need to find a way of searching for multiples of the same item.

UG_Classname Vehicle is changing the displayname to classname, im using that to changed the selected item into the Classname, so I can compare that to the {_x select 0;}foreach UG_VehicleList; in the spawn vehicle script. 

 

 

Edited by Leigham
Added a bit of info
Link to comment
Share on other sites

  • 0
_itemsListUnique = [];


{
    if !(_x in _itemsList) then { //if _x not in the list, add it
        _itemsListUnique set [count _itemsListUnique,_x]; //store current var
    };
} forEach itemsToCheck;

//_itemsListUnique is now full with unique items, no duplicates

 

Link to comment
Share on other sites

  • 0

This is where im up too 

Say I wanted to search a players inventory for a random array of items 

 

ThisNestedArrays = [
["ItemClassname",[1,1,1,2,2,3]],
["ItemClassname",[2,3,3,4,5]],
["ItemClassname",[2,4,5,6,7,7,3]]
];

I know i would use a foreach loop to check something like, 

{
    _ItemClassname = _x select 0;
    _ItemArray = _x Select 1;
        //this is the part I need help with 
        //Something like
        {
        if(_x in (magazines player))then{ //Maybe ? I want it to check for all the Items in the players inventory before carrying on. 

        }foreach _ItemArray;
}foreach ThisNestedArrays;

Any Help would be Great, Can provide a Github, link for people who want a look.

 

Edit, forgot to put, each number represents an Item, and the multiples are there for a reason, so if I needed to search for a multiple of the same Item. 

Link to comment
Share on other sites

  • 0

Here's a hint, I see you know how to operate select command, so I'll let you figure out the rest

_itemsICareAbout =
[
    "item1",
    "item2",
    "item3",
    "item4",
    "item5"
];

_itemsOnPlayer = (magazines player) + (items player) + (weapons player);

_found = [];
_foundPlusCount = [];

//Find and Find + count
{
    if (_x in _itemsOnPlayer) then { //found item on player
	
		if !(_x in _found) then //but only if item is not already added
		
			_found pushBack _x; //Arma 3
			_found set [count _found, _x]; //Arma 2
			
			_y = _x; //can't use magic var further, so store it in _y or w/e
			_count = { _x == _y} count _itemsOnPlayer;
		
			_foundPlusCount pushBack [_y,_count]; //Arma 3
			_foundPlusCount set [count _found, [_y,_count]]; //Arma 2
		};
    };
} forEach _itemsICareAbout;


_found // array of items found on player - ["item2","item5"]
_foundPlusCount //nested arrays - [["item2",2],["item5",7]]

 

Link to comment
Share on other sites

  • 0

I Dont know whats going on, im not too sure on how to pass a variable out of the foreach loop, to be used outside of it after searching for the Items heres what I have 

disableSerialization;
_display = findDisplay 7770;
_ObjectTypeText = _display displayCtrl 7779;
_typeselection = lbCurSel _ObjectTypeText;
_ObjectTypeName = lbtext [7779,_typeselection];
_index = lbCurSel _ObjectTypeText; //index is required to access data
_selectionData = _ObjectTypeText lbData _index; //className we saved earlier in a data field

diag_log format ["%1-%2",_selectionData,_ObjectTypeName];
	
	{
		if(_selectionData == (_x select 0))then{
				Diag_Log format ["%1",_x];//Dialogging the Whole Chosen Array.
				_CraftingItems = _x Select 1;//Getting the Items Needed to Craft
				_CraftingSave = _x Select 2;//Is the Vehicle going to be saved ?
				_CraftingKrypto = _x Select 3;//How Much Krypto
				_CraftingIteems pushBack [_craftingItems]; //Arma 3
		};
		
			_itemsOnPlayer = (magazines player) + (items player) + (weapons player);
		
			_found = [];
			_foundPlusCount = [];
			
			//Find and Find + count
			{
				if (_x in _itemsOnPlayer) then { //found item on player
				
					if !(_x in _found) then { //but only if item is not already added
					
						_found pushBack _x; //Arma 3
						
						_y = _x; //can't use magic var further, so store it in _y or w/e
						_count = { _x == _y} count _itemsOnPlayer;
					
						_foundPlusCount pushBack [_y,_count]; //Arma 3
						systemchat format ["%1       %2,_Count,_foundPlusCount"];
					};
				};
			} forEach _CraftingIteems;
			
	}forEach UG_VehicleList;

 

Edited by Leigham
Link to comment
Share on other sites

  • 0

 

im not too sure on how to pass a variable out of the foreach loop, to be used outside of it after searching for the Items

plenty of ways to do it. One way is storing the variable somewhere, like a global variable, or send it back via function, like so:

funcion_amountOfApples = {
    _apples = 10; //set or calculate stuffs here
    
    _apples //return value
};

//now we have a function that returns a value, here's how we retrieve it:

//create variable with value  gathered from function above
_appleCount = call funcion_amountOfApples;

hint str _appleCount; //10

So essentially, you can process data in a loop, store it in some variable and either save it into global variable (not the best idea), or return it to a caller. Caller can then define a new local/global variable from data gathered.

Functions 101, really.

And ofc you can also send information to functions and ask for value in return:

//create function
_someFunction = {

    _input = _this; //collect data from input
    _fruit = _input select 0;
    _amount = _input select 1;
    
    //add random amount of fruits and return the result to caller

    _return = _amount + round random 5;
    
    _return
};

//now lets call it

_addFruits = ["apples",10] call _someFunction;

hint str _addFruits; //number between 10 and 15

 

Link to comment
Share on other sites

  • 0

So Basically, the best way to do it for me would be, one function to get the wanted array, and sort them into variables. then another function for each variable, that needs one, (IE. one for checking the inventory, one for the spawning of the crafted vehicle, and removing items, an such

 

Edit, 

Something like 
if the classname was "Mosquito_Epoch"


_GetChosenVehicleClassname = 
		{
			if(_selectionData == (_x select 0))then{
				_VehicleClassname = _x Select 0;
	
				_VehicleClassname
			};
		}foreach UG_VehicleList;
		
_VehicleClassname = Call _GetChosenVehiclename;

Systemchat format ["%1",_VehicleClassname];


Would that return "Mosquito_Epoch"  ?

 

Edited by Leigham
Link to comment
Share on other sites

  • 0

 

plenty of ways to do it. One way is storing the variable somewhere, like a global variable, or send it back via function, like so:

funcion_amountOfApples = {
    _apples = 10; //set or calculate stuffs here
    
    _apples //return value
};

//now we have a function that returns a value, here's how we retrieve it:

//create variable with value  gathered from function above
_appleCount = call funcion_amountOfApples;

hint str _appleCount; //10

So essentially, you can process data in a loop, store it in some variable and either save it into global variable (not the best idea), or return it to a caller. Caller can then define a new local/global variable from data gathered.

Functions 101, really.

And ofc you can also send information to functions and ask for value in return:

//create function
_someFunction = {

    _input = _this; //collect data from input
    _fruit = _input select 0;
    _amount = _input select 1;
    
    //add random amount of fruits and return the result to caller

    _return = _amount + round random 5;
    
    _return
};

//now lets call it

_addFruits = ["apples",10] call _someFunction;

hint str _addFruits; //number between 10 and 15

 

	_GetVehicleClassnameFromList = {
		{
			if(_selectionData == (_x select 0))then{
				_VehicleClassname = _x Select 0;
				diag_log format ["%1",_VehicleClassname]; // Gives the Right Classname;
			};
		}foreach UG_VehicleList;
		
		_VehicleClassname // Return Variable. // Is this right, in this position ?
	};
	
	_Vehicleinfo = call _GetVehicleClassnameFromList; // Calling the function to get the classname from the list. 
	Diag_Log format ["%1",_VehicleInfo]; //This One, Undefined Variable. 

This is what Ive put to first test what you have given me and alter in the way I think I need to. 

 

Edited by Leigham
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...