nickcage 0 Posted June 14, 2021 Hi all I'm creating a store with the GUI editor, I can find out what's wrong with the code I'm hoping someone can spot it. If you could provide a better code for this would be great? this script is linked to button action. _unit = player; _Crate = PlayerStorage; _playerweap = primaryWeapon player; _cash = Daddies; _cost = 120; Ak-12 = ["arifle_AK12_F"]; buttonSetAction [1601, {if (player hasWeapon "playerweap") then { _unit action ["PutWeapon", _Crate, primaryWeapon _unit]; Hint "Weapon Stored"; } then { if (_cash >= _cost) then { MoneySystem = MoneySystem - 120; player addweapon "arifle_AK12_F"; } else { Hint "Not Enough Dosh"; } }; Share this post Link to post Share on other sites
sarogahtyp 1108 Posted June 14, 2021 I guess first thing you did wrong is to post such piece of ... and ask for help for it. for readability (this is important for other people to read your code) : 1. use a code block in forum to post code, plz - you can find it in the icon bar on top and it has this icon: <> 2. indent lines after curly brackets, plz 3. use spaces between brackets and other things. Maybe others are but I m not willing to help before you did the formal basics... and I bet you ll find some mistakes yourself during that EDIT: Read this! https://community.bistudio.com/wiki/Code_Best_Practices#Best_practices EDIT2: Perhaps I was a little too harsh but if you didn't know 'bout above things then you know now, Share this post Link to post Share on other sites
nickcage 0 Posted June 14, 2021 1 hour ago, sarogahtyp said: I guess first thing you did wrong is to post such piece of ... and ask for help for it. for readability (this is important for other people to read your code) : 1. use a code block in forum to post code, plz - you can find it in the icon bar on top and it has this icon: <> 2. indent lines after curly brackets, plz 3. use spaces between brackets and other things. Maybe others are but I m not willing to help before you did the formal basics... and I bet you ll find some mistakes yourself during that EDIT: Read this! https://community.bistudio.com/wiki/Code_Best_Practices#Best_practices EDIT2: Perhaps I was a little too harsh but if you didn't know 'bout above things then you know now, Code structuration DRY: Don't Repeat Yourself. If you write the same code block or the same logic many times, export this code as a function and use parameters with it. If your code has too many levels, it is time to split and rethink it (e.g if (a) then { if (b) then { if (c) then { /* etc */ }; }; };...) Do NOT use macros as functions - these hinder readability. Use functions instead. Thankyou. 1 Share this post Link to post Share on other sites
nickcage 0 Posted June 14, 2021 2nd _playerWeapon is not a string, its a local variable. Share this post Link to post Share on other sites
sarogahtyp 1108 Posted June 14, 2021 you did not get it. An example for readable code: Spoiler /* Author: Sarogahtyp Description: Searches for near AT objects and tries to destroy those Full Multiplayer compatible. Main loop runs on machine where vehicle is local. If locality of vehicle changes then the script will change locality as well If a threat is found then it will be tracked and destroyed by a script on the machine where that threat is local. Params: _vec - object - vehicle to which the Arena system is to be attached _skill - number - values between 0 and 100 with 100 being the best skill possible. _reload_time - number - optional - time to reload the charge after it was fired (default 2 seconds) - if lower 0.5 seconds than it is changed to 0.2 seconds _range - number - optional - detection range (default 200 meters) _fire_max_range - number - optional - maximum interception distance (default 40 meters) _fire_min_range - number - optional - minimum interception distance (default 15 meters) _charge_height - number - optional - charge explodes on this height above vehicle (default 10 meters) */ private _dummy = params [ ["_vec", objNull, [objNull]], ["_skill", 80, [0]], ["_reload_time", 2, [0]], ["_range", 200, [0]], ["_fire_max_range", 40, [0]], ["_fire_min_range", 15, [0]], ["_charge_height", 10, [0]] ]; _skill = if (_skill > 100) then {100} else {_skill}; _skill = if (_skill <= 0) then {0.001} else {_skill}; _skill = if (_skill < 68) then { 0.074 * _skill } else { if (_skill < 87) then { 0.263 * _skill - 12.895 } else { if (_skill < 91) then { 2.5 * _skill - 207.5 } else { 4.444 * _skill - 384.444 }; }; }; //set debug mode for whole arena system here _debug = false; missionNamespace setVariable ["saro_arena_debug", _debug, true]; //script entered, logging it. if (isServer) then { if ( saro_arena_debug ) then { diag_log "SASPS AS-Server: Script started, nothing checked."; }; } else { if ( saro_arena_debug ) then { diag_log "SASPS AS-Client: Script started, nothing checked."; }; }; //no object given, leave. if (isNull _vec || {!alive _vec || !(_vec in vehicles) }) exitWith { if ( saro_arena_debug ) then { diag_log "SASPS AS: Object is not an alive vehicle. Exit."; }; }; //arena active allready? if ( !isNil {_vec getVariable "saro_arena_active"} ) then { if (_vec getVariable "saro_arena_active") exitWith { if ( saro_arena_debug ) then { diag_log "SASPS AS: EXIT, arena active allready"; }; }; }; //ensure that arena runs on the machine where the vehicle is local if (!local _vec) exitWith { if (isServer) then { if ( saro_arena_debug ) then { diag_log "SASPS AS-Server: Vehicle not local, starting AS on client"; }; //send to machine where the vehicle is local _dummy = _this remoteExec [ "saro_fnc_arena_start", (owner _vec) ]; } else { if ( saro_arena_debug ) then { diag_log "SASPS AS-Client: Vehicle not local starting AS on server"; }; //as we are not on server we have to send it to server which will transfer it to the correct client _dummy = _this remoteExec [ "saro_fnc_arena_start", 2 ]; }; }; //register running script instance on server to be able to handle locality changes if (!isServer) then { _dummy = [(getPlayerUID player), _vec, _reload_time, _range, _fire_max_range, _fire_min_range, _charge_height] remoteExec [ "saro_fnc_client_register", 2 ]; }; // mark arena active _vec setVariable ["saro_arena_active", true, true]; private _bbr = boundingBoxReal _vec; private _p1 = _bbr select 0; private _p2 = _bbr select 1; _maxHeight = abs ((_p2 select 2) - (_p1 select 2)) + _charge_height; _maxWidth = 0.5 * (abs ((_p2 select 0) - (_p1 select 0)) max abs ((_p2 select 1) - (_p1 select 1))) ; private "_fire_range"; _range = if (_range <= _fire_max_range) then {_fire_max_range + 1} else {_range}; //_sleep_time for main loop. missiles should not get closer than 120% of _fire_max_range in one sleep cycle //assuming 2000 m/s as max speed for armas AT threats private _sleep_time = 0.1 * (_range - _fire_max_range) / 2000; // mark loaded charge as not fired _vec setVariable ["saro_charge_fired", false, true]; // mark tracking as inactive _vec setVariable ["saro_tracking", false, true]; // give network time to broadcast variables sleep 0.5; while { !isNil {"_vec"} && { !isNull _vec && { (_vec getVariable "saro_arena_active") && (local _vec) && (alive _vec) } } } do { _incoming =[]; while { _vec getVariable "saro_tracking" } do { sleep _sleep_time; //check if something bad happend with vec if (isNil "_vec" || { isNull _vec || { !alive _vec } } ) exitWith { if ( saro_arena_debug ) then { diag_log "SASPS AS: Vehicle null, nil or dead. Inner While"; }; true }; if (_vec getVariable "saro_charge_fired") then { _vec setVariable ["saro_charge_fired", false, true]; sleep (_reload_time max 0.2); }; }; //check if something bad happend with vec if (isNil "_vec" || { isNull _vec || { !alive _vec } } ) exitWith { if ( saro_arena_debug ) then { diag_log "SASPS AS: Vehicle null, nil or dead. Exiting while"; }; }; _incoming = (_vec nearObjects["RocketBase",_range]) select { (vectorMagnitude velocity _x) > 25 }; if (_incoming isEqualTo []) then { _incoming append (_vec nearObjects["MissileBase",_range]) select { (vectorMagnitude velocity _x) > 25 }; if (_incoming isEqualTo []) then { _incoming append (_vec nearObjects["ShellBase",_range]) select { (vectorMagnitude velocity _x) > 25 }; }; }; if !(_incoming isEqualTo []) then { _vec setVariable ["saro_tracking", true, true]; private _threat = (_incoming#0); _lift_speed = random [80 , 100, 120]; // m/s _lift_time = _charge_height / _lift_speed; _lift_start_height = _maxHeight - _charge_height; if (!local _threat) then { if (isServer) then { //send to machine where the threat is local _dummy = [ _vec, _threat, _skill, _lift_start_height, _lift_speed, _fire_max_range, _fire_min_range, _lift_time, _maxWidth ] remoteExec [ "saro_fnc_track", (owner _threat) ]; } else { //as we are not on server we have to send it to server which will transfer it to the correct client _dummy = [ _vec, _threat, _skill, _lift_start_height, _lift_speed, _fire_max_range, _fire_min_range, _lift_time, _maxWidth ] remoteExec [ "saro_fnc_track", 2 ]; }; } else { //track locally _dummy = [ _vec, _threat, _skill, _lift_start_height, _lift_speed, _fire_max_range, _fire_min_range, _lift_time, _maxWidth ] spawn saro_fnc_track; }; }; }; //check if something bad happend with vec if !(isNil "_vec" || { isNull _vec } ) then { if ( !alive _vec ) exitWith { // vehicle is destroyed. Mark arena inactive _vec setVariable ["saro_arena_active", false, true]; if (isServer) then { if ( saro_arena_debug ) then { diag_log "SASPS AS-Server: Vehicle is dead. Exiting script.";}; } else { if ( saro_arena_debug ) then { diag_log "SASPS AS-Client: Vehicle is dead. Exiting script and unregistering";}; _dummy = [(getPlayerUID player), "_vec"] remoteExec [ "saro_fnc_client_unregister", 2 ]; }; }; } else { if (true) exitWith { if ( saro_arena_debug ) then { diag_log "SASPS AS: Vehicle is nil or null. Exiting script"; }; }; }; //loop ended but arena is active. Means vehicle is not local anymore and we start the whole script again. if (_vec getVariable "saro_arena_active") exitWith { // mark arena inactive _vec setVariable ["saro_arena_active", false, true]; // give network time to broadcast variable sleep 0.5; if (isServer) then { if ( saro_arena_debug ) then { diag_log "SASPS AS-Server: Vehicle not local anymore. Restarting script on client."; }; _dummy = _this remoteExec [ "saro_fnc_arena_start", (owner _vec) ]; } else { if ( saro_arena_debug ) then { diag_log "SASPS AS-Client: Vehicle not local anymore. Unregistering. Restarting script on server."; }; _dummy = [(getPlayerUID player), "_vec"] remoteExec [ "saro_fnc_client_unregister", 2 ]; _dummy = _this remoteExec [ "saro_fnc_arena_start", 2 ]; }; }; //script was disabled, leave normal if (isServer) then { if ( saro_arena_debug ) then { diag_log "SASPS AS-Server: Normal script exit. Exiting script.";}; } else { if ( saro_arena_debug ) then { diag_log "SASPS AS-Client: Normal script exit. Exiting script and unregistering";}; _dummy = [(getPlayerUID player), "_vec"] remoteExec [ "saro_fnc_client_unregister", 2 ]; }; the important things of the biki entry I linked prior: Quote - Whatever you do about your code's format, be consistent. - Choose an indentation format and stick to it. - Use empty space. Line return, spaces before and after brackets, if this improves readability, use it: space is free. - indent with two/four spaces or one tab. Do not mix these. 2 Share this post Link to post Share on other sites
sarogahtyp 1108 Posted June 14, 2021 because I am sometimes a friendly person: _unit = player; _Crate = PlayerStorage; _playerweap = primaryWeapon player; _cash = Daddies; _cost = 120; Ak-12 = ["arifle_AK12_F"]; // square bracket never gets closed buttonSetAction [ 1601, { // assuming _playerweap shall be the same as playerweap - you are checking if player // has the weapon which he has in his hand already. Why? // playerweap is global here but some lines above you declared it local. Is this intended? if ( player hasWeapon "playerweap" ) then { _unit action ["PutWeapon", _Crate, primaryWeapon _unit]; Hint "Weapon Stored"; } //then without if will never work, must be else I guess then { if (_cash >= _cost) then { MoneySystem = MoneySystem - 120; player addweapon "arifle_AK12_F"; } else { Hint "Not Enough Dosh"; // last closing bracket of if then else statement needs a semicolon } }; //missing a curly bracket to close the code block and a square bracket to close your button action Spoiler !!! today my kindness is boundless !!! private _unit = player; private _Crate = PlayerStorage; private _playerweap = primaryWeapon player; private _cash = Daddies; private _cost = 120; buttonSetAction [ 1601, { if ( _playerweap isEqualTo "" ) then { if (_cash >= _cost) then { MoneySystem = MoneySystem - 120; player addweapon "arifle_AK12_F"; } else { Hint "Not Enough Dosh"; }; } else { _unit action ["PutWeapon", _Crate, primaryWeapon _unit]; Hint "Weapon Stored"; }; }]; Share this post Link to post Share on other sites
nickcage 0 Posted June 14, 2021 2 hours ago, sarogahtyp said: because I am sometimes a friendly person: _unit = player; _Crate = PlayerStorage; _playerweap = primaryWeapon player; _cash = Daddies; _cost = 120; Ak-12 = ["arifle_AK12_F"]; // square bracket never gets closed buttonSetAction [ 1601, { // assuming _playerweap shall be the same as playerweap - you are checking if player // has the weapon which he has in his hand already. Why? // playerweap is global here but some lines above you declared it local. Is this intended? if ( player hasWeapon "playerweap" ) then { _unit action ["PutWeapon", _Crate, primaryWeapon _unit]; Hint "Weapon Stored"; } //then without if will never work, must be else I guess then { if (_cash >= _cost) then { MoneySystem = MoneySystem - 120; player addweapon "arifle_AK12_F"; } else { Hint "Not Enough Dosh"; // last closing bracket of if then else statement needs a semicolon } }; //missing a curly bracket to close the code block and a square bracket to close your button action Reveal hidden contents !!! today my kindness is boundless !!! private _unit = player; private _Crate = PlayerStorage; private _playerweap = primaryWeapon player; private _cash = Daddies; private _cost = 120; buttonSetAction [ 1601, { if ( _playerweap isEqualTo "" ) then { if (_cash >= _cost) then { MoneySystem = MoneySystem - 120; player addweapon "arifle_AK12_F"; } else { Hint "Not Enough Dosh"; }; } else { _unit action ["PutWeapon", _Crate, primaryWeapon _unit]; Hint "Weapon Stored"; }; }]; if ((currentWeapon player == (primaryWeapon player)) and (_cash >= _cost)) then { _SIMP action ["PutWeapon", _Crate, primaryWeapon _SIMP]; Hint "Weapon Stored"; sleep 4; hintSilent ""; }; if (_cash >= _cost) then { MoneySystem = MoneySystem - 120; sleep 3; player addweapon "arifle_AK12_F" } else { Hint "Not Enough Dosh"; sleep 4; hintSilent ""; }; I am trying master Share this post Link to post Share on other sites
sarogahtyp 1108 Posted June 14, 2021 try the code I put in the spoiler of my last post ... and please use code block in forum posts. Again you can find the code block icon on the top of the post you are currently editing and its icon is: <> 1 Share this post Link to post Share on other sites
pierremgi 4857 Posted June 14, 2021 The buttonSetAction can't work with local variables coming from outside. And action must be stringed. aHH BIKI! player setVariable ["cash",_cash]; buttonSetAction [ 1601, " player action ['PutWeapon',PlayerStorage, primaryWeapon player]; if (player getVariable ['cash',0] >= 120) then { MoneySystem = MoneySystem - 120; player addweapon 'arifle_AK12_F'; } else { Hint 'Not Enough Dosh'; }; "]; 1 Share this post Link to post Share on other sites