Trader Menu based on humanity

SchwEde

OpenDayZ Rockstar!
Hey,

i try to get different Menus on the trader based on the player humanity. I tried different ways but non of them worked.

This is what i tried so far:

server_trader.sqf
Code:
switch (true) do {
    case ((player getVariable "humanity") >= 5000): {
    menu_GUE_Woodlander2 = [
    [["Black Market Ammo",527]],
    [],
    "neutral"
    ];
    };
case ((player getVariable "humanity") <= -5000): {
    menu_GUE_Woodlander2 = [
    [["Black Market Weapons",526],["Black Market Ammo",527]],
    [],
    "neutral"
    ];
    };
};
I know that the switch case part is actually working its just "false" for the humanity parts.

I tried different parts with just
Code:
     case (true): {
    menu_GUE_Woodlander2 = [
    [["Black Market Weapons",526],["Black Market Ammo",527],["Smoke Grenades",668]],
    [],
    "neutral"
    ];
    };
This works perfect.
So why i cant get the variable in this file?

i appreciate any help.

Cheers

EDIT: Think i solved it, just to need fully test it all
 
Last edited:
The server_traders.sqf is compiled, before a player loggs in (at Mission Read). Because the configuration is done Client Side you should be able to create a Trigger and if the player walks into that trigger you call server_traders.sqf instead of calling it inside the .ini file.
 
you should try it this way, related to :
https://community.bistudio.com/wiki/Code_Optimisation#Make_it_pretty.

PHP:
call {
    _humanity = player getVariable "humanity";
    if (_humanity >= 5000) exitWith {
        menu_GUE_Woodlander2 = [  [["Black Market Ammo",527]], [], "neutral"];
    };
    if (_humanity <= -5000) exitWith {
        menu_GUE_Woodlander2 = [ [["Black Market Weapons",526], ["Black Market Ammo",527]], [], "neutral"];
    };
};

This only makes sence on loops. Why? Because at the current state the if breaks up, if true and then executes the code and then continues to the next code part. Adding "exitWith" instead of "then" would prevent other code from being parsed, unless the if is false, which happens only if user has between -4999 and +4999 Humanity. Also i would not use a switch case for 2 values only, because it's still a loop and uses more memory then a simple if statement.
 
i have a bit more then 2 cases ;)
Therefore i think its easier then a bunch of if commands.

i will try it with rockeumels code and tell the results :)

EDIT: Did i read right? Switch Case is much slower then if then?
So it is better for me to take 20 if then commands rather then a switch case with 20 cases?! o_O
 
Last edited:
This only makes sence on loops. Why? Because at the current state the if breaks up, if true and then executes the code and then continues to the next code part. Adding "exitWith" instead of "then" would prevent other code from being parsed, unless the if is false, which happens only if user has between -4999 and +4999 Humanity. Also i would not use a switch case for 2 values only, because it's still a loop and uses more memory then a simple if statement.
Switch works the same, it checks every case from top to bottom.
On the other hand switch is slower than ifthenelse.

That's what i read in the Wiki.
 
i have a bit more then 2 cases ;)
Therefore i think its easier then a bunch of if commands.

i will try it with rockeumels code and tell the results :)

Indeed, when you have more then just those 2 cases switch is better then if. If you have a large file you might also want to preprocess.
 
rockeumel: didnt worked this way =/
i will keep it how i solved ^^ its running and and dont see any problems this way so far.

Still curious about the speed of switch case and if then commands though o_O
 
https://community.bistudio.com/wiki/Code_Optimisation#Make_it_pretty.

i misunderstood something? the wiki says, "switch" is always worse(slower) then "if then else"

Partially true. A switch allways loops trough every case, even after the correct case is found / used it will continue reading others, while a if statement breaks up when inside the if, will then compile code and continue to next if. If the next if would be false or not match the requirements it would not read the code inside the if.

Now in this example we got the fact, that multiple cases can be true and they can be true the same time, say if player has over -5000 Humanity and under -5000 Humanity both cases would be true and executed. While the if statement would first check the IF's and check if the statement is true the switch would read it anyway, no matter if true or not and it would compile when true. Therefore a switch for multiple cases on this would be better :)

*Sorry, if that's a lil hard to understand*
 
How can a player have humanity above and under -5000 at the same time?

It exist three different cases: bandit, Hero or Survivor, and only one can be true.
 
im not a native english speaker but i understood this :D
so i will use switch case command instead of building a tower of if commands ^^
 
How can a player have humanity above and under -5000 at the same time?

It exist three different cases: bandit, Hero or Survivor, and only one can be true.

Sorry i meant above -5000 and under +5000. So basically anything from -4999 to +4999.
 
im not a native english speaker but i understood this :D
so i will use switch case command instead of building a tower of if commands ^^
sorry, i know my english knowlegde is not the best :) but i try and learn from day to day.
i'm german :)
If Else If Else If Else ...
If you can't escape this using a switch control structure, then try and rethink the functionality. Especially if only one option is needed to match.

On the other hand switch is slower than if then else. To keep tidiness of the switch and speed of if, use if exitWith combined with call:call { if (cond1) exitWith {//code 1}; if (cond2) exitWith {//code 2}; if (cond3) exitWith {//code 3}; //default code };

This is not realy bigger then a switch
PHP:
call {
    if (cond1) exitWith {//code 1};
    if (cond2) exitWith {//code 2};
    if (cond3) exitWith {//code 3};
    //default code
};

i always try to get my scripts slim and quick, that's all :)
 
Sorry i meant above -5000 and under +5000. So basically anything from -4999 to +4999.
Yes but in that case you use the default value, if no other condition is true.

PHP:
call {
    _humanity = player getVariable "humanity";
    if (_humanity >= 5000) exitWith {
        menu_GUE_Woodlander2 = [  [["Black Market Ammo",527]], [], "neutral"];
    };
    if (_humanity <= -5000) exitWith {
        menu_GUE_Woodlander2 = [ [["Black Market Weapons",526], ["Black Market Ammo",527]], [], "neutral"];
    };
// here comes code for the case nothing of the above is true
};
 
Yes but in that case you use the default value, if no other condition is true.

PHP:
call {
    _humanity = player getVariable "humanity";
    if (_humanity >= 5000) exitWith {
        menu_GUE_Woodlander2 = [  [["Black Market Ammo",527]], [], "neutral"];
    };
    if (_humanity <= -5000) exitWith {
        menu_GUE_Woodlander2 = [ [["Black Market Weapons",526], ["Black Market Ammo",527]], [], "neutral"];
    };
// here comes code for the case nothing of the above is true
};

Basically true and correct, but he has more then one statement as he said. :) By the way: If you use "exitWith" you maybe want to use [] spawn {}; instead of call.
 
Back
Top