Jump to content
Sign in to follow this  
Sexacutioner

Sometimes my MPKilled event is not being called

Recommended Posts

So in a multiplayer game I'm creating around 50 rabbits on the map you can hunt for money. I was finding that sometimes the addaction that is supposed to be attached to the rabbit body on death isn't working.

I tested with my friend with around 400 rabbits and put in messages to tell me when the rabbit was killed, who killed it, etc. What I found is that some of the time the "MPKilled" is not getting called at all. Does anyone see any problem with my spawn code?

This is in the init, to create the initial rabbits, the minRandomLandX etc are the extents of where I want them to spawn:

if (isServer) then {
for [{_counter=0}, {_counter<_totalRabbits}, {_counter=_counter+1}] do {
	_randomX = (random minRandomLandX)+addRandomLandX;
	_randomY = (random minRandomLandY)+addRandomLandY;
	_locationWrong=true;
	while {_locationWrong} do {
		if (!surfaceIsWater [_randomX, _randomY]) then {
			if ({[_randomX, _randomY] distance _x < 300} count playableUnits==0) then {
				_locationWrong=false;
			} else {
				_randomX = (random minRandomLandX)+addRandomLandX;
				_randomY = (random minRandomLandY)+addRandomLandY;
			};
		} else {
			_randomX = (random minRandomLandX)+addRandomLandX;
			_randomY = (random minRandomLandY)+addRandomLandY;
		};
	};
	_currentRabbit = createAgent  ["Rabbit",[_randomX,_randomY], [],0,'FORM'];
	_currentRabbit addMPEventHandler ["MPKilled", {[_this,null,null,[4]] execVM "callScript.sqf"}];
};	
};

And here is what I have in callScript.sqf. This script handles multiple things so I'm just pasting the stuff dealing with rabbit death:

_scriptType=_this select 3 select 0;
_doCreateRabbit=false;
switch (_scriptType) do {
case 4: {
	_victim = _this select 0 select 0;
	_killer = _this select 0 select 1;
	_killerSide = side _killer;
	_victimSide = side _victim;
	player globalchat format["dead rabbit: %1 %2 %3 %4",_killer,_victim,_killerSide,_victimSide]; //Added for debug
	_victim addAction [format ["Get Money ($%1)",moneyDroppedRabbit], "callScript.sqf",[1]];	
	if (_killer==player) then {
		attachLogic2 globalchat "Rabbit Killed";
	};
	if (isServer) then {
		if (typeOf _killer=="Rabbit") then {	
			while { _victim distance player<300} do {
				sleep 10;
			};
			player globalchat "removed event handlers"; //Added for debug
			_victim removeAction 0;
			_victim removeAllMPEventHandlers "MPKilled"; 
			deleteVehicle _victim;
			_doCreateRabbit=true;
		};
	};
};
};
if (_doCreateRabbit) then {
_randomX = (random minRandomLandX)+addRandomLandX;
_randomY = (random minRandomLandY)+addRandomLandY;
_locationWrong=true;
while {_locationWrong} do {
	if (!surfaceIsWater [_randomX, _randomY]) then {
		if ({[_randomX, _randomY] distance _x < 300} count playableUnits==0) then {
			_locationWrong=false;
		} else {
			_randomX = (random minRandomLandX)+addRandomLandX;
			_randomY = (random minRandomLandY)+addRandomLandY;
		};
	} else {
		_randomX = (random minRandomLandX)+addRandomLandX;
		_randomY = (random minRandomLandY)+addRandomLandY;
	};
};
player globalchat "created new rabbit"; //Added for debug
_currentRabbit = createAgent  ["Rabbit",[_randomX,_randomY], [],0,'FORM'];
_currentRabbit addMPEventHandler ["MPKilled", {[_this,null,null,[4]] execVM "callScript.sqf"}];
_currentRabbit addAction ["New Spawn", "callScript.sqf",[10]];
};

