Jump to content
BobTheHunted

Trouble with creation civilian panic script - Simple issue?

Recommended Posts

Hello.  First off, I want to apologize if I have posted this topic in the wrong area, but I think it's right.  If not, oh well.  Oops.

 

Anyway, on to my script.  What I am trying to do is make a script for the civilians in my mission to make them behave in a certain way by playing some of the cowering type animations in A3.

I managed to get the basic idea working by spawning the commands manually into each units initialization field but when I try to define a function and call that I get issues. 

 

inside the civ units init:

 

_null = [this] spawn civPanic;

 

init.sqf:

 

[] execVM "functions.sqf";

 

functions.sqf;

 

 

civPanic = {

    private ["_CivUnit"];
    _CivUnit = _this select 0;

    _canim1 = { _CivUnit playMove "ApanPknlMstpSnonWnonDnon_G01"; };
    _canim2 = { _CivUnit playMove "ApanPknlMstpSnonWnonDnon_G02"; };
    _canim3 = { _CivUnit playMove "ApanPknlMstpSnonWnonDnon_G03"; };
    _canim4 = { _CivUnit setDamage 1; };

    _CivAnim = selectRandom [_canim1, _canim2, _canim3, _canim4];

    waitUntil {triggerActivated FirstContactTrigger};

    waitUntil {behaviour _CivUnit == "COMBAT"};
    sleep 1;
    if (behaviour _CivUnit == "combat") then {nul = [_CivUnit] call _CivAnim; _CivUnit disableAI "ANIM"; sleep 5; _CivUnit enableAI "ANIM";};
    sleep 1;
    
};

 

The problem I am having is I get the error message "Error: Undefined Variable in Expression: this" and the line of code it shows  above that is "0 = [] spawn { |#|this playMove "ApanPknlMstpSnonWnonDnon_G01"; };" or something along those lines.

 

I feel like the problem isn't really a problem at all, I'm just missing something obvious or I'm still just completely unfamiliar with scripting in general.  I have tried several things but I just can't seem to get it working.  Anyone know what is wrong?  Thanks in advance for the help (if anyone actually responds lol).

Share this post


Link to post
Share on other sites

inside the civ units init:

null = [this] spawn civPanic;

init.sqf:

[] call compile preprocessFileLineNumbers "functions.sqf";

functions.sqf;


civPanic = {

	params ["_CivUnit"];
        private ["_canim1", "_canim2", "_canim3", "_canim4", "_CivAnim"];


    _canim1 = { params ["_unit"]; _unit playMove "ApanPknlMstpSnonWnonDnon_G01"; };
    _canim2 = { params ["_unit"]; _unit playMove "ApanPknlMstpSnonWnonDnon_G02"; };
    _canim3 = { params ["_unit"]; _unit playMove "ApanPknlMstpSnonWnonDnon_G03"; };
    _canim4 = { params ["_unit"]; _unit setDamage 1; };

    _CivAnim = selectRandom [_canim1, _canim2, _canim3, _canim4];

    waitUntil {sleep 1; (triggerActivated FirstContactTrigger && (behaviour _CivUnit) == "COMBAT") || !(alive _CivUnit)};
	
	if !(alive _CivUnit) exitWith {};

		null = [_CivUnit] call _CivAnim;
		_CivUnit disableAI "ANIM";
		sleep 5;
		_CivUnit enableAI "ANIM";
  
};

Share this post


Link to post
Share on other sites

 

inside the civ units init:

null = [this] spawn civPanic;

init.sqf:

[] call compile preprocessFileLineNumbers "functions.sqf";

functions.sqf;


