Jump to content
sbondo1234

Counting Kills & Saving to ProfileNamespace

Recommended Posts

I currently have this:

//initPlayerLocal.sqf
private ["_sg_kills", "_sg_deaths", "_player"];

_player = _this select 2;

if (isNil {profileNamespace getVariable "sg_kills"}) then {
	profileNamespace setVariable ["sg_kills", 0];
};

_lkills = (profileNamespace getVariable "sg_kills");
_player setVariable ["sg_kills", _lkills];

player addMPEventHandler ["MPKilled", {null = execVM "scripts\sg_stats.sqf";}];

So I have created the variable sg_kills and it is set to 0 for newcomers and if you have already played it finds what it is.

 

I can only imagine that this works (please tell if I have gotten something wrong above too).

 

I am having trouble finding out what I would do in my sg_stats.sqf. I have my variable sg_kills and I have tried adding onto it, but it always ends in errors and it not doing what I wanted it to.

 

I tried:

//sg_scripts.sqf
player addMPEventHandler ["MPKilled" {sg_kills + 1}];

hint format [sg_kills];

Any help with what I should add into the sg_stats.sqf would be very much so appreciated! I am just trying to make it add a kill onto the sg_kills variable and then display a hint after every kill you get. I am using profileNamespace because I want these statistics to save even after the mission ends ( It's a DM game mode ).

Share this post


Link to post
Share on other sites

I am assuming you are just trying to keep track of kills so this should lead you in the right direction

// initPlayerLocal.sqf
private ["_sg_kills"];

// Check if player has kills saved
if (isNil {profileNamespace getVariable "sg_kills"}) then {

	// Set first instance of variable
	profileNamespace setVariable ["sg_kills", 0];
};

// Get players current kill count and set to player
_sg_kills = profileNamespace getVariable ["sg_kills", 0];
player setVariable ["sg_kills", _sg_kills, true]; // True lets everyone on the server know this variable was updated

// Run on all clients in multiplayer when player is killled
player addMPEventHandler ["MPKilled", {null = execVM "scripts\sg_stats.sqf";}];
// sg_stats.sqf - executed on every client when player it is assigned to is killed
params [["_unitKilled", objNull], ["_killer", objNull]]; // https://community.bistudio.com/wiki/Arma_3:_Event_Handlers#MPKilled
private ["sg_kills"];

// Check if you are the killer
if (player isEqualTo _killer) then {
	
	// Update kill count
	_sg_kills = (player getVariable ["sg_kills", 0]) + 1; // 0 is the default value
	profileNamespace setVariable ["sg_kills", _sg_kills]; // ProfileNamespace cannot use true with setVariable
	player setVariable ["sg_kills", _sg_kills, true];

	// Inform user of kill
	if (_unitKilled in allUnits) then {
		hint format["You just killed %1, and now your current kill count is %2", name _unitKilled, _sg_kills];
	};
};

// Check if your are the one killed
if (player isEqualTo _unitKilled) then {
	
	// Code here
};
  • Thanks 1

Share this post


Link to post
Share on other sites

@NumbNutsJunior

Thanks for this... I have put everything where it needs to be to test whether it was working and I got an error as soon as I loaded into the MP game.

Error: http://prntscr.com/mpzgt8, I'm not really good at decrypting arma errors, some help would be great.

 

Everything around line 55:

// Check if player has kills saved
if (isNil {profileNamespace getVariable "sg_kills"}) then {
    profileNamespace setVariable ["sg_kills", 0];
};

It would be nice if you could explain this line as well:

player setVariable ["sg_kills", _sg_kills, true]; // True lets everyone on the server know this variable was updated

Does everyone have to know when one person gets a kill, what would happen if this was set to false?

Share this post


Link to post
Share on other sites
59 minutes ago, sbondo1234 said:

@NumbNutsJunior

Thanks for this... I have put everything where it needs to be to test whether it was working and I got an error as soon as I loaded into the MP game.

Error: http://prntscr.com/mpzgt8, I'm not really good at decrypting arma errors, some help would be great.

 

Everything around line 55:


// Check if player has kills saved
if (isNil {profileNamespace getVariable "sg_kills"}) then {
    profileNamespace setVariable ["sg_kills", 0];
};

It would be nice if you could explain this line as well:


player setVariable ["sg_kills", _sg_kills, true]; // True lets everyone on the server know this variable was updated

Does everyone have to know when one person gets a kill, what would happen if this was set to false?

 

Change this line in InitPlayerLocal 

player addMPEventHandler ["MPKilled", {null = execVM "scripts\sg_stats.sqf";}];

to

 

player addMPEventHandler ["MPKilled", {null = _this execVM "scripts\Aquaman_PlayerStats.sqf";}];

 

Share this post


Link to post
Share on other sites
4 hours ago, sbondo1234 said:

It would be nice if you could explain this line as well:


player setVariable ["sg_kills", _sg_kills, true]; // True lets everyone on the server know this variable was updated

Does everyone have to know when one person gets a kill, what would happen if this was set to false?

It just allows other players to use the same command getVariable but instead of player they could replace the unit of someone who updated with true so if you did something like _killer getVariable ["sg_kills", 0] and find out how many kills that player has if you update using false then only your computer can get the variable that you just set with setVariable. It depends on if you want others to have access or not that is all.

  • Like 1

Share this post


Link to post
Share on other sites
On 2/25/2019 at 1:24 AM, NumbNutsJunior said:

// Run on all clients in multiplayer when player is killled 

player addMPEventHandler ["MPKilled", {null = execVM "scripts\sg_stats.sqf";}];

 

The way you are calling the event handler in initPlayerLocal you may want to change to:

player addMPEventHandler ["MPKilled", {_this execVM "scripts\sg_stats.sqf";}];

_this is a special "magic" variable that holds the arguments/parameters in this case _this is an array of the unit killed and the killer passed from the event handler MPKilled

  • Like 1

Share this post


Link to post
Share on other sites

Good work @NumbNutsJunior

Couple of notes though:

params [["_unitKilled", objNull], ["_killer", objNull]];

Having objNull as default value is pointless. Even if you check it with isNull command somewhere since you can just use isNil command.

private ["sg_kills"];

Typo in sg_stats.sqf file. Add _ in front of the variable

private ["_sg_kills"];
if (isNil {profileNamespace getVariable "sg_kills"}) then {

No real need for this as you use getVariable with default value.

Share this post


Link to post
Share on other sites
1 hour ago, HazJ said:

Good work @NumbNutsJunior

Couple of notes though:


params [["_unitKilled", objNull], ["_killer", objNull]];

Having objNull as default value is pointless. Even if you check it with isNull command somewhere since you can just use isNil command.


private ["sg_kills"];

Typo in sg_stats.sqf file. Add _ in front of the variable


private ["_sg_kills"];

if (isNil {profileNamespace getVariable "sg_kills"}) then {

No real need for this as you use getVariable with default value.

When I change the variable in sg_stats.sqf I get an error similar to this http://prntscr.com/mpzgt8, but something on line 53 instead.

 

I have also just realised a weird error in intelliJ around this code:

// Check if player has kills saved
if (isNil {profileNamespace getVariable "sg_kills"}) then {
    profileNamespace setVariable ["sg_kills", 0];
};

Error: <statement> expected, got ''.

 

I don't know if you can make sense of this, but I have no idea what it is talking about.

Share this post


Link to post
Share on other sites

Paste full code please. Also, how do you call it? execVM? From where? Please confirm.

Share this post


Link to post
Share on other sites
3 hours ago, HazJ said:

Paste full code please. Also, how do you call it? execVM? From where? Please confirm.

//initPlayerLocal.sqf
// Check if player has kills saved
if (isNil {profileNamespace getVariable "sg_kills"}) then {
    profileNamespace setVariable ["sg_kills", 0];
};

// Get players current kill count and set to player
_sg_kills = profileNamespace getVariable ["sg_kills", 0];
player setVariable ["sg_kills", _sg_kills, true]; // True lets everyone on the server know this variable was updated

// Run on all clients in multiplayer when player is killed
player addMPEventHandler ["MPKilled", {null = _this execVM "scripts\sg_stats.sqf";}];
// sg_stats.sqf
params [["_unitKilled", objNull], ["_killer", objNull]]; // https://community.bistudio.com/wiki/Arma_3:_Event_Handlers#MPKilled
private ["sg_kills"];

// Check if you are the killer
if (player isEqualTo _killer) then {

	// Update kill count
	_sg_kills = (player getVariable ["sg_kills", 0]) + 1; // 0 is the default value
	profileNamespace setVariable ["sg_kills", _sg_kills]; // ProfileNamespace cannot use true with setVariable
	player setVariable ["sg_kills", _sg_kills, true];

	// Inform user of kill
	if (_unitKilled in allUnits) then {
		hint format["You just killed %1, and now your current kill count is %2", name _unitKilled, _sg_kills];
	};
};

// Check if your are the one killed
if (player isEqualTo _unitKilled) then {
    hint "You Are Dead";
};

sg_stats.sqf is called in initPlayerLocal

Share this post


Link to post
Share on other sites

Sorry for the delay. Was in a sleep coma haha.

initPlayerLocal.sqf

if (isNil {profileNamespace getVariable "sg_kills"}) then
{
	profileNamespace setVariable ["sg_kills", 0];
};

_sg_kills = profileNamespace getVariable ["sg_kills", 0];
player setVariable ["sg_kills", _sg_kills, true];

{
	_x addEventHandler ["Killed", {_this execVM "sg_stats.sqf";}];
} forEach allUnits;

sg_stats.sqf:

params ["_unit", "_killer", "_instigator", "_useEffects"];

if (player isEqualTo _killer) then
{
	private _sg_kills = (player getVariable ["sg_kills", 0]) + 1;
	profileNamespace setVariable ["sg_kills", _sg_kills];
	player setVariable ["sg_kills", _sg_kills, true];
	hintSilent format ["You killed %1. Your kill count is now %2.", name _unit, _killer getVariable ["sg_kills", 0]];
};

if (player isEqualTo _unit) then
{
	hintSilent "You are dead.";
};

 

  • Like 2

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

×