Jump to content

Need help with a script for random flag values


Recommended Posts

Hello, Im trying to get a grip on scripting for DCS and have been succesful with some small scripts that would be of use in my missions.

But now Im trying to make a script that sets four flags to a random value of 1 -4 respectively if Flag 1 has a value of 1.

I cant get it to work, can someone help me?

Link to comment
Share on other sites

You can achieve this in many ways, for example:

ONCE > Time More 15 seconds > Flag 1 set value 1

ONCE > Flag 1 value equals 1 > do script (copy/paste the LUA script)

do
  
    --set random flag values
        trigger.action.setUserFlag(2, math.random(1, 5))
        trigger.action.setUserFlag(3, math.random(1, 5))
        trigger.action.setUserFlag(4, math.random(1, 5))
        trigger.action.setUserFlag(5, math.random(1, 5))
        
        --output results for testing
         trigger.action.outText('Flag 2 random value is: '..trigger.misc.getUserFlag(2), 5)
         trigger.action.outText('Flag 3 random value is: '..trigger.misc.getUserFlag(3), 5)
         trigger.action.outText('Flag 4 random value is: '..trigger.misc.getUserFlag(4), 5)
         trigger.action.outText('Flag 5 random value is: '..trigger.misc.getUserFlag(5), 5)
        
end

This is just a basic example, you can use variables and loops to optimize it, or move it to a Lua file and use Do Script file instead of Do Script, etc

Or, you can use ME only, without Lua:

ONCE > Time More 15 seconds > Flag 1 set value 1

ONCE > Flag 1 value equals 1 > set random flag 2 value 1-5 / set random flag 3 value 1-5 and so on, add as many you need

Hope it helps 🙂

  • Thanks 1
Link to comment
Share on other sites

On 4/2/2023 at 3:08 PM, crispy12 said:

Are you trying to learn scripting or just need it for a mission?

I've been using the DML scripting module in this forum and it can do just what you are asking very easily using the RND module

Trying to learn scripting without any addon like MIST, MOOSE or DML for some small simple scripts that would save me a huge amount of time when doing missions. The last days I have been pretty succesful without any knowledge of LUA at all. When I was in grade school and high school 20+ years ago though I had lessons in Turbopascal and Visual Basic, that and Google is what Im going on 🙂

What I want is just to be able to set a flag to true or false or a number between X and X the same way as in the mission editor but using only my scripts. 

In my scripts now I have to read flags that are set in the mission editor to be set random and thats annoying because I then have to set them up there one by one.

I just need a piece of code that makes flag 1 get a value of either 1, 2 or 3 using only a script without any addons.

Then I can figure out the rest myself.

Link to comment
Share on other sites

unless you've confused me, I can't see the problem...

Do you still want the mission editor to refer to these flags? 

If you do, then by default all flags are set to 0 / false. So if you set it to true or a non-zero number in script, then just do it from LUA with the 'trigger' api and your ME Triggers will still be able to see it as they are in the same LUA state/environment. 


Otherwise, if not, then just use variables in LUA. 

Or am I missing something?!

Here's my code for setting random flags to unique numbers that I made for another person on this forum

function SetFlagsToRandomValuesBetweenMinAndMax(min, max, flagID1, flagID2, flagID3, unitIDNumber)

  local randomNumber1, randomNumber2, randomNumber3 = 0, 0, 0
  local repeatUntilUnique = true

  randomNumber1 = math.random(min,max)

  while (repeatUntilUnique) do

    randomNumber2 = math.random(min, max)

    if randomNumber2 ~= randomNumber1 then

      repeatUntilUnique = false

    end

  end

  repeatUntilUnique = true

  while (repeatUntilUnique) do

    randomNumber3 = math.random(min, max)

    if randomNumber3 ~= randomNumber1 and randomNumber3 ~= randomNumber2 then

      repeatUntilUnique = false

    end

  end

  trigger.action.setUserFlag(flagID1, randomNumber1)
  trigger.action.setUserFlag(flagID2, randomNumber2)
  trigger.action.setUserFlag(flagID3, randomNumber3)

  if unitIDNumber then

    local text = string.format("Flags:\n%s: %d\n%s: %d\n%s: %d", flagID1, randomNumber1, flagID2, randomNumber2, flagID3, randomNumber3)

    trigger.action.outTextForUnit(unitIDNumber, text, 10, true)

  end

end

 

Link to comment
Share on other sites

6 minutes ago, markom said:

There is a minor bug in the above function. Yes, it may generate three random values, but no two of those can ever be the same. And for a smaller range of values (say 0-1), you will have an infinite loop.

there isn't an error. It works perfectly, and read the name of the function. 

7 minutes ago, markom said:

There is a minor bug in the above function. Yes, it may generate three random values, but no two of those can ever be the same. And for a smaller range of values (say 0-1), you will have an infinite loop.

