Jump to content
Sign in to follow this  
Nebulazer

Need help, this is giving me the points instead of the player who killed.

Recommended Posts

I am trying to make it so players get money and xp when they get kills on AI and enemy players but this is for some reason it i giving me the money and xp when I run the mission on my computer. I don't have it running on a dedicated server yet but the description of !ifDedicated made it seem like it would work anyway.

//Get Cash and xp when you kill
if (!isDedicated)then{
_unit=(_this select 0);

_unit addEventHandler ["killed", {
_unit=        (_this select 0); 
_killer=    (_this select 1); 


if (isPlayer _killer)then{ 

	p_exp=p_exp+100;
       cash=cash+100;
	execVM "Levels.sqf";
       hint "$+100 and +100XP";

(uiNameSpace getVariable "myUI_DollarTitle") ctrlSetText format ["Money: $%1",cash];

_unit removeAllEventHandlers "killed"; 
           };
       }];
   };  

Share this post


Link to post
Share on other sites
I am trying to make it so players get money and xp when they get kills on AI and enemy players but this is for some reason it i giving me the money and xp when I run the mission on my computer. I don't have it running on a dedicated server yet but the description of !ifDedicated made it seem like it would work anyway.

//Get Cash and xp when you kill
if (!isDedicated)then{
_unit=(_this select 0);

_unit addEventHandler ["killed", {
_unit=        (_this select 0); 
_killer=    (_this select 1); 


if (isPlayer _killer)then{ 

	p_exp=p_exp+100;
       cash=cash+100;
	execVM "Levels.sqf";
       hint "$+100 and +100XP";

(uiNameSpace getVariable "myUI_DollarTitle") ctrlSetText format ["Money: $%1",cash];

_unit removeAllEventHandlers "killed"; 
           };
       }];
   };  

I'm using this code from Techlethal for my single player mission, but it might help you with yours or at least get you on the right track. It's pretty much the same, but the if (isPlayer _killer) might need to be changed.

TL_fnc_enemyKilled = 
{
_victim = _this select 0;
_attacker = _this select 1;

// Only increase the kill count if the kill was made by the player
	if(_attacker == player) then 
{
    call TL_fnc_updateKillCount;
};
};

Edit:

I realize now that this probably won't help you at all. If I could delete this post I would.

Edited by Treoctone

Share this post


Link to post
Share on other sites

nebulazer how are you adding (call/spawn) this to the units?

it looks to me that this will run on all clients and the server at the same time as it is a global varible that is updated.

Share this post


Link to post
Share on other sites

This is the event handler on every player:

player addEventHandler ["Killed", {Null = [_this select 0, _this select 1] execVM "playerKilled.sqf";}];

This would be the script the player that dies runs, so this would be the script that is inside your event handler:

fnc_handleKill = {
_killersCash = killer getVariable "playerCash";
_killersKills = killer getVariable "playerKills";

_newKillersKills = _killersKills + 1;
_cashForKill = 100;
_newCash = _killersCash + _cashForKill;

killer setVariable ["playerCash", _newCash, true];
killer setVariable ["playerKills", _newKillersKills, true];
};

fnc_handleFriendlyKill = {
_killersCash = killer getVariable "playerCash";	
_cashForKill = -50;
if (_killersCash <= 0) then { _cashForKill = 0; };
killer setVariable ["playerCash", _cashForKill, true];
};

_suicide = false;
_friendlyKill = false;

killedPlayer = _this select 0;
killer = _this select 1;

killerSide = side killer;
killedPlayerSide = playerSide;

if (killedPlayer == killer) exitWith {};
if (killedPlayerSide == killerSide) then {_friendlyKill = true};

if (_friendlyKill) then { [] spawn fnc_handleFriendlyKill; };

if (_suicide) then {};

if (!_friendlyKill && !_suicide) then {	[] spawn fnc_handleKill;};

Share this post


Link to post
Share on other sites

