![]() |
|
|
||||||
| Operation Flashpoint: Dragon Rising - Mission Editing and Modding Chat Zone A little area for all you mission editors and modders to chat and discuss ideas. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
31-10-2009, 07:04 PM
|
#1 (permalink) |
|
Senior Member
Join Date: Jul 2009
Posts: 166
|
Hi,
i got this problem: I spawn one single unit and i want, when this specific unit gets killed, a function to be called. Now the problem: The units name gets randomized when i spawn it, so Code:
function onDeath () if victim ==unitname then function xxx () ; end end I alrdy put the unit in a single Entitygroup (=bmgrp) and tried this one: Code:
function onDeath ()
if victim ==unitnamevar then function xxx () ; end
end
function abc
OFP:spawnEntityAtLocation (abc)
unitnamevar = OFP:getGroupMember ("bmgrp", 1);
end
Any ideas?
__________________
Defend youself against a horde of enemies - waver after wave: Download and Vote for me http://www.intelofpdrcontest.com/de/...ndyinte...-19/ |
|
|
31-10-2009, 07:19 PM
|
#3 (permalink) |
|
Senior Member
Join Date: Jul 2009
Posts: 166
|
^^ well then i hope someone got an idea about this one!
__________________
Defend youself against a horde of enemies - waver after wave: Download and Vote for me http://www.intelofpdrcontest.com/de/...ndyinte...-19/ |
|
|
01-11-2009, 02:02 AM
|
#6 (permalink) |
|
Senior Member
Join Date: Jul 2009
Posts: 166
|
@paingiver....hm should work but i would be interested in getting the name and then solve it via "victim"-query. But i think this is the only way you described it (just solved it alrdy this way you told me, but its not the best....i thought). Becuase there are some situation you need exactly who got killed....well i think i have to be satisfied with this solution. Thx
![]() @TheGentle: It seem to me, that the names you gave the units in a Entityset get randomzied when they spawn. So its even possible to spawn the same Entityset more than once at the same time. Thats what i ment and that should be the reason why the victim == unitname wont work anymore.
__________________
Defend youself against a horde of enemies - waver after wave: Download and Vote for me http://www.intelofpdrcontest.com/de/...ndyinte...-19/ |
|
|
01-11-2009, 04:23 PM
|
#8 (permalink) |
|
Senior Member
Join Date: Jul 2009
Posts: 166
|
@Adam: ^^ well and that's exactly NOT working when you spawn an unit (--> its name gets randomized by the engine!)
__________________
Defend youself against a horde of enemies - waver after wave: Download and Vote for me http://www.intelofpdrcontest.com/de/...ndyinte...-19/ |
|
|
01-11-2009, 06:04 PM
|
#9 (permalink) |
|
Senior Member
Join Date: Oct 2009
Posts: 300
|
I still couldnt work it out, but I eventualy got it working another way by just using teleport, clearCommandQueue, and activateEntitySet instead, then his name will stay the same. But im still working on trying to get it to return the units actual name after he has been spawned not activated.
|
|
|
02-11-2009, 04:49 PM
|
#10 (permalink) |
|
Senior Member
Join Date: Oct 2009
Location: New York
Posts: 462
|
This is ripped from a mission I am working on so may need some tweaking.
To respawn units that were originally spawned with spawnEntitySet or spawnEntitySetAtLocation requires that we need to do some bookkeeping ourselves. In particular, we need to be able to "find" the entity set based on the ID assigned by OFP. To do this we use a "unit table" that holds the unit name, entity set name, group name, ID, and a few other values (where the unit should spawn and where the unit should be ordered to move to after spawning). I have a function that initializes the unit table. Code:
-- Function used when defining the unit tables. This ensures
-- all unit tables are constructed exactly the same way.
function makeUnit(name, home, target)
return {
set = "s" .. name,
name = "e" .. name,
group = "g" .. name,
id = nil,
target = target,
home = home,
entities = nil
}
end
-- An AI unit that will spawn at the red spawn point and attack the
-- "BlueBase" by default (until something changes its target, then
-- it will attack that target).
AIUnit = makeUnit("red1", "RedSpawnPoint", "BlueBase")
Then we need a couple of tables so we can look up a unit's ID value based on the name that OFP generated and another table that we can use to look up the "unit table" based on the ID value. Code:
-- Table used to determine the unit from the OFP generated entity ID.
Id2Unit = {}
-- Table used to determine the entity id from the random name generated
-- for the entity by OFP when it was spawned.
Name2Id = {}
Code:
function onMissionStart()
spawn(AIUnit)
...
end
-- Uses the info in the unit table to spawn the unit at its spawn point.
function spawn(unit)
show("Spawning " .. unit.set)
local x, y, z = OFP:getPosition(unit.home)
unit.id = OFP:spawnEntitySetAtLocation(unit.set, x, y, z)
-- Record the id assiged to this unit so we can find it again
-- given its id.
Id2Unit[unit.id] = unit
end
-- Called by OFP when a unit has spawned and is ready to
-- receive orders.
function onSpawnedReady(name, id, tableOfEntities, code)
-- Get the unit for this entity id.
unit = Id2Unit[id]
if unit == nil then
log("Could not find unit for id " .. id)
else
-- Record the random names generated by OFP and associate
-- them with their entity set id.
for ignored,entity in pairs(t) do
Name2Id[entity] = id
end
-- Order the unit into action
OFP:move(unit.group, unit.target, "OVERRIDE")
OFP:defendPerimeter(unit.group, "ADDTOEND")
end
end
Code:
-- Given a name randomly generated by OFP, find the named
-- entity's unit. Returns nil if the unit can't be found.
-- Otherwise returns the unit table for the entity.
function getUnit(name)
-- Find the echelon the entity belongs to.
local parent = OFP:getParentEchelon(name)
if parent == nil then
log("Could not get parent echelon")
return nil
end
-- Find the entity id that was assigned to that
-- echelon when it spawned.
local unitId = Name2Id[parent]
if unitId == nil then
log("unit id is nil")
return nil
end
-- Find the unit associated with the id.
local unit = Id2Unit[unitId]
if unit == nil then
log("unit is nil")
return nil
end
-- Else return the unit.
return unit
end
function onDeath(victim, killer)
local unit = getUnit(victim)
if unit == nil then
log("Unable to locate unit for victim " .. victim)
return
end
if not OFP:isAlive(unit.group) then
spawn(unit)
end
end
Hope it helps.
__________________
In theory there is no difference between theory and practice. But in practice there is. Intro to LUA, Fun with tables, Undocumented Functions, Saving Game State Timer library, Sector Control Framework Finite State Machines (part 1) |
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|