Jump to content

Script to Make Flag Value Increase by 1


Recommended Posts

Hi All!

 

OK, this is what I"m trying to do.... In the mission I'm making, my goal is to have a bunch of different "Fox" calls play as a sound to coalition every time one of the aircraft in a flight launches a missile.

 

For my thinking ( a LUA no-nothing), I was thinking that each time a member of a flight fires a missile, it would increase a flag value by one (example, first fire sets flag 10 to 1. Second fire sets flag 10 to 2, etc...)

 

So, I managed to cobble together a bit of a script based on the event hander SHOT below:

 

 

Handler = {}

function Handler:onEvent(event)

if event.id == world.event.S_EVENT_SHOT and event.initiator == Unit.getByName('UZI 11') or Unit.getByName('UZI 12') or Unit.getByName('UZI 13') or Unit.getByName('UZI 14') then

trigger.action.setUserFlag('10', true)

trigger.action.outText('Fox!', 3)

end

end

world.addEventHandler(Handler)

 

 

So, this works to set FLAG 10 to on each time a member of UZI 1 flight fires a missile. Good stuff.

 

Where I run out of ideas is when I want to increment the flag value by 1 each time a missile is fired. Below is what I came up with:

 

 

Handler = {}

flag_value = 1

function Handler:onEvent(event)

if event.id == world.event.S_EVENT_SHOT and event.initiator == Unit.getByName('UZI 11') or Unit.getByName('UZI 12') or Unit.getByName('UZI 13') or Unit.getByName('UZI 14') then

trigger.action.setUserFlag('10', flag_value + 1)

trigger.action.outText('Fox!', 3)

end

end

world.addEventHandler(Handler)

 

 

The above works, kind of... It will set the value of the flag to 2 (flag_value + 1 = 2). But it won't increase above that with each successive missile shot. I think the issue is that the 'flag_value' above needs to reference the actual status of Flag 10, but I cannot think of how to code that. I think it's somewhere along the lines of:

 

 

Handler = {}

flag_value = trigger.misc.getUserFlag('10')

function Handler:onEvent(event)

if event.id == world.event.S_EVENT_SHOT and event.initiator == Unit.getByName('UZI 11') or Unit.getByName('UZI 12') or Unit.getByName('UZI 13') or Unit.getByName('UZI 14') then

trigger.action.setUserFlag('10', flag_value + 1)

trigger.action.outText('Fox!', 3)

end

end

world.addEventHandler(Handler)

 

 

but it will only add a value to the flag once ( the first time a missile is fired, not any subsequent ones).

 

Can anyone point me in the right (or better direction)?

 

Thanks in advance!

Link to comment
Share on other sites

OK, disregard, I think I may have gotten it. I had to move the "flag_value = trigger.misc.getUserFlag('10')" line BELOW the "event.initiator" line... It works now like a charm (I think)

 

 

Handler = {}

function Handler:onEvent(event)

if event.id == world.event.S_EVENT_SHOT and event.initiator == Unit.getByName('UZI 11') or Unit.getByName('UZI 12') or Unit.getByName('UZI 13') or Unit.getByName('UZI 14') then

flag_value = trigger.misc.getUserFlag('10')

trigger.action.setUserFlag('10', flag_value + 1)

trigger.action.outText('Fox!', 3)

end

end

world.addEventHandler(Handler)

Link to comment
Share on other sites

I think there's still a problem with your if statement.

 

if event.id == world.event.S_EVENT_SHOT and event.initiator == Unit.getByName('UZI 11')[color="Red"] or Unit.getByName('UZI 12') or Unit.getByName('UZI 13') or Unit.getByName('UZI 14') [/color]then

 

As it's written, your statement will check for the following:

 

- Whether it's a SHOT event

and

- Whether the initiator is UZI 11

or

- Whether UZI 12 is found in database

or

- Whether UZI 13 is found in database

or

- Whether UZI 14 is found in database

 

 

If you want to check whether the initiator is UZI 11,12,13 or 14, you must write the relational condition each time:

if event.initiator == Unit.getByName('UZI 11') or event.initiator == Unit.getByName('UZI 12') or event.initiator == Unit.getByName('UZI 13') or event.initiator == Unit.getByName('UZI 14') then

 

Sure, having to write the same thing over and over is kind of annoying and dumb, but that's how Lua works.

 

Alternatively, you can simply create a table of initiators (or unit names) and then check if the initiator of the SHOT event is in it.

 

 

Also, why don't you make the script play a different sound file depending on missile type? Like Fox 1/ Fox 2 / Fox 3! That would be cooler ;)

