Jump to content
Sign in to follow this  
Lucky44

Coding issue around variables in script to let guards call for help

Recommended Posts

I'm trying to create a script that works with an EventHandler. The goal is to simulate AI guards calling for help (by radio, presumably) if they spot players and have time to make the radio call before being killed. (If you know a better way to do this, please say so!)

What I'm doing is this: I have a "fired" eventHandler on each AI guard. So if they fire their weapon, it runs the script below:

if (!isServer) exitwith {}; // make sure only the server runs the script
if (alarmSounded == "true") exitwith {}; // just quit script if alarm has already sounded
if (shotFlag == "true") exitwith {}; // this is initialized in the init.sqf

shotFlag = "true"; //the problem with this ATM is that if one person shoots, the script won't work again for 10-15 seconds, for ANYONE...
publicVariable "shotFlag";

sleep (random 5)+10; // if unit is still alive 10-15 seconds after shooting, it will call on radio for help
_shooter = _this select 0;


if (alive _shooter) then {

[[west,"HQ"],nil,rsideChat, "You've been spotted. A hostile guard put out a radio call for help!"] call RE;
//hint format ["%1 is calling for help on the radio!",_shooter]; // another approach

alarmSounded = "true"; // this will disable further messages and calls for reinforcements
publicVariable "alarmSounded"; 

sleep 260+(random 120); // put a delay before the reinforcements start out
execVM "scripts\spawnRFland.sqf"; //spawn some reinforcements on land
execVM "scripts\spawnRFair.sqf"; //spawn some reinforcements in air
}
else
{
shotFlag = "FALSE"; //reset the variable
};

The problem I foresee is if one guard shoots, no other guards will be able to shoot and radio for help until the 10-15 seconds is up for the first guard. I think I should create a variable tied to each AI unit that is like the shotFlag above. But I can't remember what that kind of variable is called to even look it up!

Any suggestions will be very welcome!

Edited by Lucky44
Fixed the code a bit

Share this post


Link to post
Share on other sites

Hi, rather than using the shotflag variable would't a simple waituntil work.

_temp=time+(random 5)+10;waituntil{time > _temp};

Share this post


Link to post
Share on other sites

I think what you are asking is to use setvariable, to assign a shootflag to the shooter

eg _shooter setvariable ["ShotFlag","true];

Share this post


Link to post
Share on other sites

And ignore what I said it won't work, I just realised the script will run with each bullet.

Share this post


Link to post
Share on other sites

Thanks to both of you for the help.

Yes! setVariable was what I was thinking of. I had it in my head that it was called a "personal variable" or something...duh.

Here's what I'm going to use for now:


if (!isServer) exitwith {}; // make sure only the server runs the script
if (alarmSounded == "true") exitwith {}; // just quit script if alarm has already sounded


//if (shotFlag == "true") exitwith {}; // this is initialized in the init.sqf
//shotFlag = "true"; //the problem with this ATM is that if one person shoots, the script won't work again for 10-15 seconds, for ANYONE...
//publicVariable "shotFlag";

_shooter = _this select 0;
if ((_shooter getVariable "alarm") == "yes") exitWith {}; // if this is not the first shot, exit
_shooter setVariable ["alarm", "yes", true]; // if this is first shot, set alarm call to YES, and make it TRUE for publicVariable

sleep ((random 5)+15); // if unit is still alive 15-20 seconds after shooting, it will call on radio for help

//If, after the delay (to give the shooter time to get on his radio), shooter's still alive, do this:
if (alive _shooter) then {
alarmSounded = "true"; // this will disable further messages and calls for reinforcements (from other units)
publicVariable "alarmSounded"; 

[[west,"HQ"],nil,rsideChat, "You've been spotted. A hostile guard put out a radio call for help!"] call RE;
//hint format ["%1 is calling for help on the radio!",_shooter]; // another approach

sleep 260+(random 120); // put a delay before the reinforcements start out
execVM "scripts\spawnRFland.sqf"; //spawn some reinforcements on land
execVM "scripts\spawnRFair.sqf"; //spawn some reinforcements in air
};

Share this post


Link to post
Share on other sites

I'm confused, isn't this going to run just once?

There's nothing resetting alarmSounded "true" to "false" unless your doing it via one of the other scripts.

I also see the same for _shooter setVariable ["alarm", "yes", true]; there's no resetting it.

Share this post


Link to post
Share on other sites

Yes, it's a sloppy coder thing to do. But the reasoning is that this is a one-time thing. This call for help should only go out once (so that help, in the form of reinforcements only comes once). So once the variables you mention are set, there's no good reason to reset them. Does that make sense?

Share this post


Link to post
Share on other sites

I see, you may want to look at removing the eventhandlers once they're not needed as there may be slowdown if a lot are being used to check every bullet.

You may get away with

if (alarmSounded == "true") exitwith {(_this select 0) removeeventHandler ["fired",0]};

This will remove the eventhandler from each unit if they try to fire their gun once alarmSounded == "true"

Share this post


Link to post
Share on other sites

That sounds smart, Sel, thanks. I intend this to be a stealth mission (briefing: HERE), but if the players get sloppy, it'll get busy. So I think I should actually run a loop for all opFor and remove all EH after the alarms have been sounded. There could be a lot of shooting!

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  

×