Operation Flashpoint Dragon Rising OFDR Forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Spawning and Controlling Spawned Units TemplarGFX

2 posters

Go down

Spawning and Controlling Spawned Units TemplarGFX Empty Spawning and Controlling Spawned Units TemplarGFX

Post by oANIMUSo Sat 09 Aug 2014, 23:22

TemplarGFX
Senior Member
 
Spawning and Controlling Spawned Units TemplarGFX C:\Users\Aaron\AppData\Local\Temp\msohtmlclip1\01\clip_image001
 
Join Date: Oct 2009

Posts: 4,955 

Hi there, there seems to be a lot of questions regarding spawning of units, and getting them to do stuff. so here is a tutorial of sorts :
 
 
Creating a Fireteam the right way!
 
in order to easily find, store and order newly spawned units, the name of each element must be considered carefully
 
Spawning and Controlling Spawned Units TemplarGFX C:\Users\Aaron\AppData\Local\Temp\msohtmlclip1\01\clip_image001
 
here I have created a custom fireteam. notice the name of the echelon, and the names of the units. you will see why in a moment. All the units and the Echelon entity are a part of the same set. only THIS fireteam and its echelon should be in this set. for another fireteam (with different units), create another entity set!
Say you want to spawn 5 of this fireteam, DON'T create 5 separate fireteams with separate entity sets!
This fireteam contains a Leader, a Medic, a Rifleman and an Anti Tank specialist.
you only create a new fireteam and entity set if you want other specialties (such as a sniper fireteam)
 
Entity Sets, and why naming is important!
 
Spawning and Controlling Spawned Units TemplarGFX C:\Users\Aaron\AppData\Local\Temp\msohtmlclip1\01\clip_image002
 
here is the explorer view. notice how the entities are ordered in the list on the right? the Echelon entity is at the top, followed by the leader of the echelon. this is EXTREMELY important. if the entity is not at the top, problems WILL occur.
Internal ordering of Entity Sets is done via alphabetical order, keep this in mind when renaming everything!
 
were about to get to the code, where everything will (hopefully) start to make sense! but first, we need somewhere to spawn them!
 
Spawning and Controlling Spawned Units TemplarGFX C:\Users\Aaron\AppData\Local\Temp\msohtmlclip1\01\clip_image003
 
and them somewhere to send them!
 
Spawning and Controlling Spawned Units TemplarGFX C:\Users\Aaron\AppData\Local\Temp\msohtmlclip1\01\clip_image004
 
these are just quick examples. you call these whatever you like, but keeping there names similar makes coding A LOT easier!
 
 
ok, now onto the code
 
first we need to setup a few things in the onCreate() function
Code:
function onCreate()
--this is a table containing all the possible spawn points for the PLA fireteam(s) we will spawn
PLA_spawn_points = {"PLAspawn1", "PLAspawn2", "PLAspawn3", "PLAspawn4"}
--this is a table containing all the possible defend points for the PLA fireteam(s) we will spawn
PLA_defend_points = {"PLAdef1", "PLAdef2", "PLAdef3", "PLAdef4"}
--this is a table containing all the entity sets we want to choose from, heres its just one, but you can have more
PLA_fireteam_sets = {"aaPLA1set"}
--we use a timer to spawn the units, you can add this timer to another function for more control
OFP:addTimer("SpawnTimer",5000);
end
So as you can see, we are using tables here to store everything. this makes coding much easier, and allows a lot more random-ness to our spawns
 
Defend points could be waypoint paths, other entities. anything you want*
 
Also, for this example, I'm using a timer to spawn a single fireteam. you could place the next bit of code inside any function you wish, it doesn't have to be a timer!
 
