Jump to content
Retrosniper

Spawning units when player is within radius of marker

Recommended Posts

Hi All,

I'm New to the forums and to scripting i'm well out of my depth but i'm learning slowly.

i'm sure this has been asked before sorry if it has.

I'm having some issues with my script.

I want to set up multiple markers that when a player that is alive is within a radius is near this marker it spawns units from a list of possible units. I want all the values the units the spawn radius and etc to be customisabled.

I'd love to know how to get them to spawn in buildings as well as on different floors.

These spawn points would be through the map in towns and other locations.

At the moment i only have it set to 1 marker and 1 unit type but the units don't spawn and i'm a bit lost as to what i should do i can't seem to figure it out.

I'd rather create something my self than use already existing scripts (i'm being difficult i know sorry)

My script is below I'm not sure if ive pasted it in correctly let me know.

Thanks all.

_opforMarker = "Opfor_1"; // Name of the marker where the OPFOR units will spawn around
_spawnRadius = 100; // Radius around the marker where units can spawn
_opforUnits = ["O_Soldier_F"]; // Types of units that can spawn

// Get the position of the marker
_markerPos = getMarkerPos _opforMarker;

// Create a new group for the spawned units
_newGroup = createGroup east;

// Define the trigger area
_triggerPos = _markerPos;
_triggerRadius = 50;

// Create the trigger
_opforTrigger = createTrigger ["EmptyDetector", _triggerPos];
_opforTrigger setTriggerArea [_triggerRadius, _triggerRadius, 10, false];
_opforTrigger setTriggerActivation ["WEST", "PRESENT", true];
_opforTrigger setTriggerStatements ["True", "hint 'trigger on'", "hint 'trigger off'"];

// Spawn units around the marker
{
    _unitPos = [_markerPos, _spawnRadius, 360] call BIS_fnc_findSafePos;
    _unitType = selectRandom _opforUnits;
	_unitType createUnit [_unitPos, _newGroup, "_newUnit = this"];
} 

 

Share this post


Link to post
Share on other sites
15 minutes ago, Retrosniper said:

... "_newUnit = this"];

 

Not sure what you are trying to do here. That local variable will only exist in that init , and you are doing nothing with it.

Share this post


Link to post
Share on other sites
2 minutes ago, Harzach said:

 

Not sure what you are trying to do here. That local variable will only exist in that init , and you are doing nothing with it.

I was reccomended that by a friend.
what would you suggest instead?

Share this post


Link to post
Share on other sites

im getting errors on line 24 with this below code saying no ; im unsure as to whats going on.
 

_opforMarker = "Opfor_1";
_spawnRadius = 100;
_opforUnits = ["O_Soldier_F"];


_markerPos = getMarkerPos _opforMarker;


_newGroup = createGroup east;


_triggerPos = _markerPos;
_triggerRadius = 50;


_opforTrigger = createTrigger ["EmptyDetector", _triggerPos];
_opforTrigger setTriggerArea [_triggerRadius, _triggerRadius, 0, false];
_opforTrigger setTriggerActivation ["WEST", "PRESENT", true];


{
    _unitPos = [_markerPos, _spawnRadius, 360] call BIS_fnc_findSafePos;
    _unitType = selectRandom _opforUnits;
	_unit = createUnit [_unitType, _unitPos, [], 0, "NONE"]; // create the unit
    _unit setGroupId [_newGroup];
};

 

Share this post


Link to post
Share on other sites
36 minutes ago, Retrosniper said:

im unsure as to whats going on.

 

Syntax error. Standard syntax for createUnit is:

Quote

group createUnit [type, position, markers, placement, special]


You are missing the group reference.

_unit = _newGroup createUnit [_unitType, _unitPos, [], 0, "NONE"]; // create the unit

 

1 hour ago, Retrosniper said:

what would you suggest instead?

 

Nothing? Unless there is something specific you are trying to do with that init line.

Share this post


Link to post
Share on other sites
12 minutes ago, Harzach said:

_unit = _newGroup createUnit [_unitType, _unitPos, [], 0, "NONE"]; // create the unit

 

Is this for the first iteration of the script or the second? ive tested on both and nothing spawns still 

Share this post


Link to post
Share on other sites

You could use it for either.

 

This makes no sense:

_unit setGroupId [_newGroup];

 

Your trigger is doing nothing.

 

You also have unnecessary curly brackets around your position/unit/spawn block.

