Go Back   Codemasters Forums > Action, RPG and Strategy Games > Operation Flashpoint: Dragon Rising > English > Operation Flashpoint: Dragon Rising - Mission Editing and Modding Chat Zone
Sign In
Register on CodeM

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.

Reply
 
LinkBack Thread Tools Display Modes
Old 31-10-2009, 07:04 PM   #1 (permalink)
Senior Member
 
Join Date: Jul 2009
Posts: 166
Default OnDeath - Victim

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
doesnt work anymore.
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
Which is working neither.

Any ideas?
__________________
Defend youself against a horde of enemies - waver after wave:

Download and Vote for me http://www.intelofpdrcontest.com/de/...ndyinte...-19/
Lonesome Wulf is offline   Reply With Quote
Old 31-10-2009, 07:06 PM   #2 (permalink)
Senior Member
 
Join Date: Oct 2009
Posts: 300
Default

same question baisicaly that i just posted too

because i want one guy that has been spawned by spawnEntitySetAtLocation to be able to set an objective complete when he dies.
RIKKI_B is offline   Reply With Quote
Old 31-10-2009, 07:19 PM   #3 (permalink)
Senior Member
 
Join Date: Jul 2009
Posts: 166
Default

^^ 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/
Lonesome Wulf is offline   Reply With Quote
Old 31-10-2009, 08:05 PM   #4 (permalink)
Member
 
Paingiver's Avatar
 
Join Date: Oct 2009
Posts: 51
Default

Put the single unit into a group called 'alone' or whatever then...

Code:
function onDeath(victim,killer)
     if OFP:isAlive("alone") == false then
        do some stuff
     end
end
Paingiver is offline   Reply With Quote
Old 31-10-2009, 08:22 PM   #5 (permalink)
Member
 
Join Date: Sep 2009
Posts: 33
Default

Quote:
Now the problem: The units name gets randomized when i spawn it, so
What do you mean with this?
Something like:
Code:
unitname[var]
var = math.random(x,y)
or something totally different?
TheGentleOne is offline   Reply With Quote
Old 01-11-2009, 02:02 AM   #6 (permalink)
Senior Member
 
Join Date: Jul 2009
Posts: 166
Default

@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/
Lonesome Wulf is offline   Reply With Quote
Old 01-11-2009, 05:48 AM   #7 (permalink)
Member
 
Join Date: Oct 2009
Posts: 34
Default

if its just for one specific unit use:

function onDeath_unitname(victim, killer)
OFP: etc
Adamdf is offline   Reply With Quote
Old 01-11-2009, 04:23 PM   #8 (permalink)
Senior Member
 
Join Date: Jul 2009
Posts: 166
Default

@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/
Lonesome Wulf is offline   Reply With Quote
Old 01-11-2009, 06:04 PM   #9 (permalink)
Senior Member
 
Join Date: Oct 2009
Posts: 300
Default

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.
RIKKI_B is offline   Reply With Quote
Old 02-11-2009, 04:49 PM   #10 (permalink)
Senior Member
 
Join Date: Oct 2009
Location: New York
Posts: 462
Default

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")
I use the following naming convention for entity sets, echelon names and group names. If a unit is named "red1" (say) then its echelon will be named "ered1", its entity set will be "sred1" and it will be assigned to a group "gred1".

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 = {}
I have a function named "spawn" that uses the information from the unit table to spawn the unit.
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
In the onDeath event we find the victim's unit table, check if the group is alive, and if not respawn the unit. (In reality there is a delay before the unit respawns, but including that code makes this "simple" example more complicated).
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
This code works perfectly... in my mission. I've just cut and pasted it here and I don't know if I got all the relevant bits or made any typos in the bits I had to change to have it make sense out of context.

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)
Haywood Slap is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 07:39 PM.


Powered by vBulletin®
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.