A) if you're an idiot and have a function for 3 unique random numbers and give it only a range of two, it's your own stupid fault. 

B) The whole point of the function WAS for 3 unique random numbers - as someone asked me to write that exact function.

C) Idiots are going to idiot, this function was set to do a specific thing - and it does it perfectly. It's not what you **think** it's doing. 

Link to comment
Share on other sites

No need for language. The request in this forum did not ask for unique numbers though - just random numbers between values X and Y. I simply pointed out a bug. /shrug

[ I shied away from commenting on the copy/paste code instead of properly iterating, and using hashes as a faster way to identify duplicate numbers. ]


Edited by markom
Link to comment
Share on other sites

16 minutes ago, markom said:

No need for language. The request in this forum did not ask for unique numbers though - just random numbers between values X and Y. I simply pointed out a bug. /shrug

[ I shied away from commenting on the copy/paste code instead of properly iterating, and using hashes as a faster way to identify duplicate numbers. ]

 

So 

a) You're <profanity>-posting about a sample code that I provided to give a clue to the original person - not directly provide their answer, as their requirement isn't completely clear and they didn't even seem to know about math.random().

b) I noticed you didn't provide any code for their problem or did anything useful except dumping on someone who did offer some coding help. 

c) the code was written simply as it was provided TO A NEWBIE to help them understand HOW IT WORKS so they can modify it for their own use. You think talking about the extra ops to 'hash' a simple int is suitable or appropriate for helping a newbie? What are you on?!

d) If you want to get your 'superiority complex jollies' about pre-emptively 'optimising' someones code for a trivial function that maybe get's called once per mission... then you really have shown your intent. 

Please don't bother engaging with a post of mine again. Thanks. 


Edited by Elphaba
Link to comment
Share on other sites

That's a fair comment, indeed. I apologize. When I wrote my original comment, I was more coming from "this was asked for by a beginner, they can lock-up their DCS if they use this wrongly" - mostly because I knew they were unlikely to catch the corner case.

Again, I apologize if all that came out wrong.

As an aside, and this is truly my ignorance. But wouldn't this function always generate the numbers of the same values for the same inputs every time? Do you happen to know how random seed is initialized in DCS for user scripts? As I said - I am now genuinely curious (also, there were other posts on this topic in the forum in the last week or so).


Edited by markom
Link to comment
Share on other sites

  • ED Team
1 hour ago, Elphaba said:

there isn't an error. It works perfectly, and read the name of the function. 

A) if you're an idiot and have a function for 3 unique random numbers and give it only a range of two, it's your own stupid fault. 

B) The whole point of the function WAS for 3 unique random numbers - as someone asked me to write that exact function.

C) Idiots are going to idiot, this function was set to do a specific thing - and it does it perfectly. It's not what you **think** it's doing. 

Normally I delete these and give warning points, it will still get warning points but in what world is this an acceptable way to treat your fellow DCS community members, we are all here for the fun and joy of DCS World, please be kind and treat each other with the respect you would like to be treated with. 

This goes for everyone here, please post in a mature and constructive manner or take it elsewhere.

Thanks. 

  • Like 1
  • Thanks 1

64Sig.png
Forum RulesMy YouTube • My Discord - NineLine#0440• **How to Report a Bug**

1146563203_makefg(6).png.82dab0a01be3a361522f3fff75916ba4.png  80141746_makefg(1).png.6fa028f2fe35222644e87c786da1fabb.png  28661714_makefg(2).png.b3816386a8f83b0cceab6cb43ae2477e.png  389390805_makefg(3).png.bca83a238dd2aaf235ea3ce2873b55bc.png  216757889_makefg(4).png.35cb826069cdae5c1a164a94deaff377.png  1359338181_makefg(5).png.e6135dea01fa097e5d841ee5fb3c2dc5.png

Link to comment
Share on other sites

  • 2 weeks later...

Hi Fisherman82,

Here is a mission, and the supporting doc, that has 5 possible objectives from which one is randomly selected at the mission start.  Upon completion, a Client can choose, through the F10 Radio, to randomly load another one of the four remaining objectives.  When that objective is met the Client has the option of loading a final objective from the three possibilities that are left.  The scripts in the mission have to keep track of which objectives have been used so it won't choose the same one twice.   

My scheme for the events in the doc are using abbreviations for the event type, such as:

MS is for Mission Start

SC is for Switched Condition

O would be for Once

Then the name of the event follows.

Then, where it says stuff like F73T that is the Condition and means Flag 73 is True.  Example:

-- SC, code.F5 Next Random Value, F18T --------- this means the event is a Switched Condition, name is "code.F5 Next Random Value", and Condition is Flag 18 True

Let me know if you have any questions.  I had a lot of help from the nice folks around here when I developed this so feel free to ask away!  

 

I do use Mist. 

 

Boxcutter Aleppo 04.lua Boxcutter Aleppo 27S1k.miz

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

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...