_opforMarker = "Opfor_1";
_spawnRadius = 100;
_opforUnits = ["O_Soldier_F"];

_markerPos = getMarkerPos _opforMarker;

_newGroup = createGroup east;

_unitPos = [_markerPos, _spawnRadius, 360] call BIS_fnc_findSafePos;
_unitType = selectRandom _opforUnits;
_unit = _newGroup createUnit [_unitType, _unitPos, [], 0, "NONE"];

This works fine, spawning one enemy unit in a safe position somewhere between 100 and 360 meters from the center of the marker "Opfor_1."

Share this post


Link to post
Share on other sites

My advice: you will learn by an order of magnitude faster if you take things one at a time. Break your idea down into chunks. Learn how each command/function works (USE THE WIKI!) using simple examples. Start adding things together and see how they interact. Before long, you will have a solid foundation from which to solve more complex problems.

  • Like 1

Share this post


Link to post
Share on other sites
_opforMarker = "Opfor_1"; // Name of the marker where the OPFOR units will spawn around
_spawnRadius = 100; // Radius around the marker where units can spawn
_opforUnits = ["O_Soldier_F"]; // Types of units that can spawn

// Get the position of the marker
_markerPos = getMarkerPos _opforMarker;

// Create a new group for the spawned units
_newGroup = createGroup east;

// Define the trigger area
_triggerPos = _markerPos;
_triggerRadius = 50;

// Create the trigger
_opforTrigger = createTrigger ["EmptyDetector", _triggerPos];
_opforTrigger setTriggerArea [_triggerRadius, _triggerRadius, 10, false];
_opforTrigger setTriggerActivation ["WEST", "PRESENT", true];
_opforTrigger setTriggerStatements ["True", "hint 'trigger on'", "hint 'trigger off'"];

// Spawn units around the marker
    _unitPos = [_markerPos, _spawnRadius, 360] call BIS_fnc_findSafePos;
    _unitType = selectRandom _opforUnits;
	_unit = _newGroup createUnit [_unitType, _unitPos, [], 0, "NONE"]; // create the unit

So with the changes youve suggested how could i link the trigger to activate and then spawn the units because if they are currently not tied together then its activating the trigger but not spawning the units.

Share this post


Link to post
Share on other sites

The most basic way would be to place the "Spawn units" section into the trigger statements (second element, which is the "onAct.")

 

You also have your condition statement set to "True" which makes the trigger fire immediately upon creation. We'll change it to "this" so that it will fire when the activation statement is true.

_opforMarker = "Opfor_1";
_spawnRadius = 100;
_opforUnits = ["O_Soldier_F"];

_markerPos = getMarkerPos _opforMarker;

_newGroup = createGroup east;

_triggerPos = _markerPos;
_triggerRadius = 50;

_opforTrigger = createTrigger ["EmptyDetector", _triggerPos];
_opforTrigger setTriggerArea [_triggerRadius, _triggerRadius, 10, false];
_opforTrigger setTriggerActivation ["WEST", "PRESENT", true];
_opforTrigger setTriggerStatements 
[
	"this", 
	"
		hint 'trigger on';
		_unitPos = [_markerPos, _spawnRadius, 360] call BIS_fnc_findSafePos;
		_unitType = selectRandom _opforUnits;
		_unit = _newGroup createUnit [_unitType, _unitPos, [], 0, 'NONE'];
	", 
	"hint 'trigger off'"
];

 

Share this post


Link to post
Share on other sites

Thanks dude.

I was talking on the discord as well and i got a response from Lepoard20 they wrote this up for me. 
It works and everyhing im going to keep working on this and try to improve it and learn more and more.

I just want to make a array and it selects units from that creates a squad and creates a random amount of squads with a max squad limit per objective.

_opforMarker = "Opfor_1"; // Name of the marker where the OPFOR units will spawn around

// Get the position of the marker
_markerPos = getMarkerPos _opforMarker;

// Define the trigger area
_triggerPos = _markerPos;
_triggerRadius = 50;

