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.

Group Kill Trigger

4 posters

Go down

Group Kill Trigger Empty Group Kill Trigger

Post by RookieCop Tue 21 Jul 2015, 10:27

Hi guys! I'm new in this forum and was having a problem on finding a way to make a trigger when groups of unit are killed.

I created a new mission with 2 groups. I added them to "grp1" and "grp2". and i tried this :

 function onDeath(victim,killer)
if OFP:isAlive("grp1") == false then
OFP:showPopup("Test","Worked")
elseif OFP:isAlive("grp2") == false then
OFP:showPopup("Test 2","Worked")
end
end

this doesn't work.

Before i tried this, i had tried another way using many triggerzones, so every time a unit come in or out the triggerzones, it will always check if the enemy unit is dead or not.

triggerzone ex:

 function onEnter_triggerzone()
if OFP:isAlive("grp1") == false then
event1()
end
end

 function onLeave_triggerzone()
if OFP:isAlive("grp1") == false then
event1()
end
end

 function event1()
OFP:showPopup("Test","Worked")
OFP:disableEvent("onEnter_triggerzone")
OFP:disableEvent("onLeave_triggerzone")
end

....and so on.
The thing is, i want to check the alive condition on their deaths directly, not from entering/leaving triggerzone.


Last edited by RookieCop on Thu 23 Jul 2015, 04:26; edited 6 times in total

RookieCop

Points : 15
Reputation : 1
Join date : 2015-07-10

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by Zeewolf Wed 22 Jul 2015, 20:37

Well I just tried something similar and it worked fine:

Code:
function onDeath(victim, killer)
   if(OFP:isAlive("grp1")) then
      OFP:displaySystemMessage("Alive")
   else
      OFP:displaySystemMessage("Dead")
   end
end