I see that is giving money but not experience, would I need to make a similar one for the p_exp gain?so the player addEventHandler ["killed".... goes in the init then the rest of the code just goes in playerKilled.sqf, is that correct? or does the playerKilled.sqf have to be run on the AI as well? Also, kevsno, I am running this in the Init for players killing eachother and putting this in the AI init

null =[this]execVM "cashOnKill.sqf" call BIS_fnc_MP;

Share this post


Link to post
Share on other sites
I see that is giving money but not experience, would I need to make a similar one for the p_exp gain?so the player addEventHandler ["killed".... goes in the init then the rest of the code just goes in playerKilled.sqf, is that correct? or does the playerKilled.sqf have to be run on the AI as well? Also, kevsno, I am running this in the Init for players killing eachother and putting this in the AI init
null =[this]execVM "cashOnKill.sqf" call BIS_fnc_MP;

All you would have to do is make more lines for the xp manipulation. copy the cash lines and make equivalents for xp

Share this post


Link to post
Share on other sites

I see, does that mean i need to change the playerCash to Cash and do I need to create a new variable in the init for playerKills = 0;?

EDIT: I made the changes I thought i would need for my variables instead but its giving me the error "undefined variables in expression: _killerscash"

fnc_handleKill = {
   _killersCash = killer getVariable "cash";
   _killersKills = killer getVariable "playerKills";

   _newKillersKills = _killersKills + 1;
   _cashForKill = 100;
   _newCash = _killersCash + _cashForKill;

   killer setVariable ["cash", _newCash, true];
   killer setVariable ["playerKills", _newKillersKills, true];
};

fnc_handleFriendlyKill = {
   _killersCash = killer getVariable "cash";    
   _cashForKill = -50;
   if (_killersCash <= 0) then { _cashForKill = 0; };
   killer setVariable ["cash", _cashForKill, true];
};

_suicide = false;
_friendlyKill = false;

killedPlayer = _this select 0;
killer = _this select 1;

killerSide = side killer;
killedPlayerSide = playerSide;

if (killedPlayer == killer) exitWith {};
if (killedPlayerSide == killerSide) then {_friendlyKill = true};

if (_friendlyKill) then { [] spawn fnc_handleFriendlyKill; };

if (_suicide) then {};

if (!_friendlyKill && !_suicide) then {    [] spawn fnc_handleKill;};  

This is what my init.sqf looks like, maybe something there is wrong, but it seems to work fine until the point that I kill the AI, then it gives me the script error.

cash = 0;
p_exp = 0;
p_level = 0;
playerKills = 0;
player addEventHandler ["Killed", {Null = [_this select 0, _this select 1] execVM "playerKilled.sqf";}];
   [] spawn SRS_fnc_init;	
//execVM "cashKillFunc.sqf";
//execVM "killed_killer.sqf";
//execVM "respawnGear.sqf";
execVM "moneyLevels.sqf";
execVM "Levels.sqf";
execVM "fatigue.sqf";	
//execVM "cashOnKill.sqf";
//execVM "Grenadestop.sqf";      
0 = [] execVM "group_manager.sqf";
0 = [] execVM "player_markers.sqf"; 
nul=[] execVM "repair.sqf";
ETG_Reinforcements = 0;

Edited by Nebulazer

Share this post


Link to post
Share on other sites

A local variable is different than a variable set onto the player, this would allow for others to see the cash you have whether it is the server or other players. Therefore making this function easier.

So this would have to be your init...

player setVariable ["cash", 0, true];
player setVariable ["p_exp", 0, true];
player setVariable ["p_level", 0, true];
player setVariable ["playerKills", 0, true];

player addEventHandler ["Killed", {Null = [_this select 0, _this select 1] execVM "playerKilled.sqf";}]; 

[] spawn SRS_fnc_init;     
//[] execVM "cashKillFunc.sqf"; 
//[] execVM "killed_killer.sqf"; 
//[] execVM "respawnGear.sqf"; 
[] execVM "moneyLevels.sqf"; 
[] execVM "Levels.sqf"; 
[] execVM "fatigue.sqf";     
//[] execVM "cashOnKill.sqf"; 
//[] execVM "Grenadestop.sqf";       
[] execVM "group_manager.sqf"; 
[] execVM "player_markers.sqf";  
[] execVM "repair.sqf"; 
ETG_Reinforcements = 0;

Share this post


Link to post
Share on other sites

There are no script errors now, but my UI in the bottom right just shows Level: any Money : $any no matter how much money i have. It does not seem to be finding the global variables that are being set in the init. Here is the whole mission on Github, it is still very much WIP obviously and there are some old files sitting in the root that are not being used any more, i have to delete some of them, but maybe you will see what is causing me problems in the UI https://github.com/Nebulazer/CWCKunduz

EDIT: I think i figured it out, it is just going to take a lot of rewriting scripts, thank you for the help. This is my moneyLevels.sqf now to display the initial UI for the money

//cash and level display on GUI 
_cash = player getVariable "cash";
_cash=_cash+500;

("DollarTitle_layer" call BIS_fnc_rscLayer) cutRsc ["DollarTitle","PLAIN"];
("LevelTitle_layer" call BIS_fnc_rscLayer) cutRsc ["LevelTitle","PLAIN"];
(uiNameSpace getVariable "myUI_DollarTitle") ctrlSetText format ["Money: $%1",_cash];
//(uiNameSpace getVariable "myUI_LevelTitle") ctrlSetText format ["Level: %1", (p_level) call calcLevel];

EDIT2: its still not quite working, whenever i get a kill with your script, it puts my money back to zero instead of the initial 500. I am going to take away the initial 500 once it saves with iniDB, but it still should not set me to zero.

I should also note that I am testing this on AI using the line you provided above for the killed event handler.

Edited by Nebulazer

Share this post


Link to post
Share on other sites
There are no script errors now, but my UI in the bottom right just shows Level: any Money : $any no matter how much money i have. It does not seem to be finding the global variables that are being set in the init. Here is the whole mission on Github, it is still very much WIP obviously and there are some old files sitting in the root that are not being used any more, i have to delete some of them, but maybe you will see what is causing me problems in the UI https://github.com/Nebulazer/CWCKunduz

EDIT: I think i figured it out, it is just going to take a lot of rewriting scripts, thank you for the help. This is my moneyLevels.sqf now to display the initial UI for the money

//cash and level display on GUI 
_cash = player getVariable "cash";
_cash=_cash+500;

("DollarTitle_layer" call BIS_fnc_rscLayer) cutRsc ["DollarTitle","PLAIN"];
("LevelTitle_layer" call BIS_fnc_rscLayer) cutRsc ["LevelTitle","PLAIN"];
(uiNameSpace getVariable "myUI_DollarTitle") ctrlSetText format ["Money: $%1",_cash];
//(uiNameSpace getVariable "myUI_LevelTitle") ctrlSetText format ["Level: %1", (p_level) call calcLevel];

EDIT2: its still not quite working, whenever i get a kill with your script, it puts my money back to zero instead of the initial 500. I am going to take away the initial 500 once it saves with iniDB, but it still should not set me to zero.

I should also note that I am testing this on AI using the line you provided above for the killed event handler.

Go through your code and put diag_log at each step. To check your variables are being set properly you can do this:

diag_log format ["Cash: %1", _cash];

That should help you figure out what is really going on when the code is running

Share this post


Link to post
Share on other sites

I can see if the variables change or not on my UI as its already set to do that, it shows me when i run moneylevels.sqf that i have 500 dollars but if i run it again in Debug it keeps me at 500 dollars, I think that is the problem right there. Is there something I am not understanding about local variables and setvariable?

_cash = player getVariable "playerCash";
_cash=_cash+500;

("DollarTitle_layer" call BIS_fnc_rscLayer) cutRsc ["DollarTitle","PLAIN"];
("LevelTitle_layer" call BIS_fnc_rscLayer) cutRsc ["LevelTitle","PLAIN"];
(uiNameSpace getVariable "myUI_DollarTitle") ctrlSetText format ["Money: $%1",_cash];

that keeps at 500 dollars no matter how many times I run it, but why if _cash=_cash+500; every time you run it? I think if that can be explained to me, it might solve my problem.

Edit: After messing around a bit with it i found the answer. It needs a new local variable to replace the old amount as so

_cash = player getVariable "playerCash";

_newCash=_cash+500;
player setVariable ["playerCash", _newCash, true];

I have it so the variable is changing now when i kill AI but it is still not doing it right. The AI are independent but set to be hostile to both red and blue but when i Kill an AI it gives me -50 dollars, if I left the script the way that you gave it to me it would make you have -50 dollars at the end instead of losing -50, but really I want it so you gain +100 when you kill an independent AI and 200 when you kill a player on the opposite team.

fnc_handleKill = {
   _killersCash = killer getVariable "playerCash";
   _killersKills = killer getVariable "playerKills";

   _newKillersKills = _killersKills + 1;
   _cashForKill = 100;
   _newCash = _killersCash + _cashForKill;

   killer setVariable ["playerCash", _newCash, true];
   killer setVariable ["playerKills", _newKillersKills, true];
(uiNameSpace getVariable "myUI_DollarTitle") ctrlSetText format ["Money: $%1",_newCash];
};

fnc_handleFriendlyKill = {
   _killersCash = killer getVariable "playerCash";    
   _cashForKill = -50;
_newCash = _killersCash + _cashForKill;
   if (_killersCash <= 0) then { _cashForKill = 0; };
   killer setVariable ["playerCash", _newCash, true];
(uiNameSpace getVariable "myUI_DollarTitle") ctrlSetText format ["Money: $%1",_newCash];
};

_suicide = false;
_friendlyKill = false;

killedPlayer = _this select 0;
killer = _this select 1;

killerSide = side killer;
killedPlayerSide = playerSide;

if (killedPlayer == killer) exitWith {};
if (killedPlayerSide == killerSide) then {_friendlyKill = true};

if (_friendlyKill) then { [] spawn fnc_handleFriendlyKill; };

if (_suicide) then {};

if (!_friendlyKill && !_suicide) then {    [] spawn fnc_handleKill;};  

I tried running a different script on the AI with the killed event handler and changing it to AI_killed.sqf but it still gives me the same results even though the part about -50 is commented out, so that means it is still using the eventhandler of the player instead of the one on the AI?

fnc_handleKill = {
   _killersCash = killer getVariable "playerCash";
   _killersKills = killer getVariable "playerKills";

   _newKillersKills = _killersKills + 1;
   _cashForKill = 100;
   _newCash = _killersCash + _cashForKill;

   killer setVariable ["playerCash", _newCash, true];
   killer setVariable ["playerKills", _newKillersKills, true];
(uiNameSpace getVariable "myUI_DollarTitle") ctrlSetText format ["Money: $%1",_newCash];
};

//fnc_handleFriendlyKill = {
//   _killersCash = killer getVariable "playerCash";    
//   _cashForKill = -50;
//	_newCash = _killersCash + _cashForKill;
 //  if (_killersCash <= 0) then { _cashForKill = 0; };
 //  killer setVariable ["playerCash", _newCash, true];
//	(uiNameSpace getVariable "myUI_DollarTitle") ctrlSetText format ["Money: $%1",_newCash];
//};

_suicide = false;
_friendlyKill = false;

killedPlayer = _this select 0;
killer = _this select 1;

killerSide = side killer;
killedPlayerSide = playerSide;

if (killedPlayer == killer) exitWith {};
if (killedPlayerSide == killerSide) then {_friendlyKill = true};

if (_friendlyKill) then { [] spawn fnc_handleFriendlyKill; };

if (_suicide) then {};

if (!_friendlyKill && !_suicide) then {    [] spawn fnc_handleKill;};  

Edited by Nebulazer

Share this post


Link to post
Share on other sites

the person that dies runs the event handler, so when scripting for this event, you need to keep in mind who you are referring to.

Share this post


Link to post
Share on other sites

I think that this part is thinking that the independant side AI that are spawned are friendly and it is running fnc_handleFriendlyKill. does ! && ! mean if neither?

_suicide = false;
_friendlyKill = false;

killedPlayer = _this select 0;
killer = _this select 1;

killerSide = side killer;
killedPlayerSide = playerSide;

if (killedPlayer == killer) exitWith {};
if (killedPlayerSide == killerSide) then {_friendlyKill = true};

if (_friendlyKill) then { [] spawn fnc_handleFriendlyKill; };

if (_suicide) then {};

if (!_friendlyKill && !_suicide) then {    [] spawn fnc_handleKill;}; 

Share this post


Link to post
Share on other sites
does ! && ! mean if neither?

Correct.

Share this post


Link to post
Share on other sites

I got it to work on the AI, now i need to see if it works for players who join my game, and I need to make sure the player event handler works as well.

this addEventHandler ["Killed", {Null = [_this select 0, _this select 1] execVM "AI_Killed.sqf";}]; 

AI_Killed.sqf

fnc_handleKill_AI = {  
   _killersCash = killer getVariable "cash";  
   _xp = killer getVariable "p_exp";  

   _newXP = _xp + 100;  
   _cashForKill = 100;  
   _newCash = _killersCash + _cashForKill;  

   killer setVariable ["cash", _newCash, true];  
   killer setVariable ["p_exp", _newXP, true];  
   (uiNameSpace getVariable "myUI_DollarTitle") ctrlSetText format ["Money: $%1",_newCash];
execVM "Levels.sqf";
hint "+$100 and +100XP";	
};  




killedPlayer = _this select 0;  
killer = _this select 1;  

killerSide = side killer;  
killedPlayerSide = playerSide;  
[] spawn fnc_handleKill_AI;

EDIT: If i wanted to spawn these AI with Spunfin's AI script, how would I have to set this up in the custom init? It says I cant use any conditions like the _this select 0, _this select 1. So I thought it would work like this but it does not.

nul = [this, 2, true, 2, 50, 1, 0.35, nil, "this addEventHandler ['Killed', {execVM 'AI_Killed.sqf';}];", 1] execVM "LV\fillHouse.sqf";

Edited by Nebulazer

Share this post


Link to post
Share on other sites
I got it to work on the AI, now i need to see if it works for players who join my game, and I need to make sure the player event handler works as well.

this addEventHandler ["Killed", {Null = [_this select 0, _this select 1] execVM "AI_Killed.sqf";}]; 

Just add the event handler inside the initPlayerLocal.sqf file if you didn't do that already. Every player will have it.

Share this post


Link to post
Share on other sites

I tested this with another player joining my server, and when he kills the AI it still gives me(host) the money and XP even though its with set/getvariable. I thought doing all of this was to make each individual player have their own money gain. Do I need to run a dedicated server for this to work? Is there no way to have a money system when you just host on your computer?

Share this post


Link to post
Share on other sites

It should work regardless. Must be an issue within your code somewhere. Probably step through it all as if you're manually running it yourself. Make sure it makes sense to you, add logging comments as well. Get the RPT logs from the clients and make sure they are running the right things (with diag_log comments).

Share this post


Link to post
Share on other sites

I think this is what I am missing. I looked into initPlayerLocal.sqf, and it looks like that runs on the clients only, so is that where I am going to set the variables? and also should I take the set variables out of the normal Init?

Edited by Nebulazer

Share this post


Link to post
Share on other sites

Here is a little something to pick apart that may help.. MISSION if not maybe relieve some tension for 5 mins :D

  • Like 1

Share this post


Link to post
Share on other sites

thank you for this, I will study this and learn from it.

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  

×