civPanic = {

	params ["_CivUnit"];
        private ["_canim1", "_canim2", "_canim3", "_canim4", "_CivAnim"];


    _canim1 = { params ["_unit"]; _unit playMove "ApanPknlMstpSnonWnonDnon_G01"; };
    _canim2 = { params ["_unit"]; _unit playMove "ApanPknlMstpSnonWnonDnon_G02"; };
    _canim3 = { params ["_unit"]; _unit playMove "ApanPknlMstpSnonWnonDnon_G03"; };
    _canim4 = { params ["_unit"]; _unit setDamage 1; };

    _CivAnim = selectRandom [_canim1, _canim2, _canim3, _canim4];

    waitUntil {sleep 1; (triggerActivated FirstContactTrigger && (behaviour _CivUnit) == "COMBAT") || !(alive _CivUnit)};
	
	if !(alive _CivUnit) exitWith {};

		null = [_CivUnit] call _CivAnim;
		_CivUnit disableAI "ANIM";
		sleep 5;
		_CivUnit enableAI "ANIM";
  
};

That was a fast reply.  This setup is certainly much more elegant and polished than my attempt, but I still get the same undefined variable error for some reason.  I'll  post exactly what it says:

 

'0 = [] spawn {canim1 = |#|this playMove "ApanPknlMstpSnonWnonDnon_...'

Error Undefined Variable in expression: this

Share this post


Link to post
Share on other sites

there are no "this" in example above, i do not knew if that will work with (selectrandom call {}) but there are  even better one:

civPanic = {

	params ["_CivUnit"];

    waitUntil {sleep 1; (triggerActivated FirstContactTrigger && (behaviour _CivUnit) == "COMBAT") || !(alive _CivUnit)};
	
	if !(alive _CivUnit) exitWith {};

		 if ((random 1) < 0.31) then { //31% chance that unit dies need to be adjusted
		 
			_CivUnit setDamage 1; 
		 
		 } else {
		
			_CivUnit playMove (selectRandom ["ApanPknlMstpSnonWnonDnon_G01", "ApanPknlMstpSnonWnonDnon_G02", "ApanPknlMstpSnonWnonDnon_G03"]);
			_CivUnit disableAI "ANIM";
			sleep 5;
			_CivUnit enableAI "ANIM";	 
		 
		 };

};

besides that if i am understanding what you trying to achieve i think the better solution will be using eventhandler  FiredNear

instead of some trigger

Share this post


Link to post
Share on other sites

there are no "this" in example above, i do not knew if that will work with (selectrandom call {}) but there are  even better one:

civPanic = {

	params ["_CivUnit"];

    waitUntil {sleep 1; (triggerActivated FirstContactTrigger && (behaviour _CivUnit) == "COMBAT") || !(alive _CivUnit)};
	
	if !(alive _CivUnit) exitWith {};

		 if ((random 1) < 0.31) then { //31% chance that unit dies need to be adjusted
		 
			_CivUnit setDamage 1; 
		 
		 } else {
		
			_CivUnit playMove (selectRandom ["ApanPknlMstpSnonWnonDnon_G01", "ApanPknlMstpSnonWnonDnon_G02", "ApanPknlMstpSnonWnonDnon_G03"]);
			_CivUnit disableAI "ANIM";
			sleep 5;
			_CivUnit enableAI "ANIM";	 
		 
		 };

};

besides that if i am understanding what you trying to achieve i think the better solution will be using eventhandler  FiredNear

instead of some trigger

I was unaware of the eventhandler.  I'll look into it.  As for this function, now there are no errors, but the civilians just behave as they normally would.  They also seem to go into combat mode whenever there is an OPFOR soldier nearby, whether shots are being fired or not.  I'm lost as to why this wouldn't work.  There must be a better way to play their animations?  I don't know

Share this post


Link to post
Share on other sites
_CivUnit disableAI "ANIM";
sleep 5;
_CivUnit enableAI "ANIM";	

for what you need this part of

Share this post


Link to post
Share on other sites
_CivUnit disableAI "ANIM";
sleep 5;
_CivUnit enableAI "ANIM";	

for what you need this part of

 

I shortened the sleep time for testing purposes.  But I use the disableAI so the civs stay where they are without exiting their animation.  I would enableAI after a few minutes so they can move again

Share this post


Link to post
Share on other sites

I think that _CivUnit needs to actually be defined somehow

 