Returned "Dead" when I killed the last unit in grp1. I suggest you add some debug code to your onDeath function to verify that the unit is in the group you think it is (I think that that's the issue), you can do this by calling OFP:isInGroup(unit,group) and passing the victim. Note that you can concatenate strings and output the contents of variables in LUA using

Code:
"My variable is "..variableName

So you could put in something like:

Code:
OFP:displaySystemMessage("Victim is "..victim)


 this would allow you to view the victim's name for example.

Zeewolf

Points : 42
Reputation : 13
Join date : 2013-10-02

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by RookieCop Thu 23 Jul 2015, 04:25

Thanks Zeewolf, i'll try it later. But how do you do it for 2 groups? 

 function onDeath(victim,killer)
if OFP:isAlive("grp1") == false then
OFP:showPopup("Test","Worked")
elseif OFP:isAlive("grp2") == false then
OFP:showPopup("Test 2","Worked")
end

This would work if i delete the "elseif" and only want to check 1 group
The issue is how you check every different group's death?

RookieCop

Points : 15
Reputation : 1
Join date : 2015-07-10

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by TheGeneral Thu 23 Jul 2015, 11:26

Maybe try putting all the groups into the array and set them with a variable???? 

Something is floating in my brain from code i've seen before that tells me a table of your groups using a variable would work. Then when the function is called the table will be search to see which of the groups is alive or dead.

Not done any code for DR in a long while so i'm pretty hazey, not yhat i was any good to begin with.
TheGeneral
TheGeneral
Admin

Points : 780
Reputation : 35
Join date : 2012-12-15

http://maniacaldog.wix.com/keepdralive

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by RookieCop Thu 23 Jul 2015, 19:18

Mdog wrote:Maybe try putting all the groups into the array and set them with a variable???? 
 Do you mean by putting all the groups into the array is to put all the groups in one function?

RookieCop

Points : 15
Reputation : 1
Join date : 2015-07-10

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by Zeewolf Thu 23 Jul 2015, 19:46

As Mdog says LUA tables are the way to go really. It's quite simple if each unit is only a member of a single group because you can set up a one dimensional table with a hash key, value pairing to retrieve a unit's group name efficiently.

Suppose I have three units called us1, us2 and us3. us1 and us2 are in group grp1, us3 is in grp2 I could use a table indexed by unit name to store their group which could then be referenced in the onDeath function to call isAlive:

Code:
function onMissionStart()
      myGroupTable = {}
      myGroupTable["us1"] = "grp1"
      myGroupTable["us2"] = "grp1"
      myGroupTable["us3"] = "grp2"
end
function onDeath(victim, killer)
      if(not OFP:isAlive(myGroupTable[victim])) then
              OFP:displaySystemMessage("Group "..myGroupTable[victim].." is dead!")
      end
end

Note that the key value pairing is case sensitive and that the game will force unit names to lower case, so it's best to stick to lower case to avoid confusion.

Things get tricky if units are members of multiple groups since then you would need a "table of tables", i.e. each element of myGroup table would itself need to be a table of all the groups the unit belongs to, you would then need to iterate over each of these to check them.

Things also get complicated if you are spawning/despawning entity sets since you then need to start retrieving their entity names when they spawn (since new names are generated on the fly when they spawn and aren't related to their original name in the editor), you would then have to update the group table in the OnSpawnedReady eventhandler.

You might think that the OFP:isInGroup function would be useful here, but it isn't since units are removed from groups as soon as they die, so checking if a victim is in a given group will always return false.

I suggest you check out some tutorials on LUA tables as they are pretty much essential for managing non-trivial scripts in DR.

Zeewolf

Points : 42
Reputation : 13
Join date : 2013-10-02

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by RookieCop Thu 23 Jul 2015, 20:02

Where can i find LUA tables tutorial? A quick link might be useful.

RookieCop

Points : 15
Reputation : 1
Join date : 2015-07-10

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by Zeewolf Thu 23 Jul 2015, 20:04

I like this one myself

Zeewolf

Points : 42
Reputation : 13
Join date : 2013-10-02

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by RookieCop Thu 23 Jul 2015, 20:08

Nice! Thanks Zeewolf.

RookieCop

Points : 15
Reputation : 1
Join date : 2015-07-10

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by RookieCop Fri 24 Jul 2015, 09:10

I still can't figure it out. Zeewolf, i tried your table above 

function onMissionStart()
       myGroupTable = {}
       myGroupTable["us1"] = "grp1"
       myGroupTable["us2"] = "grp1"
       myGroupTable["us3"] = "grp2"
end
function onDeath(victim, killer)
      if(not OFP:isAlive(myGroupTable[victim])) then
              OFP:displaySystemMessage("Group "..myGroupTable[victim].." is dead!")
      end
end

 I tested it and the result is not like what i mean. It triggers the message every unit i killed
What i'm trying to do is to trigger something when the group is killed.
What i mean is, when group 1 is killed, an event happens, when group 2 is killed, another event happens.
Or maybe I still didn't get how tables work?


Last edited by RookieCop on Fri 24 Jul 2015, 16:25; edited 1 time in total

RookieCop

Points : 15
Reputation : 1
Join date : 2015-07-10

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by RookieCop Fri 24 Jul 2015, 14:08

I think i found what i need. Just like what Mdog said, use variables.

Code:
death1 = 0;
death2 = 0;
death3 = 0;
death4 = 0;
enspawn = 0;

function onDeath(victim, killer)
if (not OFP:isAlive("grp1")) and OFP:wasInGroup("grp1","grp1") then
death1 = death1 + 1;
event1()
end
if(not OFP:isAlive("grp2")) and OFP:wasInGroup("grp2","grp2") then
death2 = death2 + 1;
event2()
end
if (not OFP:isAlive("grp3")) and OFP:wasInGroup("grp3","grp3") then
death3 = death3 + 1;
event3()
end
if (not OFP:isAlive("grp4")) and OFP:wasInGroup("grp4","grp4") then
death4 = death4 + 1;
event4()
end
end

function event1()
if death1 == 1 then
OFP:displaySystemMessage("grp1 dead")
end
end

function event2()
if death2 == 1 then
OFP:displaySystemMessage("grp2 dead")
end
end

function event3()
if death3 == 1 then
OFP:displaySystemMessage("grp3 dead")
end
end

function event4()
if death4 == 1 then
OFP:displaySystemMessage("grp4 dead")
end
end

function onEnter_triggerzone(zoneName, unitName)
if unitName == "iu11usgren" then
enspawn = OFP:activateEntitySet("spawns")
end
end
(Edited)



But i have another issue, how do i do this if the group i want to check is not spawn from the start (which mean the alive condition will call it as false/dead)? Can i use 2 the same onDeath(Group tables?)? or maybe there is a way to disable an "if" condition?


Last edited by RookieCop on Sat 25 Jul 2015, 02:41; edited 5 times in total

RookieCop

Points : 15
Reputation : 1
Join date : 2015-07-10

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by Zeewolf Fri 24 Jul 2015, 18:19

Wow, that's some ugly code, you know you can just use
if not OFP:isAlive("grp1") then

Rather than continually using == false all the time. I don't know why you're putting "return" in there either you can't return values from the eventhandler.

Anyway, the onDeath() eventhandler will only be triggered when something is killed, not being present at the start of the mission (due to being in an entity set other than startup) doesn't count. Spawned entities remain in the groups they were assigned in the mission editor, if they die they will be removed from the group but if that set is then respawned the replacement unit will be in the group again.

Zeewolf

Points : 42
Reputation : 13
Join date : 2013-10-02

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by RookieCop Sat 25 Jul 2015, 01:34

Well, i'm not an experienced coder and sometimes i put random things to see how things work (that's why i put return). Haha, some ugly code. Using == false became a habit since the time I learned from someone's tutorial. I'll try not to do that again.

But everything worked out well now after i used wasInGroup condition. Thanks for the help Zeewolf, Mdog.

RookieCop

Points : 15
Reputation : 1
Join date : 2015-07-10

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by tvig0r0us Sat 25 Jul 2015, 11:42

You guys are way overthinking this.

Code:

if not OFP:isAlive("grp1") and not OFP:isAlive("grp2") and (OFP:wasInGroup(victim,"grp1") or OFP:wasInGroup(victim,"grp2")) then --If both groups are dead and the victim was in one of them
  --do something
end


All you are doing is checking to see if both groups are dead.

If you need to check to see if one or the other is dead do this...

Code:

dead = {}; -- set this in the onMissionStart
if not OFP:isAlive("grp1") and not dead["grp1"] and OFP:wasInGroup(victim,"grp1") then --This checks if is alive and not already accounted for
  --do something
  dead["grp1"]=true; --this just creates an index in the dead table to suppress the above evaluation
end
if not OFP:isAlive("grp2") and not dead["grp2"] and OFP:wasInGroup(victim,"grp2") then
  --do something else
  dead["grp2"]=true;
end 

That one just triggers the code blocks once when the whole group is dead. Then it sets up and index in the dead table that is used to keep the code from executing more than once.

--Cheers
tvig0r0us
tvig0r0us
Admin

Location : Nky
Points : 266
Reputation : 29
Join date : 2013-10-16

http://www.moddb.com/members/tvig0r0us

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by RookieCop Sat 25 Jul 2015, 13:01

It worked, but i still don't understand it. tvig0r0us can you explain why you put not dead["grp1"] ? Does it check that grp1 is not in dead ={} / false, or what?

RookieCop

Points : 15
Reputation : 1
Join date : 2015-07-10

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by tvig0r0us Sat 25 Jul 2015, 13:19

RookieCop wrote:It worked, but i still don't understand it. tvig0r0us can you explain why you put not dead["grp1"] ? Does it check that grp1 is not in dead ={} / false, or what?

That is exactly right. In lua, a nil value means something does not exist. In this case, it will evaluate false when used in an if...then evaluation. So in the onMissionStart, we set an empty array named 'dead'. When we check for dead['grp1'] or dead['grp2'], we are looking to see if there is a value that will evaluate true there. So in saying not dead['grp#'], we are evaluating whether the index 'grp#' does not exist in the array. Combine that with not OFP:isAlive() to check if the group has any living members, and OFP:wasInGroup() to see if the current victim was in the group in question and you evaluate all of those conditions in one line of code. Is the group alive? Was the victim in the group? Have we already determined that the group is dead? Those are the three questions you evaluate. If all three conditions are met, you execute your code inside of the block, and then(very important) you set dead['grp#'] to true. Then the next time the on death fires it assures you that that block of code will not fire again because not dead['grp#'] will test false... or not true.

I threw the array in extra so you could see how they can be used to evaluate the status of different events. In your example, just checking to see if the victim was in the group in question will do just fine because once the group is dead, there is no chance that you will kill another member of that group. The array example can be used in cases that exist outside of that type of situation or for anything you want to test once.

Hopefully that's not too confusing.

Cheers Very Happy
--tvig
tvig0r0us
tvig0r0us
Admin

Location : Nky
Points : 266
Reputation : 29
Join date : 2013-10-16

http://www.moddb.com/members/tvig0r0us

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by Zeewolf Sat 25 Jul 2015, 13:41

tvig0r0us wrote:You guys are way overthinking this.

Sorry I completely disagree. How am I "overthinking this" by encouraging someone to write code that is more scalable and efficient? Every time your onDeath function is being called you are having to evaluate an if statement for every single group in the game, this is unnecessary and inefficient, especially when the condition is non-trivial (i.e. consists of several calls to library functions). Here's another example:

Code:
function onMissionStart()
 groupLookup = {}
 groupLookup["us1"] = "grp1"
 groupLookup["us2"] = "grp1"
 groupLookup["us3"] = "grp2"
 groupStatus = {}
 groupStatus["grp1"] = {true,grp1Dead}
 groupStatus["grp2"] = {true,grp2Dead}
end
function onDeath(victim, killer)
 local grp = groupLookup[victim]
 if(not OFP:isAlive(grp)) then
 if(groupStatus[grp][1]) then
 groupStatus[grp][1] = false
 groupStatus[grp][2]()
 end
 end
end
function grp1Dead()
 OFP:displaySystemMessage("Group 1 dead")
end
function grp2Dead()
 OFP:displaySystemMessage("Group 2 dead")
end

If a unit is a member of more than one group, then groupLookup will have to become two dimensional to hold all the groups that a unit is a member of. This would then be iterated over, to check each group's status.

I believe programmers should be encouraged to think about the efficiency of their code where ever possible, while the majority of the time the performance impact will be negligible, one day you may find yourself in a situation where it isn't.

Zeewolf

Points : 42
Reputation : 13
Join date : 2013-10-02

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by tvig0r0us Sat 25 Jul 2015, 13:53

Only in the second example. He is trying to evaluate for 2 groups, not every group in the game. I'm trying to suggest the most direct route for someone that has not got a lot of experience with code. Thanks for putting me in my place though.

--tvig
tvig0r0us
tvig0r0us
Admin

Location : Nky
Points : 266
Reputation : 29
Join date : 2013-10-16

http://www.moddb.com/members/tvig0r0us

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by TheGeneral Sat 25 Jul 2015, 14:13

Lets avoid the sniedy comments please! This forum has has been quiet for a while, its nice to see constructive examples and help coming back in. Don't you think it would be nice to welcome all points of view with objective sentiment rather than poke negative sticks. 

In other words, watch the tone please! Or i'll lock the thread.

 Please continue without the digs. Thanks!
TheGeneral
TheGeneral
Admin

Points : 780
Reputation : 35
Join date : 2012-12-15

http://maniacaldog.wix.com/keepdralive

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by RookieCop Sat 25 Jul 2015, 14:32

I think I understand. Thanks for the explanation tvig0r0us. 
Thanks for the examples Zeewolf, I'll try to comprehend it.

RookieCop

Points : 15
Reputation : 1
Join date : 2015-07-10

Back to top Go down

Group Kill Trigger Empty Re: Group Kill Trigger

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


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