Lua Help.....randomize the deletion of a unit (Full Version)

All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series



Message


DWReese -> Lua Help.....randomize the deletion of a unit (6/7/2017 3:46:05 AM)

I have a Lua Script question.

I am using a Lua Script in a Special Action (which costs the game player some Victory Points to implement) to delete a unit from the map. I do know how to do this.

But, what if I wanted to activate this action, but I also wanted to randomize it to only occur a certain percentage of the time, such as like 80 percent of the time.

Is this possible? If so, what would I need to add to this string which eliminates the unit to make it occur 80 percent of the time?

ScenEdit_DeleteUnit({side="Iran", unitname="SA-20a Gargoyle"})

If I can do this, the game player will have to risk paying the Victory Points in hopes of getting something accomplished, but not knowing whether it worked at all.

Thanks in advance.

Doug




CCIP-subsim -> RE: Lua Help.....randomize the deletion of a unit (6/7/2017 4:13:23 AM)

Here's an easy one:


local g = math.random(1,10)
if g <= 8 then
ScenEdit_DeleteUnit({side="Iran", unitname="SA-20a Gargoyle"})
end


("g" is just an arbitrary variable name - you can replace it with something more descriptive like "gargoyledeletechance" if you want)




DWReese -> RE: Lua Help.....randomize the deletion of a unit (6/7/2017 4:30:33 AM)

Thank you so much. It's nice to have an easy one once in a while.

Doug




kevinkins -> RE: Lua Help.....randomize the deletion of a unit (6/7/2017 12:06:48 PM)

Doug

I remember reading something about lua's random function not being 100% reliable. This may not become an issue for you but I wanted to make you aware. Note: the seeding process as outline in the stack over-flow thread below.

Kevin

https://stackoverflow.com/questions/20154991/generating-uniform-random-numbers-in-lua




CCIP-subsim -> RE: Lua Help.....randomize the deletion of a unit (6/7/2017 1:34:40 PM)

Yes, I was actually thinking that earlier today.

It's only really an issue if you are randomizing a number repeatedly, or you have several math.random instances with the same range in the same script (e.g. if you have to roll a "virtual 10-sided die" 3 times, each time to determine the chance of a certain SAM being deleted) then it's going to generate the same number for each of them. But if you're only using this once, at the start of the scenario, it's not an issue I don't think.

But if you want to make sure that your numbers are truly random, then just plug this in at the start of your script:

math.randomseed(os.time())

also easy enough [:)]





DWReese -> RE: Lua Help.....randomize the deletion of a unit (6/11/2017 9:41:01 PM)

I just discovered a problem with my Special Action for my scenario. Here is what I have so far:

local g = math.random(1,10)
if g <= 7 then
ScenEdit_DeleteUnit({side="Iran", unitname="SA-20a Gargoyle"})
a=ScenEdit_GetScore("Israel")
ScenEdit_SetScore('Israel',(a-500),'-500 Points for Suppressing SA-20a bn')
end

It is working fine as long as the random roll was 7 or below. If not, then, of course, the unit does not get deleted. What I would like is if you select this Special Action, I still want you to have to pay the 500 Victory Points, regardless of what randomized number occurs. So, by paying the 500 VP, you have a CHANCE to delete the unit.

What changes should I make to make this happen?

Thanks in advance.

Doug




Dan109 -> RE: Lua Help.....randomize the deletion of a unit (6/11/2017 10:21:43 PM)

Doug, this will do it

local g = math.random(1,10)
if g <= 7 then
ScenEdit_DeleteUnit({side="Iran", unitname="SA-20a Gargoyle"})
end
a=ScenEdit_GetScore("Israel")
ScenEdit_SetScore('Israel',(a-500),'-500 Points for Suppressing SA-20a bn')





DWReese -> RE: Lua Help.....randomize the deletion of a unit (6/11/2017 10:49:50 PM)

Thanks, Dan

I see that the only difference was where the word "end" was placed. When I was first provided with the example, it was one correctly. It was ME who moved the word END to the end of the string because that where I thought that the word belonged, since it was the END. It seemed logical at the time. So, I messed it up myself. Well, at least I'm trying to learn LUA.

Thanks again for your assistance and patience.

Doug




Dan109 -> RE: Lua Help.....randomize the deletion of a unit (6/12/2017 7:29:54 PM)

Np, you only learn from trying and failing - I've pulled plenty of hair out the last few days working on my Cruise Missile ToT mod. All of the prototypes were working great, at one point, I had over 100 tomahawks flying in a gentle 2nm radius loiter pattern over my task force waiting to begin their flight plan. Then I glued it all together and nothing works now!! Lol




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.6875