Vandeanson 1677 Posted April 3, 2019 Good morning everyone, I am at that point again - where I believe understanding the use of "private" could save a lot of headache, but I just don't seem to understand it. For my WIP basebuilding mods material spawner, I use the second script below: The script selects a random "material type" that will then be spawned. I would like to assign a display name that will then be shown in a hint in-game (every time, material is picked up) to each of there variables. "VABB_ScrapMetal" is not nice to look at, hence it should be shown as "Scrap Metal". I use "switch" to assign a display name to each VABB_XXX material name. as this is code within code, It would not pass _RndMatDispln to the rest of the script. I have for now resorted to using RndMatDispln instead, but I would like to stick to local variables if possible. My question is - how would I make the the local variable _RndMatDispln available to the rest of the script? Switch code: Spoiler switch (_RandomMat) do { case "VABB_ScrapMetal": { RndMatDispln = "Scrap Metal" }; case "VABB_Cloth": { RndMatDispln = "Cloth" }; case "VABB_Wire": { RndMatDispln = "Wire" }; case "VABB_Netting": { RndMatDispln = "Netting" }; case "VABB_Nail": { RndMatDispln = "VABB_Nail" }; case "VABB_MetalBar": { RndMatDispln = "Metal Bar" }; case "VABB_ElectronicParts": { RndMatDispln = "Electronic Parts" }; case "VABB_MaterialScraps": { RndMatDispln = "Material Scraps" }; case "VABB_Screw": { RndMatDispln = "Screw" }; }; Full script: (_x stands for all building positions btw) Spoiler params ["_x"]; _mat = selectRandom ["Land_ToolTrolley_02_F","CUP_box_c", "Land_PaperBox_01_small_closed_brown_F","Land_PaperBox_01_small_destroyed_white_IDAP_F", "Land_PaperBox_01_small_destroyed_brown_IDAP_F"] createVehicle _x; _mat setPosATL _x; VABB_LoottoDel append [_mat]; [_mat,["Loot", { params ["_target", "_caller", "_actionId", "_arguments"]; [_target,_actionID] remoteexec ["removeaction",0,true]; _RandomMat = selectRandom [ "VABB_ScrapMetal", "VABB_Cloth", "VABB_Wire", "VABB_Netting", "VABB_Nail", "VABB_MetalBar", "VABB_ElectronicParts", "VABB_MaterialScraps", "VABB_Screw"]; switch (_RandomMat) do { case "VABB_ScrapMetal": { RndMatDispln = "Scrap Metal" }; case "VABB_Cloth": { RndMatDispln = "Cloth" }; case "VABB_Wire": { RndMatDispln = "Wire" }; case "VABB_Netting": { RndMatDispln = "Netting" }; case "VABB_Nail": { RndMatDispln = "VABB_Nail" }; case "VABB_MetalBar": { RndMatDispln = "Metal Bar" }; case "VABB_ElectronicParts": { RndMatDispln = "Electronic Parts" }; case "VABB_MaterialScraps": { RndMatDispln = "Material Scraps" }; case "VABB_Screw": { RndMatDispln = "Screw" }; }; _countMat = _caller getVariable [_RandomMat, 0]; _matNew = 1 + floor (random 5); _countMat = _caller getVariable [_RandomMat, 0]; _caller setVariable [_RandomMat, _countMat + _matNew]; _matTot = _caller getVariable [_RandomMat, false]; hint format ["+ %1 %3, %2 in Total",_matNew,_matTot,RndMatDispln]; }, [],3,true,true,"","true",5,false,"",""]] remoteExec ["addAction",0,true]; VABB_BuildPosArray = []; I have tried to use "private" as per BI forums but it seems that I don't get it yet. Thanks for any help on this! Cheers VD Share this post Link to post Share on other sites
sarogahtyp 1109 Posted April 3, 2019 1. do not use _x as variable name because it is a magic variable 2. if you define _RndMatDispln like this private _RndMatDispln = ""; at the beginning of your script then it is available to the whole script. 1 Share this post Link to post Share on other sites
Dedmen 2724 Posted April 3, 2019 When you access a local variable, Arma searches current scope, and then goes through all the parent scopes till it finds a variable with that name. If it doesn't find one, it either creates a new one in the current scope (when using _var=..) or returns nil. private keyword just disables that lookup and forces Arma to only look in the current scope, and ignore the parents. 2 hours ago, Vandeanson said: My question is - how would I make the the local variable _RndMatDispln available to the rest of the script? Well for one 2 hours ago, Vandeanson said: case "VABB_ScrapMetal": { RndMatDispln = "Scrap Metal" }; That's not a local variable in your script. Let's pretend it is local. To get it to be visible outside of the switch case, you need to define it outside and above the switch case. Like this: private _RndMatDispln = ""; //The "" is the default value, in case none of the switch cases match. switch (_RandomMat) do { case "VABB_ScrapMetal": { _RndMatDispln = "Scrap Metal" }; }; 1 hour ago, sarogahtyp said: 1. do not use _x as variable name because it is a magic variable Doesn't really matter and doesn't hurt. Magic variables are usually created a private variables so they wouldn't overwrite the other _x. It just looks ugly. 2 Share this post Link to post Share on other sites
sarogahtyp 1109 Posted April 3, 2019 2 minutes ago, Dedmen said: Doesn't really matter and doesn't hurt. Magic variables are usually created a private variables so they wouldn't overwrite the other _x. who tried to tell me bout readability? I don't remember... It hurts if one tries to access it in a count/forEach/select loop while assuming that it is the _x of the outer scope. Share this post Link to post Share on other sites
Grumpy Old Man 3550 Posted April 3, 2019 1 minute ago, sarogahtyp said: It hurts if one tries to access it in a count/forEach/select loop while assuming that it is the _x of the outer scope. That's a user error then. Cheers Share this post Link to post Share on other sites
sarogahtyp 1109 Posted April 3, 2019 4 minutes ago, Grumpy Old Man said: That's a user error then. Cheers sure. but may happen. better to not use _x as a variable name...especially for a beginner who may read this thread Share this post Link to post Share on other sites
Belbo 462 Posted April 3, 2019 private _RndMatDispln = ""; //The "" is the default value, in case none of the switch cases match. _RndMatDispln = switch (_RandomMat) do { case "VABB_ScrapMetal": { "Scrap Metal" }; }; RndMatDispln doesn't have to be a global variable. You don't have to define it inside of the switch, but make the statement of the switch the definition of _RndMatDispln respectively. 2 Share this post Link to post Share on other sites
Dedmen 2724 Posted April 3, 2019 17 minutes ago, Belbo said: RndMatDispln doesn't have to be a global variable. You don't have to define it inside of the switch, but make the statement of the switch the definition of _RndMatDispln respectively. And to go one step further: private _RndMatDispln = switch (_RandomMat) do { case "VABB_ScrapMetal": { "Scrap Metal" }; }; Looking at the original script. I don't see the need for the switch case. selectRandom [ ["VABB_ScrapMetal", "Scrap Metal"], ["VABB_Cloth", "Cloth"], ["VABB_Wire", "Wire"], ["VABB_Netting", "Netting"], ["VABB_Nail", "Nail"], ["VABB_MetalBar", "Metal Bar"], ["VABB_ElectronicParts", "Electronic Parts"], ["VABB_MaterialScraps", "Material Scraps"], ["VABB_Screw", "Screw"] ] params ["_RandomMat","_RndMatDispln"] 1 1 Share this post Link to post Share on other sites
tanin69 17 Posted April 3, 2019 Hi Vandeanson, In addition, you should read this http://killzonekid.com/arma-scripting-tutorials-scopes/ KK's blog is definitly one if the best ressource about Arma scripting (many thanks to you Killzone Kid !!!!) 1 1 Share this post Link to post Share on other sites
Vandeanson 1677 Posted April 3, 2019 @all Thank you very much for all the valuable input! 9 hours ago, Dedmen said: selectRandom [ ["VABB_ScrapMetal", "Scrap Metal"], ["VABB_Cloth", "Cloth"], ["VABB_Wire", "Wire"], ["VABB_Netting", "Netting"], ["VABB_Nail", "Nail"], ["VABB_MetalBar", "Metal Bar"], ["VABB_ElectronicParts", "Electronic Parts"], ["VABB_MaterialScraps", "Material Scraps"], ["VABB_Screw", "Screw"] ] params ["_RandomMat","_RndMatDispln"] does the job for me and the answers helped me to understand some things that I can use in the future as well;) Going to read into KKs article indeed, thanks for that! Cheers VD 1 Share this post Link to post Share on other sites