_CivUnit = _this select 0;

This doesn't seem to work.  I'm at a loss here

Share this post


Link to post
Share on other sites

I do not knew what you are talking about there are everything defined.

Anyway lets try eventhandler put that in civilian unit init field:

this addEventHandler ["FiredNear",{

	params ["_unit"];
	_this select 2 params ["_distance"];
	
	if !(alive _unit) exitWith {};
	
	if (_distance < 10) then {
	
		if ((random 1) < 0.05) then {   
		 
			_unit setDamage 1; 
		 
		};
	};
		
	if (alive _unit) then {
		
		_unit playMove (selectRandom ["ApanPknlMstpSnonWnonDnon_G01", "ApanPknlMstpSnonWnonDnon_G02", "ApanPknlMstpSnonWnonDnon_G03"]);	 

	};
}];

If somebody fired weapon near civ and distance between are (<10m) there are 5% chance that the unit die from shock

If still alive unit plays random given animation

If even, they all looks for me as the same

Share this post


Link to post
Share on other sites

I do not knew what you are talking about there are everything defined.

Anyway lets try eventhandler put that in civilian unit init field:

this addEventHandler ["FiredNear",{

	params ["_unit"];
	_this select 2 params ["_distance"];
	
	if !(alive _unit) exitWith {};
	
	if (_distance < 10) then {
	
		if ((random 1) < 0.05) then {   
		 
			_unit setDamage 1; 
		 
		};
	};
		
	if (alive _unit) then {
		
		_unit playMove (selectRandom ["ApanPknlMstpSnonWnonDnon_G01", "ApanPknlMstpSnonWnonDnon_G02", "ApanPknlMstpSnonWnonDnon_G03"]);	 

	};
}];

If somebody fired weapon near civ and distance between are (<10m) there are 5% chance that the unit die from shock

If still alive unit plays random given animation

If even, they all looks for me as the same

I don't have much time to test this right now, but I will soon.  Thanks for the help

Share this post


Link to post
Share on other sites

That script does work, but I have managed to get the script to work in the individual unit init field.  I am trying to find a way to simplify this so it execs on all civs or on one function or something.  But using the event handler is definitely much better.  Also the setdamage command was initially just for testing purposes, and isn't really necessary and obviously not very realistic

Share this post


Link to post
Share on other sites

init.sqf

{

	_x addEventHandler ["FiredNear",{

		params ["_unit"];	
			
		if (alive _unit) then {
			
			_unit playMove (selectRandom ["ApanPknlMstpSnonWnonDnon_G01", "ApanPknlMstpSnonWnonDnon_G02", "ApanPknlMstpSnonWnonDnon_G03"]);	 

		};
	}];
} forEach (allUnits select {(side _x) == CIVILIAN});
  • Like 1

Share this post


Link to post
Share on other sites

 

init.sqf

{

	_x addEventHandler ["FiredNear",{

		params ["_unit"];	
			
		if (alive _unit) then {
			
			_unit playMove (selectRandom ["ApanPknlMstpSnonWnonDnon_G01", "ApanPknlMstpSnonWnonDnon_G02", "ApanPknlMstpSnonWnonDnon_G03"]);	 

		};
	}];
} forEach (allUnits select {(side _x) == CIVILIAN});

That worked! Thank you for your help.  I have added the remaining 3 animations that I didn't use before to the list and now everything is working like a charm

Share this post


Link to post
Share on other sites

That worked! Thank you for your help.  I have added the remaining 3 animations that I didn't use before to the list and now everything is working like a charm

What are the other 3 animations?

Share this post


Link to post
Share on other sites

What are the other 3 animations?

These are all the animations I am using: ["ApanPknlMstpSnonWnonDnon_G01", "ApanPknlMstpSnonWnonDnon_G02", "ApanPknlMstpSnonWnonDnon_G03", "ApanPpneMstpSnonWnonDnon_G01", "ApanPpneMstpSnonWnonDnon_G02", "ApanPpneMstpSnonWnonDnon_G03"]

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

×