// Create the trigger
_opforTrigger = createTrigger ["EmptyDetector", _triggerPos];
_opforTrigger setTriggerArea [_triggerRadius, _triggerRadius, 10, false];
_opforTrigger setTriggerActivation ["WEST", "PRESENT", true];
_opforTrigger setTriggerStatements ["this", 
toString {
    hint 'trigger on';
    // Spawn units around the marker
    _opforMarker = "Opfor_1"; // Name of the marker where the OPFOR units will spawn around
    _spawnRadius = 100; // Radius around the marker where units can spawn
    _opforUnits = ["O_Soldier_F"]; // Types of units that can spawn

    // Get the position of the marker
    _markerPos = getMarkerPos _opforMarker;

    // Create a new group for the spawned units
    _newGroup = createGroup east;

    // Define the trigger area
    _triggerPos = _markerPos;
    _triggerRadius = 50;
    _unitPos = [_markerPos, _spawnRadius, 360] call BIS_fnc_findSafePos;
    _unitType = selectRandom _opforUnits;
    _unit = _newGroup createUnit [_unitType, _unitPos, [], 0, "NONE"]; // create the unit
}, "hint 'trigger off'"];

 

Share this post


Link to post
Share on other sites

Something to get you started/learn from. It's basically your code from OP just reordered and made to handle east or west markers rather than dependent on a single side.

Spoiler

//initServer.sqf

TAG_fnc_unitMarkerSpawner = {
	params[
		[ "_spawnMarker", "", [ "" ] ],
		[ "_spawnRadius", 100, [ 0 ] ],
		[ "_spawnSide", east, [ sideUnknown ] ], 
		[ "_spawnAmount", 5, [ 0 ] ]
	];
	
	//Error check passed parameters
	if ( _spawnMarker == "" || { getMarkerPos _spawnMarker isEqualTo [0,0,0] } ) exitWith { "Invalid spawnMarker given" call BIS_fnc_error; };
	if !( _spawnSide in [ east, west ] ) exitWith { "spawnMarkers only works with East or West sides" call BIS_fnc_error };
	
	//Get _spawnSide enumeration
	_spawnSideID = _spawnSide call BIS_fnc_sideID; //East = 0, West = 1
	
	// Types of units that can spawn
	_spawnUnitTypes = [
		[ "O_Soldier_F" /*Add more east types here*/ ], //East types
		[ "B_Soldier_F" /*Add more west types here*/ ]  //West types
	] select _spawnSideID; //Select types based on _spawnSideID
	
	
	// Define the trigger area
	_triggerRadius = 50;
	//Define the triggers Activation side
	_triggerSide = [ "WEST", "EAST" ] select _spawnSideID; //Select opposite side to _spawnSideID
	
	// Create the spawnMarker trigger
	_spawnTrigger = createTrigger[ "EmptyDetector", getMarkerPos _spawnMarker, false ]; //false, only on server
	_spawnTrigger setTriggerArea[ _triggerRadius, _triggerRadius, 10, false ];
	_spawnTrigger setTriggerActivation[ _triggerSide, "PRESENT", true ];
	_spawnTrigger setTriggerStatements[ "this", "thisTrigger call TAG_fnc_spawnMarkerUnits", "" ];
	
	//Set spawn info on the trigger
	_spawnTrigger setVariable[ "markerSpawnInfo", [ _spawnRadius, _spawnSide, _spawnAmount, _spawnUnitTypes ] ];
};


//Called by spawnMarker trigger
TAG_fnc_spawnMarkerUnits = {
	params[ "_spawnTrigger" ];
	
	//Get info from trigger
	_spawnTrigger getVariable[ "markerSpawnInfo", [] ] params[ "_spawnRadius", "_spawnSide", "_spawnAmount", "_spawnUnitTypes" ];
	
	//Get a safe position for the group to spawn
	_spawnGroupPos = [ getPosATL _spawnTrigger, 0, _spawnRadius ] call BIS_fnc_findSafePos;
	
	//Create group
	_spawnGroup = createGroup[ _spawnSide, true ]; //Delete group when empty
	
	// Spawn units around the marker
	for "_i" from 1 to _spawnAmount do {	    
	    _spawnGroup createUnit[ selectRandom _spawnUnitTypes, _spawnGroupPos, [], 0, "FORM" ];
	} 
};



//Wait for mission to start
waitUntil{ time > 0 };

//Initialise spawn Markers
{
	_x call TAG_fnc_unitMarkerSpawner;
}forEach [
	//Marker name,   spawn radius,      spawn side,      num units to spawn
	[ "Opfor_1",       100,               east,            10 ]//, 
	//Add as many markers as needed with associated parameters here
];

 

Untested

Share this post


Link to post
Share on other sites