(Btw, you know that the "fox number" doesn't represent the quantity of missiles fired, but the missile type, right?)

 

Fox 1 = Semi-active missile

Fox 2 = Heat seeker missile

Fox 3 = Active missile

 

It's actually quite easy to do, you just need to know the missile type enumerators (which can be gathered from a simple test mission), then classify them according to fox type.


Edited by Hardcard
Link to comment
Share on other sites

Hey Hardcard!

 

Thanks, good catch on the if statement, thanks!

 

The biggest hurdle I was having was trying to get the flag to increase value by 1 each time a particular missile was launched, so I'd have one flag for Fox 3, one for Fox 2, etc....

 

The goal is to have a physically different "Fox-3" audio call for each time an AMRAAM is fired. I kind of figured the best way to do it was have a flag increase by 1, then in the editor have a Flag 10 = 1 play Fox3a.ogg, Flag 10 = 2 play Fox3b.ogg, etc...

 

Do you think there might be a more elegant solution for this?

 

Below is the code that should do the trick to increase the flag depending on which type of missile is fired:

 

--FOX 3 SCRIPT

 

 

Handler = {}

function Handler:onEvent(event)

if event.initiator == Unit.getByName('UZI 11') or event.initiator == Unit.getByName('UZI 12') or event.initiator == Unit.getByName('UZI 13') or event.initiator == Unit.getByName('UZI 14') then

_weapon = event.weapon:getDesc()

_weapon_category =_weapon.missileCategory

_weapon_guidance = _weapon.guidance

if _weapon_category ==1 then

if _weapon_guidance == 3 then

flag_value = trigger.misc.getUserFlag('10')

trigger.action.setUserFlag('10', flag_value + 1)

trigger.action.outText('Fox 3!', 3)

end

end

end

end

world.addEventHandler(Handler)

 

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 

--FOX 2 SCRIPT

 

Handler = {}

function Handler:onEvent(event)

if event.initiator == Unit.getByName('UZI 11') or event.initiator == Unit.getByName('UZI 12') or event.initiator == Unit.getByName('UZI 13') or event.initiator == Unit.getByName('UZI 14') then

_weapon = event.weapon:getDesc()

_weapon_category =_weapon.missileCategory

_weapon_guidance = _weapon.guidance

if _weapon_category ==1 then

if _weapon_guidance == 2 then

flag_value = trigger.misc.getUserFlag('20')

trigger.action.setUserFlag('20', flag_value + 1)

trigger.action.outText('Fox 2!', 3)

end

end

end

end

world.addEventHandler(Handler)

 

Link to comment
Share on other sites

The goal is to have a physically different "Fox-3" audio call for each time an AMRAAM is fired. I kind of figured the best way to do it was have a flag increase by 1, then in the editor have a Flag 10 = 1 play Fox3a.ogg, Flag 10 = 2 play Fox3b.ogg, etc...

 

Do you think there might be a more elegant solution for this?

 

So you have 4 pilots with different voices, is that it?

 

If that's the case, then I'd use the initiator group/unit names rather than flags to assign individual sound files. You could also add an extra layer of variation by recording several fox calls for each pilot.

 

If that's not the case, then I guess you have a "common pool" of fox calls and you simply want to randomize things a bit.

 

Either way, I'd work with tables, for loops and randomizers instead of flags.

 

I can provide script examples later, if you're interested.

Link to comment
Share on other sites

Hey Hardcard!

 

I was thinking to use just random voices on each missile shot, as I was intending it to be background/ immersion related, but if there is a was to tie the voice overs to the individual aircraft that might be neat.

 

Yes, I'd love to see some examples if you've got the chance.

 

Thank you!

 

So you have 4 pilots with different voices, is that it?

Either way, I'd work with tables, for loops and randomizers instead of flags.

 

I can provide script examples later, if you're interested.

Link to comment
Share on other sites

@Sedlo

 

The key is to build the audio file table according to fox type and unit.

 

Script + demo mission below

 

   local Foxcall_Table = {
                          Fox_1 = { 
                                    ['UZI 11'] = { "Fox1_big_1.ogg" , "Fox1_big_2.ogg" , "Fox1_big_3.ogg" } , [color="Blue"]-- 3 different fox1 calls for pilot 1[/color]
                                    ['UZI 12'] = { "Fox1_normal_1.ogg" , "Fox1_normal_2.ogg" , "Fox1_normal_3.ogg" } , [color="blue"]-- 3 different fox1 calls for pilot 2[/color]
                                    ['UZI 13'] = { "Fox1_tiny_1.ogg" , "Fox1_tiny_2.ogg" , "Fox1_tiny_3.ogg" } , [color="blue"]-- 3 different fox1 calls for pilot 3[/color]
                                  },
                                   
                          Fox_2 = { 
                                    ['UZI 11'] = { "Fox2_big_1.ogg" , "Fox2_big_2.ogg" , "Fox2_big_3.ogg" } , [color="blue"]-- 3 different fox2 calls for pilot 1[/color]
                                    ['UZI 12'] = { "Fox2_normal_1.ogg" , "Fox2_normal_2.ogg" , "Fox2_normal_3.ogg" } , [color="blue"]-- 3 different fox2 calls for pilot 2[/color]
                                    ['UZI 13'] = { "Fox2_tiny_1.ogg" , "Fox2_tiny_2.ogg" , "Fox2_tiny_3.ogg" } , [color="blue"]-- 3 different fox2 calls for pilot 3[/color]
                                  },
                                   
                          Fox_3 = { 
                                    ['UZI 11'] = { "Fox3_big_1.ogg" , "Fox3_big_2.ogg" , "Fox3_big_3.ogg" } , [color="blue"]-- 3 different fox3 calls for pilot 1[/color]
                                    ['UZI 12'] = { "Fox3_normal_1.ogg" , "Fox3_normal_2.ogg" , "Fox3_normal_3.ogg" } , [color="blue"]-- 3 different fox3 calls for pilot 2[/color]
                                    ['UZI 13'] = { "Fox3_tiny_1.ogg" , "Fox3_tiny_2.ogg" , "Fox3_tiny_3.ogg" } , [color="blue"]-- 3 different fox3 calls for pilot 3[/color]
                                  }                        
                        }

local Handler = {}

function Handler:onEvent(event)
  
  if event.id == world.event.S_EVENT_SHOT and Foxcall_Table.Fox_1[event.initiator:getName()] then
     
     if event.weapon and event.weapon:getTypeName() and event.weapon:getDesc() and event.weapon:getDesc().guidance then
        
        local Initiator_Name = event.initiator:getName()
        local Initiator_Coalition = event.initiator:getCoalition()
        
        local _weapon = event.weapon:getDesc()
        local _weapon_guidance = _weapon.guidance
        local _weapon_type = event.weapon:getTypeName()
        
        if _weapon_guidance == 4 then  [color="Blue"]-- Guidance type 4 is fox 1[/color]
           
           trigger.action.outSoundForCoalition( Initiator_Coalition , Foxcall_Table.Fox_1[initiator_Name][math.random(1,3)] ) 
        
        elseif _weapon_guidance == 3 then  [color="Blue"]-- Guidance type 3 is fox 3[/color]
           
           trigger.action.outSoundForCoalition( Initiator_Coalition , Foxcall_Table.Fox_3[initiator_Name][math.random(1,3)] ) 
        
        elseif _weapon_guidance == 2 then  [color="Blue"]-- Guidance type 2 is fox 2[/color]
           
           trigger.action.outSoundForCoalition( Initiator_Coalition , Foxcall_Table.Fox_2[initiator_Name][math.random(1,3)] ) 
        end
        
        trigger.action.outText(event.initiator:getName().." launched ".._weapon_type, 10)
     end
  end
end

world.addEventHandler(Handler)

Fox Detection Test.miz

Link to comment
Share on other sites

  • 9 months later...

Hi All, AND thanks Hardcard!!!

 

I want to add in your script the player name that shot, and

I am trying to put this:

 

trigger.action.outText(Unit.getByName("UZI 11"):getPlayerName()..

 

But, I can't solve the right way...

 

please help!

[sIGPIC][/sIGPIC]

Intel(R) Core(TM) i9-10900KF CPU @ 3.70GHz   3.70 GHz ROG STRIX Z490-E GAMING
RAM 128 M.2*2 2T total SSD*3 2.5T total
GeForce RTX 3090   Orion2 HOTAS F-16EX  Saitek Pro Rudder

Link to comment
Share on other sites

  • 2 years later...

Very late to this party (in 2023!) but this is GOLD! Thanks Hardcard! :thumbsup:

  • Like 1

Intel i7-2600K 3.4Ghz

ASUS P8P67 EVO Motherboard B3

Kingston HyperX KHX1600C9D3K2/8GX (2x4GB) DDR3

OCZ RevoDrive PCI-Express SSD 120GB

Corsair Hydro Series H70 CPU Cooler

2 xWestern Digital Caviar Black 1TB WD1002FAEX

TM Warthog Hotas

TrackIR 5 Pro

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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