The problem I'm running into is sometimes a rabbit is killed and there is no message sent and no addAction attached to the rabbit. So it seems sometimes the "MPKilled" event is not happening at all. I've been through the code multiple times but can't seem to find anything wrong with it. Anyone else have an idea what I could be doing wrong? Thanks for any and all help on this!

Share this post


Link to post
Share on other sites

enable -showScriptErrors & cross check .rpt

just a suggestion but your debug should be set to throw messages on the action that takes place, and also on action that has not taken place yet

but just before. to help locate the broken part of code.

something like

while _locationWrong == true do {hint "Debug: location set to: wrong"} else {hint "Debug: rabbits spawned?"};

something like that.

Share this post


Link to post
Share on other sites

I can't find anything relevant in .rpt and have ui_cc -showScriptErrors with no errors.

I'm not sure I have to get feedback for the creation loop because it's for sure the rabbit is created as we can see them. It's just that some don't trigger the MPKilled after they are shot. But they are for sure getting created...

Share this post


Link to post
Share on other sites
I can't find anything relevant in .rpt and have ui_cc -showScriptErrors with no errors.

I'm not sure I have to get feedback for the creation loop because it's for sure the rabbit is created as we can see them. It's just that some don't trigger the MPKilled after they are shot. But they are for sure getting created...

what I meant in my pathetic example was merely an example at a pathetic attempt to explain ..

after the action that you want to take place "MPKilled" before and after in your syntax

I didn't examine your code entirely I pulled the part of spawning rabbits to explain the example.

Share this post


Link to post
Share on other sites

So just an update on what I've tried and it's still not working.

Put in a very long sleep before creating the initial rabbits, some were still duds (did not call the MPKilled event when killed)

I tried spawning the rabbits all right on top of the players in a multiplayer game to make testing quicker. For whatever reason when surrounded by 100 rabbits instead of them spread out across the map this worked every time. Restarted the mission like 5 times and did not have one dud rabbit after tons of shooting. After thinking it was fixed I went and spawned the rabbits spread across the map far away from the players again and back to having random duds.

I made the ressurected rabbits cows to tell them apart and see if only that part of the script was missing. It wasn't as some rabbits were still duds. I actually think the initial created ones are the only duds. Wondering if there is some sync issue while the map is first starting and the clients are loading it while the server makes rabbits. But I tried with a long sleep and it still messes up occasionally.

I'm out of ideas right now, wondering if it is something weird like an automatic resource culling if rabbit is out of range of player I dunno.

Share this post


Link to post
Share on other sites

Sooo. After lots of testing against rabbits, cows, 1000's of animals on the map and so forth I found what the problem was. There are already rabbits on the map! I was way off :).

That's why it seemed the error was so random. Sometimes we were killing rabbits I hadn't created. So now I'm looking for a way to delete them. I found the "agents" command and can find all the agents on the map like so:

agentCounter=0;
{
agentCounter=agentCounter+1;
player globalChat "agent "+str(agent _x)+" "+str(agentCounter);

} forEach agents;

The problem now is I don't know what is rabbits and what is not. The typeOf agent _x returns an empty string. The getPos returns [0,0,0]. Is there any way of telling if this agent is a rabbit? I don't wish to remove everything from the map.

---------- Post added at 00:20 ---------- Previous post was at 23:54 ----------

Another problem I've found is I cannot find a way to delete these guys. Even if I can verify if it is a rabbit, deletevehicle or setdamage do not seem to change the agents array at all.

Share this post


Link to post
Share on other sites

does setPos work?

if an addon is an option, you could disable it by delete Rabbit; in class Ambient(_A3)

Share this post


Link to post
Share on other sites

setPos on the agents I get back from this array does nothing. I haven't been able to extract any information from them besides the fact they seem to be of type "object". I'm not even sure this array contains the rabbits, but it is my best guess. Oh and also it changes size as the game runs.

I'll have to look into that class. I haven't made much for addons since OFP but from what I remember you can include them in missions yes?

Share this post


Link to post
Share on other sites

no you cant :(

you could check the dayz mod code - it operates also with agents.. although not auto generate ambient ones

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  

×