Jump to content
Sign in to follow this  
EtherNight

Export a value from variable in function

Recommended Posts

Hello,

I would to create a script life point with AddEventHandler HandleDamage, I need to subtract the variable "maxlife" each time this object get damage from anybody.
But I don't how to.
 

_bossrandom = floor(random 1001);    
this setVehicleVarName str _bossrandom;    
_boss = this; 
dommageext = 0;  
maxlife = (10000 - dommageext); 
    
  
_boss addEventHandler ["HandleDamage", { 
params ["_unit", "_dommage"];    
    _unit = _this select 0;   
    _dommage = _this select 2;    
    _unit setDamage 0; 
    _life = maxlife; 
    if (_life < 0) then {    
        _life = 0;    
    };   
   
    _unit setDamage 0;    
    
    if (_life <= 0) then {    
        _unit setDamage 1;    
    };  
    dommageext = _dommage; 
publicVariable "dommageext"; 
hint format ["Variable : %2\nDommages reçus : %1\nVie restante : %3\n dommage externe %4", _dommage, _unit, _life, dommageext];  
}];



 

Share this post


Link to post
Share on other sites

Hi.
I'm not sure if this helps, but if you just want to have an enemy who can endure more than an ordinary enemy, you could alternatively reduce the incoming damage instead of managing a separate variable for the HP.

 

If I understand the documentation for 'HandleDamage' correctly, you can override the received damage value by overriding the '_damage' parameter and returning this value:
https://community.bistudio.com/wiki/Arma_3:_Event_Handlers#HandleDamage

this addEventHandler ["HandleDamage", {
	params ["_unit", "_selection", "_damage", "_source", "_projectile", "_hitIndex", "_instigator", "_hitPoint", "_directHit"];
	_damage = _damage * 0.1;
	_damage;
}];

 

(This is not tested!)

 

The following are just tips:

A small tip that I can give is that it's not necessary to receive the parameters using 'select.'
After you have received the parameters using 'params,' you can simply access them by their names. For example:

params ["_unit", "_damage"];
_unit setDamage 0;

 

 

 

Another thing I noticed is that the variable you are trying to publish with 'publicVariable' (dommage) is not the same as _dommage.

Notice the underscore (_) before the name. The underscore indicates that it's a private variable and is only known within your script's scope.

You cannot publish a private variable with 'publicVariable.' This can only be done with public variables, which are the ones without an underscore.

However, public variables have the disadvantage that they can only exist once and will be overwritten in any other script.




I'd like to mention that I am still a beginner in SQF, so please take my statements with a grain of salt.

Share this post


Link to post
Share on other sites
10 minutes ago, Joshua9797 said:

Another thing I noticed is that the variable you are trying to publish with 'publicVariable' (dommage) is not the same as _dommage.

 

I believe the syntax used for public variable is correct, as the suggested steps for that were followed:

GlobalVariable = _myLocalVariable;
publicVariable "GlobalVariable";
https://community.bistudio.com/wiki/Variables#Global_Scope

So the private was called for Global, prior to use the new Global as public, and it ended up like:
dommageext = _dommage;   // exporting the _private as Global;
publicVariable "dommageext"   // exporting the Global as finally public;
😉

Share this post


Link to post
Share on other sites
On 9/23/2023 at 8:38 AM, EtherNight said:

 



_boss addEventHandler ["HandleDamage", { 
params ["_unit", "_dommage"];    
    _unit = _this select 0;   
    _dommage = _this select 2;    
    _unit setDamage 0; 


 

As far as I have learnt, you need to follow the original params for handlers, so you cannot change "_dAmage" into "_dOMmage" - even if you have set "_dommage = _this # 2" a few lines later.
https://community.bistudio.com/wiki/Arma_3:_Event_Handlers#HandleDamage

It's most likely you will need to create a global "count" for the boss' full life, and use eventHandler to remove "points" from that life.
For instance, this is what I use for a "knock-out" script, which works as oppose-counting from what you might need:

You have already done that
😳

Spoiler

 


//Global variable to keep track of the execution count in a init.sqs (or maybe player's init field?)
// Initialize the global variable to store the execution count.
knockoutCount = 0;


    player addEventHandler ["action", {
    _action = _this select 3;

    // Is action being executed is "Action_knockout."
    if (actionName _action == "Action_knockout") then {
        // adds to global execution count.
        knockoutCount = knockoutCount + 1;

        // Has player has executed the action 3+ times?
        if (knockoutCount >= 3) then {
            systemChat "Undercover at risk!";
            player setUnitTrait ["CamouflageCoef",1];
            player setUnitTrait ["AudibleCoef",1];
        }
    }
}];

 

 

 

 

Edited by JCataclisma

Share this post


Link to post
Share on other sites
7 minutes ago, JCataclisma said:

I believe the syntax used for public variable is correct, as the suggested steps for that were followed:

GlobalVariable = _myLocalVariable;
publicVariable "GlobalVariable";
https://community.bistudio.com/wiki/Variables#Global_Scope

So the private was called for Global, prior to use the new Global as public, and it ended up like:
dommageext = _dommage;   // exporting the _private as Global;
publicVariable "dommageext"   // exporting the Global as finally public;
😉

Oh, right. I don't know how I overlooked that. It's even the step right above the publicVariable.
thanks for pointing that out.😅

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
Sign in to follow this  

×