Jump to content

Strange multiple event handler behaviour


OneFatBird

Recommended Posts

Hello. As usual, apologies if this has been covered before somewhere else; search didn't give any clues...

 

This picture...

 

Land_Spawn.jpg

 

...is the result of using this event_handler script on one landing...

 

local Event_Handler = {}

function Event_Handler:onEvent(Event)
   if Event.id == world.event.S_EVENT_LAND then
       if Event.initiator then
           local Init_Name = Event.initiator:getName()
           if Init_Name == "BHGP-01" then
               if Event.place == nil then
                   local helo_pos = Event.initiator:getPosition().p
                   RPG_MAN_SPAWN =
                   {
                       ["visible"] = false,
                       ["groupId"] = math.random(999),
                       ["name"] = string.format("RPG_" .. math.random(10, 10000)),
                       ["hidden"] = false,
                       ["units"] = 
                       {
                           [1] = 
                           {
                               ["type"] = "Soldier RPG",
                               ["name"] = string.format("RPG_U_" .. math.random(10,10000)),
                               ["unitId"] = math.random(10,10000),
                               ["heading"] = 0,
                               ["playerCanDrive"] = false,
                               ["skill"] = "Excellent",
                               ["x"] = helo_pos.x + (math.random(40) - 20),
                               ["y"] = helo_pos.z + (math.random(40) - 20),
                           },
                       },
                   }
               end
               coalition.addGroup(country.id.USA, Group.Category.GROUND, RPG_MAN_SPAWN)
           end
       else
           trigger.action.outText(_Name .. " landed at " .. Event.place:getName(), 10)
       end
   end
end


world.addEventHandler(Event_Handler)

Basically, a single landing is generating a number of landing events for this UH-1, and spawning an RPG man for each of them. Now, there is a work around if I use flags for the spawning action, but the questions remain...

 

1. Is this right? Why are there multiple landing events registered? Is it normal?

 

2. What is the correct way to go about preventing this issue?

 

Thanks in advance!

 

PS: For info, I am triggering this off a Continuous Action, because I do not seem to get it to work off a Switched Action condition. However, I am wondering if I am not on the right track in considering the way it is invoked.


Edited by OneFatBird
Link to comment
Share on other sites

It may be how its triggered. When you add an event handler it is active until you remove it. So you only need to add it once. So what might be happening is that you are contentiously adding event handlers so when one event occurs it will run that function multiple times. Change it to once> Time More> do Script and see if that works.

  • Like 1

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

Link to comment
Share on other sites

That was it, thank you so much!

 

For some reason I thought ONCE would only do the script once for the whole game (ie, only ever deploy one soldier in the entire game), and didn't think of trying it. It now deploys one soldier every time I land the Huey, and I have learned the final key to getting the event handler to work.

Link to comment
Share on other sites

For some reason I thought ONCE would only do the script once for the whole game

 

Your thinking isn't exactly wrong.

In fact, your script will actually only run once!

 

What the trigger action (better to say the script) does is quite simple: it creates your Event_Handler (a variable containing a function) in the local space. After this, it executes world.addEventHandler, to register your custom event handler, with your custom function as a parameter.

No call to your custom function was made yet!

 

From then on it's all up to the cores event system to call your event handler. And this will be done on each possible event.

Your custom function will get called quite often (on each possible event), but your script itself will only run once/will only register the event handler once.

  • Like 1
- Deutsche Tutorials und DCS Gameplay: youtube.com/Rakuzard | raku.yt/discord -
Link to comment
Share on other sites

Hello.

...

 


function Event_Handler:onEvent(Event)

                               ["skill"] = "Excellent",

world.addEventHandler(Event_Handler)

 

 

Ooh! I like the Skill setting in this kind of script. (MANPAD Stingers never seem to hit anything...)

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

:) Regarding skill, yes; I do want those RPG guys to actually hit something before they get mowed down by some coax.

 

Your thinking isn't exactly wrong.

In fact, your script will actually only run once!

 

Thank you. I was beginning to get that idea, but you've put it better words than I would have done, for sure.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...