Jump to content
cybercoco

Multiple conditions for variables in hint

Recommended Posts

I am trying to create a hint which will display several variable, for exemple _health and _ammo.

That's easily done, however what I would like to do is display the variable with a specific color, according to the importance...

 

For exemple if player is at 50% health and as 100% ammo on him then hint _health orange and _ammo green.

If the player is at 25% health and 50% ammo, then it will be _health in red and _ammo in orange.

 

I don't want to make a "if, then" statement for each possible way. Is there a smarter solution ?

 

Thanks in advance,

 

C.Coco

Share this post


Link to post
Share on other sites

Use structured text to colorize your code. The color can be determined using a simple switch statement.

_health = 0.7;
_ammo = 23;
_ammoMax = 100;

_fnc_getColor = {
    switch true do {
        case (_this < 0.25): {
            "#ff0000"    //red
        };
        
        case (_this < 0.5): {
            "#ff8800"    //orange
        };
        
        case (_this < 0.75): {
            "#ffff00"    //yellow
        };
        
        default {
            "#00ff00"    //green
        };
    };
};

hintSilent parseText format ["
    <t align='left'>Health:</t><t align='right' color='%2'>%1</t><br/>
    <t align='left'>Ammo:</t><t align='right' color='%4'>%3</t>
", _health, _health call _fnc_getColor, _ammo, (_ammo / _ammoMax) call _fnc_getColor];
  • Like 1

Share this post


Link to post
Share on other sites

 

Use structured text to colorize your code. The color can be determined using a simple switch statement.

_health = 0.7;
_ammo = 23;
_ammoMax = 100;

_fnc_getColor = {
    switch true do {
        case (_this < 0.25): {
            "#ff0000"    //red
        };
        
        case (_this < 0.5): {
            "#ff8800"    //orange
        };
        
        case (_this < 0.75): {
            "#ffff00"    //yellow
        };
        
        default {
            "#00ff00"    //green
        };
    };
};

hintSilent parseText format ["
    <t align='left'>Health:</t><t align='right' color='%2'>%1</t><br/>
    <t align='left'>Ammo:</t><t align='right' color='%4'>%3</t>
", _health, _health call _fnc_getColor, _ammo, (_ammo / _ammoMax) call _fnc_getColor];

Nice, thank you Heeeere's johnny!, I didn't know we could use %1 inside the style of <t>.

Also, If I understand this correctly _fnc_getColor is a function right ?

And why have "_ammo / _ammoMax" ?

 

C.Coco

Share this post


Link to post
Share on other sites

Nice, thank you Heeeere's johnny!, I didn't know we could use %1 inside the style of <t>.

You can use %1, %2 ... anywhere inside a formatted string. The <t> only becomes relevant in the parseText command. The format command sees the string just as any other string.

 

Also, If I understand this correctly _fnc_getColor is a function right ?

Correct. You don't need to give functions any special name, but I give them the prefix "_fnc_" for local functions and "fnc_" for global functions respectively. That's simply a naming convention to identify them quickly as functions.

Share this post


Link to post
Share on other sites

Thank you for the explanation. Is there a way to give two inputs (or more) and get multiple outputs.

Does it work if I input a array instead of a variable in the function ?

 

Another thing, am I allowed to do this ?

    _health = 0.7;
    _ammo = 0.4;
     
    _fnc_getFile = {
        switch true do {
            case (_this < 0.5 and _this == _health): {
                "healthlow.sqf"
            };

            case (_this > 0.5 and _this == _health): {
                "healthhigh.sqf"
            };

            case (_this < 0.5 and _this == _ammo): {
                "ammolow.sqf"
            };

            case (_this > 0.5 and _this == _ammo): {
                 "ammohigh.sqf"
            };
        };
    };
    
    player addaction ["Health Status", _health call _fnc_getFile]
    player addaction ["Ammo Status", _ammo call _fnc_getFile]

Share this post


Link to post
Share on other sites

Thank you for the explanation. Is there a way to give two inputs (or more) and get multiple outputs.

Does it work if I input a array instead of a variable in the function ?

You may invoke a function with any parameter you like. If you want to pass an array, you just need to handle it as an array inside the function.

 

Another thing, am I allowed to do this ?

- snip -

Well, I see what you want to do, but " _health " and "_ammo" do not exist inside the "_fnc_getFile" function, so this is syntactically not correct. Also, if you compare two variables, the comparison will only be regarding the value. So even i f your code would work, it would always go into one of the two first cases if "_health " and "_ammo" were the same value.

What you could do instead is pass a second parameter, like the following:

 

I do not recommend execute files directly though, because that's as if you were calling the file with execVM which means that everytime the file is executed, it's firstly read, then interpreted ("compiled") and then and then executed - which is highly inefficient.

_health = 0.7;
_ammo = 0.4;
 
_fnc_getFile = {
	params ["_value", "_name"];
	
	switch true do {
		case (_value < 0.5 and _name == "health"): {
			"healthlow.sqf"
		};

		case (_value > 0.5 and _name == "health"): {
			"healthhigh.sqf"
		};

		case (_value < 0.5 and _name == "ammo"): {
			"ammolow.sqf"
		};

		case (_value > 0.5 and _name == "ammo"): {
			"ammohigh.sqf"
		};
	};
};

player addaction ["Health Status", [_health, "health"] call _fnc_getFile];
player addaction ["Ammo Status", [_ammo, "ammo"] call _fnc_getFile];

If the code inside these files is mostly similar, you may wanna think about combining them to one file and make the code dynamic enough to distinguish the different cases using parameters.

Share this post


Link to post
Share on other sites

Thank you for your help,

Well, I see what you want to do, but " _health " and "_ammo" do not exist inside the "_fnc_getFile" function, so this is syntactically not correct. Also, if you compare two variables, the comparison will only be regarding the value. So even i f your code would work, it would always go into one of the two first cases if "_health " and "_ammo" were the same value.

I understand what you're saying. I think I'm going to go with combining the two similar files into one.

I may have to send variables to this unique file, is that correct ?

 

Thanks again !

And Happy Christmas to you all !!!

Share this post


Link to post
Share on other sites

Thank you for your help,

I understand what you're saying. I think I'm going to go with combining the two similar files into one.

I may have to send variables to this unique file, is that correct ?

Yes, that's what I meant with "parameters".

 

Thanks again !

And Happy Christmas to you all !!!

Happy Christmas to you, too!

Share this post


Link to post
Share on other sites

Will it work if I add this code in the new file :

#include "blablabla.sqf" //the first file

And then use the local variables in this same file ?

Share this post


Link to post
Share on other sites
Guest

Nope.

For exemple in the first part where you have the local vars.

You call the second part like this.

[_health,_ammo] execVM "blabla.sqf";
[_args] execVM "mysqf.sqf";
[_moreargs] call fnc_myfnc;
And in your second part you retrieve your vars like this.

_health = _this select 0;
_ammo = _this select 1;
// You may use BIS_fnc_param to define a default value for your checks after
Hopes that helps.

Happy Christmas

Share this post


Link to post
Share on other sites

That doesn't work for me

 

test\export.sqf

_var1 = "I am var one";
_var2 = "I am var two";
[_var1,_var2] execVM "test\export.sqf";
[_args] execVM "test\export.sqf";

test\import.sqf

_var1 = _this select 0;
_var2 = _this select 1;
hint format ["Testing vars : %1 and %2",_var1, _var2];

Share this post


Link to post
Share on other sites

I figured it out after a little while...

 

test\export.sqf

_var1 = "1";
_var2 = "2";
hint format ["Testing vars : %1 and %2",_var1, _var2];
[_var1,_var2] execVM "test\import.sqf";
hint "vars exported";

// -------TESTING IF SCRIPT WORKS AFTER EXECUTING THE OTHER-------
player sidechat "hello1";
sleep 2;
player sidechat "hello2";
sleep 2;
player sidechat "hello3";
sleep 2;
player sidechat "hello4";
sleep 2;
player sidechat "hello5";
sleep 2;
// -------OF COURSE IT DOES-------

test\import.sqf

sleep 2;
hint "vars imported";
sleep 2;
_var1 = _this select 0;
_var2 = _this select 1;
hint format ["Testing vars : %1 and %2",_var1, _var2];

However I don't understand why you added that (below) in the first script.

[_args] execVM "mysqf.sqf"; // ?
[_moreargs] call fnc_myfnc; // ?

Thanks for the help harmdhast

Share this post


Link to post
Share on other sites

For exemple if player is at 50% health and as 100% ammo on him then hint _health orange and _ammo green.

If the player is at 25% health and 50% ammo, then it will be _health in red and _ammo in orange.

 

 

Create a series of separate triggers for each possibility.

 

When the variables are met the appropriate trigger will fire.

.

Share this post


Link to post
Share on other sites

Create a series of separate triggers for each possibility.

 

When the variables are met the appropriate trigger will fire.

.

Thanks very much Joe98, but problem's already solved.

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

×