Jump to content
Sign in to follow this  
DChristy87

Help with a script? - Help or suggestions, please!

Recommended Posts

I'm trying to create a simple AI spawning script. I want the AI to spawn when a player gets within a 200 meter radius of a game logic.

I create a game logic and put this in the init field:

nul = this execVM "test.sqf";

test.sqf:

_AISpawn = _this;
_pos = getPos _AISpawn;
_list = _AIspawn nearEntities ["Man", 200];
_players = [];
waituntil {!isnil "bis_fnc_init"};
while {true} do 
{
{
	_players = isPlayer _x
}forEach _list;

_cnt = count _players;
while {_cnt > 0} do
{
	_side = createCenter East;
	_grp = creategroup East;
	_grp = [getPos _pos, EAST, ["o_soldier_f", "o_soldier_f"]] call BIS_fnc_spawnGroup;
	[_grp, getPos _pos, 45] call bis_fnc_taskPatrol;
};
};

I get an error on line 17, "type bool, expected array"

Any ideas?

---------- Post added at 18:02 ---------- Previous post was at 17:58 ----------

Nevermind, I'm an idiot. I had to put [] around _players:

_cnt = count [_players];

---------- Post added at 18:15 ---------- Previous post was at 18:02 ----------

Ehh, I also had to change the BIS_fnc_spawnGroup and the taskPatrol lines to this:

_grp = [_pos, EAST, ["o_soldier_f", "o_soldier_f"]] call BIS_fnc_spawnGroup;

[_grp, _pos, 45] call bis_fnc_taskPatrol;

BUUUUUUT now my mission freezes up... lol.

I've found out its because its spawning more and more of the groupSpawns :confused:

Share this post


Link to post
Share on other sites

Okay so you want the A.I. to spawn only when the player gets within a certain distance. This may or may not help but I HIGHLY advise you check out the modules. There is one in picticular that will help you in this situation. I won't make it easy for you because well you need to know what tools you alreay have available.

Share this post


Link to post
Share on other sites

This sort of answer is precisely why people don't post in the forum and the community loses content. Next time you take your car to get fixed or your fridge breaks down remember how you answered this post then look in your tool chest because you too have the tools to fix it .. ;)

Share this post


Link to post
Share on other sites

I do appreciate the tip Johnson. However, I am aware of the modules and the few out of date tutorials for using them. I'm really just trying to learn scripting, and just hoping for some help along the way.

Share this post


Link to post
Share on other sites

@DaSkunk how else are you going to learn something if every time you get stuck you have to ask for help when BIS took the time to create something for this situation? What is wrong with knowing all the tools you have available? DChristy87 a trigger would a lot simpler. You wouldn't need all that code just the spawn group and the patrol task. But I'm telling you there is a totally simpler way and all you have to do is place the group and a module and you're done.

Share this post


Link to post
Share on other sites

Because all of the modules do not have any documentation for them anyway yet! .... and sometimes, if you want to advance your learning and understanding of the code to make good functional missions you have to come on forums like this for advice and as DChristy mentioned a pointer in the right direction.

Share this post


Link to post
Share on other sites

He's real close, but if you look at the code you can see why it's locking up. He's constantly checking if players are in the zone. Once they are in the zone he's constantly spawning groups as long as a count of players in the zone is greater than 0. That's what I'm talking about with limiting the spawn. If he's got this running from a trigger that means the server and all other players are all spawning and spawning... a mess. :)

So first figure out how you want to spawn your group. Get a function setup for that, you're close but there's a few little things you don't have right (like drop the functions init thing that's arma2).

Once you have your function to spawn the group then come up with a solution for triggering it and marking it while it's active and how to mark it inactive. Sometimes a trigger onAct/onDeAct works, other times you just set a variable for 'it's on like donkey kong' and other times you just check for bad guys in the area. Figure out how you want that to work and build that part of it.

So take it step by step and you'll do better. :)

Share this post


Link to post
Share on other sites

I give up I will give a simpler answer. Just spawn the group in the 2D editor and in the module category: Object Modifiers, module name is Simulation Manager. Just put 200 in the radius the A.I. will spawn when a player gets in range. Now they technically don't spawn it just that their actions are suspended and the A.I. are hidden until you get in range. When you are in range their simulation will resume. This is very helpful if you have enemies all of the map. Now this works for every A.I. in your mission. If you click on Show Info tab on the bottom of every module it will give an example on how to implemented. The Show / Hide Module can hide/show what units you want instead of all the A.I.

Share this post


Link to post
Share on other sites

Thanks fellas. Kylania, You're right, of course, when I managed to hit esc and break out of the loop I saw a hundred or more AI all spawned on the game logic. I appreciate the guidance as usual Kylania! I'll try and come up with something as you suggested to make it only happen once. Johnson, I see your brother was in the Marine Corps. I was stationed in Camp Lejeune with 1/9 for a few years and 3/9 for the rest of my enlistment. I appreciate the sacrifice your brother and your family made, Semper Fi.

Share this post


