Jump to content
Sign in to follow this  
sinny

Spawn units around player when shots are fired

Recommended Posts

Hi, im new to scripting but love to learn...

 

I have had alot of help from this forum in the past and i hope someone can show me or point me in the right direction.

 

I would like to make a script that spawns a random no of units (between 1-9) around the player when shots are fired. I dont want the units to spawn after every shot, but randomly between 1-100 shots. I want this function to be activated every five minutes.

 

I hope its not to confusing.

 

In short, i would like to create the feeling that the units is lurking around the player and "hears" the shots and going in for an attack. Maybe someone have an better idea than my "rules" above for this to create the illusion that the player wakes up units that going in for an attack.

 

See pic:

 

b191214.jpg

Share this post


Link to post
Share on other sites

Well for sure you need a preprocessed function here , but say first if that is MP or SP environment.

Share this post


Link to post
Share on other sites

Advising using the Eventhandler Fired. If its a multiplayer mission have this run on the server, or even better a headlessclient if that's possible. It will only run when the player fired his gun.

After this you can do a random check if it will spawn yes or no. For the spawn like David said use a function.

Share this post


Link to post
Share on other sites

Thanks guys!

I would really like to have it functional in MP.

So a "function"... Can you please explain more?

I want to leave the "headless client" out from this... if possible...

Share this post


Link to post
Share on other sites

Well a function is just a script that you can call or spawn. If you create a function out of this with cfgFunctions it wont need to read the whole script every time. It will just load it up on mission start and keep it in memory.

Means if you call/spawn your function it will run a lot quicker.

 

So for your function I suggest to make a spawnZombie function. Which takes the position of the player and then spawn a random # on a random or fixed range from the player. Also in that function check if it can spawn. With like you said so its random.

Share this post


Link to post
Share on other sites

Wow, yes yes! Sounds awesome and not that hard... 

But i have no idea where to start... Do you know any similar function i can have a look at?

Or can you help me write a foundation to the function?

Share this post


Link to post
Share on other sites

Well to learn to code you need to code, and tbh this is an easy starting project.

I am happy to give you the help to start this.

Suggest you begin with the eventhandler and start working from there. So create a initPlayerServer.sqf and add the eventhandler there to the player, try to find the commands to do what you want in your function.

And when you think you've tried everything and it might work or not post it here and ill have a look at it ;).

Share this post


Link to post
Share on other sites

Super!

Many thanks! 

 

I´ll post my progress here :)

 

Edit.

I found this in the function viewer.

In my rookie-brain, i could modify this piece of code...

Or use it as a start... 

/*
	Author: Karel Moricky

	Description:
	Spawns enemies around players

	Parameter(s):
	_this select 0: OBJECT - player
	_this select 1: OBJECT - refence object (enemies will be spawned on opposite direction)
	_this select 2: SIDE - side of enemies
	_this select 3 (Optional): ARRAY - list of enemy classes
	_this select 4 (Optional): NUMBER - delay between spawning
	_this select 5 (Optional): CODE - code executed on every soldier. Unit is passed as _this

	Returns:
	ARRAY - list of all spawned crows
*/

_player = _this select 0;
_target = _this select 1;
_side = _this select 2;
_classList = _this select 3;
_maxCount = if (count _this > 4) then {_this select 4} else {10};
_delay = if (count _this > 5) then {_this select 5} else {30};
_code = if (count _this > 6) then {_this select 6} else {{}};

if (typename _target == typename objnull) then {_target = [_target,_target];};

_otherPlayer = (_players - [_player]) select 0;
_grp = creategroup _side;
_wp = _grp addwaypoint [position _player,10];
_wp setwaypointstatements ["false",""];
_wp setwaypointtype "guard";
_wp waypointattachvehicle _player;
_player setvariable ["BIS_spawnedEnemies",[]];

while {true} do {
	waituntil {sleep 1; isplayer _player};
	waituntil {sleep 1; {alive _x} count (_player getvariable "BIS_spawnedEnemies") < _maxCount};

	_class = _classList call bis_fnc_selectrandom;
	_dirToTarget = if (isMultiplayer) then {
		_targetTemp = _target select 1;
		([_player,_targetTemp] call bis_fnc_dirTo)
	} else {
		_targetTemp = _target select isMultiplayer;
		([_player,_targetTemp] call bis_fnc_dirTo) + 180
	};
	_pos = [position _player, 180 + random 40, _dirToTarget + 90 + random 180] call BIS_fnc_relPos;
	_unit = _grp createunit [_class,_pos,[],1,""];
	_unit setskill (random 0.5);
	_unit allowfleeing 0;
	_unit setvehicleammo (0.5 + random 0.5);
	if (random 1 > 0.5) then {_unit setunitpos "up"} else {_unit setunitpos "middle"};
	[_player,"BIS_spawnedEnemies",[_unit]] call bis_fnc_variablespaceadd;
	_wp setwaypointposition [position _player,0];

	_unit call _code;

	sleep _delay;
};

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  

×