Hey all im still alive went on holidays for a bit and was busy with study. 

Never expected Larrow and Harzach to come in and help ahah legends thanks for that.

so a few things i want to do ill try to describe it as best i can.

I basically want to create my own version of liberation but bare bones. Sorry Psiko and KP team.

i will add more scripts over time and expand further but i want these functions working as the core of the system first

I want to have three scripts one with lists of units that are defending and one thats a reinfocement script (so that they can be customised easily) that are arrays that are globally accessable and the same but for different markers i want the spawning script to only handle the spawning and calling of things from other scripts i dont want to have the lists of units and markers in that script to long to many variables my god the horror.

i want the ai to spawn once being triggered by the marker when the players enter a 1.5km radius (i know it could cause alot of performance issues but i want vehicles to be able use their weapons at range more realistic blah blah also i know how to do that) the players clear the area and capture the marker and it changes it to blue the enemy can then come back and take those markers.

and somehow work out how to have the ai commander or whatever controls the ai call in reinforcements any thing you guys can point me towards?

For example like the below. 
1st have i create a global variable correctly here? do i need to define it somewhere like in the init.sqf for it to be able to be called by other scripts?
2nd basically the same but with marker names instead humor me haha.
 

Quote

// [CLASSNAME] these are just Infantry it will select units at random from this list and create a squad.
Defensive_Infantry = [ 
"rhs_vdv_aa",
"rhs_vdv_at",
"rhs_vdv_arifleman",
"rhs_vdv_"grenadier_rpg,
"rhs_vdv_marksman",
"rhs_vdv_medic",
"rhs_vdv_grenadier_alt",
"rhs_vdv_rifleman",
"rhs_vdv_LAT"
];


I will eventually do a script for a ui for that shows a  capture rate and whatever.

heres the current spawning script below.

any help or directions to point me in would be great thanks again gang.
 

Quote

_opforMarker = "Opfor_1"; // Name of the marker where the OPFOR units will spawn around

// Get the position of the marker
_markerPos = getMarkerPos _opforMarker;

// Define the trigger area
_triggerPos = _markerPos;
_triggerRadius = 50;

// Create the trigger
_opforTrigger = createTrigger ["EmptyDetector", _triggerPos];
_opforTrigger setTriggerArea [_triggerRadius, _triggerRadius, 10, false];
_opforTrigger setTriggerActivation ["WEST", "PRESENT", true];
_opforTrigger setTriggerStatements ["this", 
toString {
    hint 'trigger on';
    // Spawn units around the marker
    _opforMarker = "Opfor_1"; // Name of the marker where the OPFOR units will spawn around
    _spawnRadius = 100; // Radius around the marker where units can spawn
    _opforUnits = [
    "rhs_vdv_mflora_junior_sergeant",
    "rhs_vdv_mflora_marksman",
    "rhs_vdv_mflora_rifleman_lite",
    "rhs_vdv_mflora_rifleman_lite",
    "rhs_vdv_mflora_medic",
    "rhs_vdv_mflora_grenadier_rpg",
    "rhs_vdv_mflora_rifleman",
    "rhs_vdv_mflora_rifleman",
    "rhs_vdv_mflora_at",
    "rhs_vdv_mflora_grenadier",
    "rhs_vdv_mflora_arifleman_rpk"
]; 
    // Types of units that can spawn ^^

    // Get the position of the marker
    _markerPos = getMarkerPos _opforMarker;

    // Define the trigger area
    _triggerPos = _markerPos;
    _triggerRadius = 50;

    // Loop through and create up to 10 EAST groups
    _array = [3, 5, 7];
    _random = selectRandom _array;
    for "_i" from 1 to _random do {
        _newGroup = createGroup east;
        _newGroup deleteGroupWhenEmpty true;

        // Create a new unit in the new group
        array = [3, 5, 7];
        _random = selectRandom _array;
        for "_i" from 1 to _random    do {
        _unitPos = [_markerPos, _spawnRadius, 360] call BIS_fnc_findSafePos;
        _unitType = selectRandom _opforUnits;
        _unit = _newGroup createUnit [_unitType, _unitPos, [], 0, "NONE"]};

        // Sleep for a bit to avoid spawning too quickly
        sleep 1; //change so that it sleeps until all players leave the area then clear units and spawn again cooldown on how long players must leave area before deleting units 
    };
}, "hint 'trigger off'"];




 

 

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

×