Link to post
Share on other sites
Thanks fellas. Kylania, You're right, of course, when I managed to hit esc and break out of the loop I saw a hundred or more AI all spawned on the game logic. I appreciate the guidance as usual Kylania! I'll try and come up with something as you suggested to make it only happen once. Johnson, I see your brother was in the Marine Corps. I was stationed in Camp Lejeune with 1/9 for a few years and 3/9 for the rest of my enlistment. I appreciate the sacrifice your brother and your family made, Semper Fi.

Thanks a lot brother.

Share this post


Link to post
Share on other sites

Here's an example of the first thing I was talking about, a function to spawn a group and make them patrol. Basically I took your spawn code and made it optional.

CHR_fnc_spawnGroup = {
_startPos = [_this, 0, [0,0,0]] call BIS_fnc_param;
_patrolPos = [_this, 1, [0,0,0]] call BIS_fnc_param;
_units = [_this, 2, ["O_soldier_F", "O_soldier_F"], [["O_soldier_F"], (configfile >> "CfgGroups" >> "East" >> "OPF_F" >> "Infantry" >> "OIA_InfSentry")]] call BIS_fnc_param;
_dist = [_this, 3, 50, [50]] call BIS_fnc_param;
_side = [_this, 4, opfor, [opfor]] call BIS_fnc_param;

_grp = [_startPos, _side, _units] call BIS_fnc_spawnGroup;
[_grp, _patrolPos, _dist] call bis_fnc_taskPatrol;
};

Can be called in multiple ways:

// Spawn two OPFOR soldiers at spawn marker and patrol patrol marker, 50m.

0 = [getMarkerPos "spawn", getMarkerPos "patrol"] call CHR_fnc_spawnGroup;

// Spawn a sniper team at a marker called start and have them patrol 50 meters around a marker called patrol.

0 = [getMarkerPos "start", getMarkerPos "patrol", (configfile >> "CfgGroups" >> "East" >> "OPF_F" >> "Infantry" >> "OI_SniperTeam")] call CHR_fnc_spawnGroup;

// Spawn two OPFOR soldiers at a helicopter and have them patrol 100m around a flagpole. (This one uses a trick, that nil might be confusing but it's giving a wrong value in order to default)

0 = [getPos someHeli, getPos flagPole, nil, 100] call CHR_fnc_spawnGroup;

// Spawn 3 BLUFOR crewmen at the player and have them patrol 60 meters from the base marker

0 = [getPos player, getMarkerPos "base", ["B_crew_F", "B_crew_F", "B_crew_F"], 60, blufor] call CHR_fnc_spawnGroup;

The last two lines are exactly the same as your two lines to spawn things, we just have full control over them now. Also notice how nothing in those lines is hard coded. Defaults are set in the params though.

First two positions will default to [0,0,0] if nothing is given.

The units will default to 2 OPFOR soldiers, but will accept any array of classnames or a config entry.

Distance will default to 50 but accept any number.

Side will default to opfor but accept any side.

Share this post


Link to post
Share on other sites

Nice man, That helps a lot with my understanding of how a function itself works. I guess what I really don't understand is when you create a function like that "CHR_fnc_spawnGroup"... where it, itself, is placed (like do you define it in the init.sqf?) Also, when you use something like

0 = [getMarkerPos "spawn", getMarkerPos "patrol"] call CHR_fnc_spawnGroup;

how it knows to get to the "CHR_fnc_spawngroup" without being referred to an actual .sqf file. Like, really whats the difference from creating a "CHR_FNC_WHATEVER" and just calling an sqf (in Arma Edit, it refers to .sqf as a "function file") so why not just use that? Sorry if I'm hard to follow. It's just hard for me to wrap my head around it. :confused:

Share this post


Link to post
Share on other sites

This is pretty indepth, but it explains all about Functions: /wiki/Functions_Library_(Arma_3)

Basically creating a function creates a global variable which can be used to run the code. execVM does exactly the same thing but reads the script file every time it's executed. So you use functions so that you "read" it once and execute it many times since the game already knows the code.

Think of it as speed dial on a telephone. execVM would be looking up the number and manually dialing it everytime you wanted to call someone. It works just fine but takes a lot of time every time you do it. However if you add a number to speed dial (create a function with the code) you can than just press 1 to dial Suzy without reading the number every time. Faster booty calls through functions!

You can declare the function anywhere really. Ideally you'd "register the function" as listed on that page but I haven't tried that yet. Generally I either have them in the init.sqf or call them via a separate script with just functions, [] execVM "loadFunctions.sqf" kind of thing. That way all clients have access to them when needed.

Once you have your function you can either call or spawn it. Call will run through the function quickly before anything else happens. Spawn will start the function running on it's own separately. Calls can't sleep but spawns can is basically how I remember it.

Share this post


Link to post
Share on other sites

Nice, that explains a lot! I'll be reading over the wiki link several times, I'm sure. Again, I appreciate all the knowledge passed my way! "Faster booty calls through functions!" - Now THAT really made some sense to me!

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  

×