Getting things to spawn!
Code:
function onTimerSpawnTimer()
--choose a random whole number between 1, and the total number of spawn points
local spawn_rand = math.random(1,#PLA_spawn_points);
--choose a random whole number between 1, and the total number of entity sets
local set_rand = math.random(1,#PLA_fireteam_sets);
--get the X,Y,Z location of the randomly chosen spawn point
local x,y,z = OFP:getPosition(PLA_spawn_points[spawn_rand]);
--finally we spawn our random fireteam at a random location
OFP:spawnEntitySetAtLocation(PLA_fireteam_sets[set_rand], x, y, z);
end
here we choose a random location, then we choose a random fireteam and spawn that fireteam at the chosen location.
Notice the lack of "OFP:teleport()" that command is not needed, and just causes problems. by spawning this way, you can give any order to the spawned fireteam without worrying about it not following that order!
 
finally, the most important part, and why the "how we name stuff" is important
 
Order those spawned bad guys!
Code:
function onSpawnedReady(setName, setID, tableOfEntities, errorCode)
--thanks to the naming of our echelon and units, the first entry in this table, is the echelon (you could store this)
local echelon_name = tableOfEntities[1];
--choose a random whole number between 1, and the total number of defend points
local defend_rand = math.random(1,#PLA_defend_points);
--time to give an order to the newly spawned echelon
OFP:move(echelon_name, PLA_defend_points[defend_rand], "OVERRIDE");
end
When something is spawned, this function is called when its ready to be interacted with. this function returned a table containing all of the entities that were spawned at that time. This table is ordered the same way the entity set is. so, the first entry in this table will be our Echelon, the second entry will be the fireteam leader, etc etc.
This allows us to easily give orders to the fireteam! it also means, if you want to be a lot more complex, you can store the echelon name into a table for use later (not shown here).
 
One thing to note, when you spawn entity sets, rather than activating them. every entity in that set is given a unique name, in other words, they are not called whatever you called them in the Mission Editor.
 
here we use a simple move order. but it can be any order you wish. but take note. if your table of points contains waypoints, only orders that work with waypoints can be given. similarly if your table contains units. only orders like follow and assault will work. keep this in mind!
 
Now, it may LOOK like a lot of code, but its actually not, lets have a look at the whole thing, without comments :
Code:
function onCreate()
PLA_spawn_points = {"PLAspawn1", "PLAspawn2", "PLAspawn3", "PLAspawn4"}
PLA_defend_points = {"PLAdef1", "PLAdef2", "PLAdef3", "PLAdef4"}
PLA_fireteam_sets = {"aaPLA1set"}
OFP:addTimer("SpawnTimer",5000);
end
 
function onTimerSpawnTimer()
local spawn_rand = math.random(1,#PLA_spawn_points);
local set_rand = math.random(1,#PLA_fireteam_sets);
local x,y,z = OFP:getPosition(PLA_spawn_points[spawn_rand]);
OFP:spawnEntitySetAtLocation(PLA_fireteam_sets[set_rand], x, y, z);
end
 
function onSpawnedReady(setName, setID, tableOfEntities, errorCode)
local echelon_name = tableOfEntities[1];
local defend_rand = math.random(1,#PLA_defend_points);
OFP:move(echelon_name, PLA_defend_points[defend_rand], "OVERRIDE");
end
finally, here is an MSSN file for you to look at, and see it in action :
 
Spawning Tutorial example MSSN
 
 
***I'm not very good at writing tutorials, please let me know if things need explaining more!***
__________________
"In the ground!"
"In the ground!"
"In the ground!"
"Dead!"
"In the ground!"
"Dead!"

"In the ground!"
oANIMUSo
oANIMUSo

Location : Florida, U.S.
Points : 94
Reputation : 1
Join date : 2012-12-22
Age : 52

https://www.youtube.com/user/oANIMUSoTube?feature=mhee

Back to top Go down

Spawning and Controlling Spawned Units TemplarGFX Empty Re: Spawning and Controlling Spawned Units TemplarGFX

Post by oANIMUSo Sat 09 Aug 2014, 23:24

SiMpLE Jack
Senior Member
 
Spawning and Controlling Spawned Units TemplarGFX C:\Users\Aaron\AppData\Local\Temp\msohtmlclip1\01\clip_image001
 
Join Date: Oct 2009
Posts: 290
 
 
I tried tacking on a patrol command but nothing I do is working, no doubt because I don't fully understand the preceding script to begin with.
 
Adding a path to each random waypoint is easy enough, and they follow that. But that requires no code other than changing waypoint names to path names
 
PLA_defend_points = {"PLAdef1p", "PLAdef2p", "PLAdef3p", "PLAdef4p"}
 
But that's not what I'm after. I tried a few obvious onArriveAtWaypoint options to add a patrol command but no go.
Code:
function onArriveAtWaypoint_aaPLA1a_PLAdef1(entityName, waypointName)
OFP:patrol("aaPLA1", "PLAdef1p", "EBACKANDFORTH", 1, "ADDTOFRONT")
OFP:disableEvent("onArriveAtWaypoint_aaPLA1a_PLAdef1")
end
I also tried starting the patrol at the Last waypoint, since the path is working ok, but the just stop at the last point as if there was no patrol command.


Last edited by SiMpLE Jack; 24-11-2009 at 04:33 PM.
oANIMUSo
oANIMUSo

Location : Florida, U.S.
Points : 94
Reputation : 1
Join date : 2012-12-22
Age : 52

https://www.youtube.com/user/oANIMUSoTube?feature=mhee

Back to top Go down

Spawning and Controlling Spawned Units TemplarGFX Empty Re: Spawning and Controlling Spawned Units TemplarGFX

Post by oANIMUSo Sat 09 Aug 2014, 23:25

TemplarGFX
 
Code:
function onArriveAtWaypoint_aaPLA1a_PLAdef1(entityName, waypointName)
OFP:patrol("aaPLA1", "PLAdef1p", "EBACKANDFORTH", 1, "ADDTOFRONT")
OFP:disableEvent("onArriveAtWaypoint_aaPLA1a_PLAdef1")
end
your problem with the above code, is that you are attempting to find out when aaPLA1a (the leader of the echelon) arrives at the waypoint, however :
Quote:
Originally Posted by templargfx
One thing to note, when you spawn entity sets, rather than activating them. every entity in that set is given a unique name, in other words, they are not called whatever you called them in the Mission Editor.
the unit will not be called aaPLA1a, it will likely have a name like lun32n5 or some other random combination.
 
is your intention to first have the unit move to a waypoint, then start patrolling a path once its reached it?
 
if so, you could go the MUCH simpler approach. in onSpawnedReady queue up 2 commands for the spawned team :
 
OFP:move(echelonName,move_waypoint,"OVERRIDE");
OFP
Spawning and Controlling Spawned Units TemplarGFX C:\Users\Aaron\AppData\Local\Temp\msohtmlclip1\01\clip_image001
atrol(echelonName,patrol_point,1,"EBACKANDFOR TH","ADDTOEND");
 
the unit will move to the first waypoint, and then start patrolling a path.
 

give me a fuller explanation of what you are attempting to achieve and I will write out a better/fuller example
oANIMUSo
oANIMUSo

Location : Florida, U.S.
Points : 94
Reputation : 1
Join date : 2012-12-22
Age : 52

https://www.youtube.com/user/oANIMUSoTube?feature=mhee

Back to top Go down

Spawning and Controlling Spawned Units TemplarGFX Empty Re: Spawning and Controlling Spawned Units TemplarGFX

Post by oANIMUSo Sat 09 Aug 2014, 23:26

Haywood Slap
Senior Member
 
Join Date: Oct 2009
Location: New York
Posts: 808
 
That is because OFP renames the echelons and units when you use spawnEntitySetAtLocation so there is no unit named "aaPLA1a" after spawning, so that onArriveAtWaypoint function will never be triggered. The easiest solution is to simply tack the patrol command onto the units command queue at the same time you give the move order.
Code:
function onSpawnedReady(setName, setID, tableOfEntities, errorCode)
...
local def = PLA_defend_points[defend_rand]
OFP:move(echelon_name, def, "ADDTOEND")
OFP:patrol(echelon_name, def, "EBACKANDFORTH", 1, "ADDTOEND")
end
Things get a little trickier if you want to respawn the entity sets as you need to keep track of the setID (so you can destroy it) and the name of the set you need to respawn. You can use two tables for this:
Code:
-- Table used to look up the entity set id for the named unit/echelon
OFPName2ID = {}
-- Table used to look up the real entity set name for an echelon
OFPName2SetName = {}
 
function onSpawnedReady(setName, setID, tableOfEntities, errorCode)
...
local echelon_name = tableOfEntities[1]
-- Record the set ID and real set name (defined in the editor)
OFPName2ID[echelon_name] = setID
OFPName2SetName[echelon_name] = setName
end
 
function onDeath(victim, killer)
local echelon = OFP:getParentEchelon(victim)
if not OFP:isAlive(echelon) then
-- Get the setID and setName for the echelon
local setID = OFPName2ID[echelon]
local setName = OFPName2SetName[echelon]
-- Destroy the existing set
OFP:destroyEntitySet(setID)
-- Respawn the set in a random location.
local spawn_rand = math.random(1,#PLA_spawn_points);
local x,y,z = OFP:getPosition(PLA_spawn_points[spawn_rand]);
OFP:spawnEntitySetAtLocation(setName, x, y, z);
end
end
Edit Cross-posted with Templar again... I have to type faster
Spawning and Controlling Spawned Units TemplarGFX C:\Users\Aaron\AppData\Local\Temp\msohtmlclip1\01\clip_image001
__________________

In theory there is no difference between theory and practice. But in practice there is.
oANIMUSo
oANIMUSo

Location : Florida, U.S.
Points : 94
Reputation : 1
Join date : 2012-12-22
Age : 52

https://www.youtube.com/user/oANIMUSoTube?feature=mhee

Back to top Go down

Spawning and Controlling Spawned Units TemplarGFX Empty Re: Spawning and Controlling Spawned Units TemplarGFX

Post by oANIMUSo Sat 09 Aug 2014, 23:27

SiMpLE Jack
 
Quote:
is your intention to first have the unit move to a waypoint, then start patrolling a path once its reached it?
 
if so, you could go the MUCH simpler approach. in onSpawnedReady queue up 2 commands for the spawned team :
Code:
OFP:move(echelonName,move_waypoint, "OVERRIDE");
OFP:patrol(echelonName,patrol_point, 1, "EBACKANDFORTH", "ADDTOEND");
the unit will move to the first waypoint, and then start patrolling a path.
Yes that's exactly what I'm trying to do. I've got it working nicely using "OFP:teleport()" but I want to learn to use what you have posted here. I just got home and am about to try this out.
Thanks Templar and HS
__________________
randomness is next to godliness


Last edited by SiMpLE Jack; 25-11-2009 at 01:08 AM. 
oANIMUSo
oANIMUSo

Location : Florida, U.S.
Points : 94
Reputation : 1
Join date : 2012-12-22
Age : 52

https://www.youtube.com/user/oANIMUSoTube?feature=mhee

Back to top Go down

Spawning and Controlling Spawned Units TemplarGFX Empty Re: Spawning and Controlling Spawned Units TemplarGFX

Post by oANIMUSo Sat 09 Aug 2014, 23:28

TemplarGFX
 
I just wrote single points in there becuase I didnt know what you were trying to do.
 
for example, in my Urban War mission, I have my spawned units first move to one of 4 random points using the rapidMove command, then afterwords they move to a random of 9 points using the defendPos command.
 
that code looks like this :
Code:
function onSpawnedReady( setName, setID, tableOfEntities, errorCode )
unitName = tableOfEntities[2];
echelonName = tableOfEntities[1];
local point_rand = math.random(1,#defend_points);
if OFP:getSide(unitName) == 0 then
local rapid_rand = math.random(1,#US_rapid_points);
OFP:rapidMove(echelonName,US_rapid_points[rapid_rand],"OVERRIDE");
OFP:defendPos(echelonName,defend_points[point_rand],50,"ADDTOEND")
else
local rapid_rand = math.random(1,#PLA_rapid_points);
OFP:rapidMove(echelonName,PLA_rapid_points[rapid_rand],"OVERRIDE");
OFP:defendPos(echelonName,defend_points[point_rand],50,"ADDTOEND")
end
end
here I have code for PLA spawned units, and US spawned units. the defend points are the same, but the rapid move ones are different for each team.
 

hope that clears it up a little
oANIMUSo
oANIMUSo

Location : Florida, U.S.
Points : 94
Reputation : 1
Join date : 2012-12-22
Age : 52

https://www.youtube.com/user/oANIMUSoTube?feature=mhee

Back to top Go down

Spawning and Controlling Spawned Units TemplarGFX Empty Re: Spawning and Controlling Spawned Units TemplarGFX

Post by John J. Stevens Sun 10 Aug 2014, 20:21

see that you are finding that old knowledge base useful, know I saved it for a reason - this is the stuff we were originally creating a WIKI around until Saint burned it to the ground

Good to see you spreading the word.
John J. Stevens
John J. Stevens
Admin

Location : WV/PA
Points : 285
Reputation : 22
Join date : 2012-12-19

Back to top Go down

Spawning and Controlling Spawned Units TemplarGFX Empty Re: Spawning and Controlling Spawned Units TemplarGFX

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum