Lua Help.....randomize the deletion of a unit

Take command of air and naval assets from post-WW2 to the near future in tactical and operational scale, complete with historical and hypothetical scenarios and an integrated scenario editor.

Moderator: MOD_Command

Post Reply
DWReese
Posts: 2547
Joined: Fri Mar 21, 2014 11:40 am
Location: Miami, Florida

Lua Help.....randomize the deletion of a unit

Post by DWReese »

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
User avatar
CCIP-subsim
Posts: 467
Joined: Tue Nov 10, 2015 6:59 pm

RE: Lua Help.....randomize the deletion of a unit

Post by CCIP-subsim »

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
Posts: 2547
Joined: Fri Mar 21, 2014 11:40 am
Location: Miami, Florida

RE: Lua Help.....randomize the deletion of a unit

Post by DWReese »

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

Doug
User avatar
kevinkins
Posts: 2465
Joined: Wed Mar 08, 2006 11:54 am

RE: Lua Help.....randomize the deletion of a unit

Post by kevinkins »

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/201 ... ers-in-lua
“The study of history lies at the foundation of all sound military conclusions and practice.”
Alfred Thayer Mahan
User avatar
CCIP-subsim
Posts: 467
Joined: Tue Nov 10, 2015 6:59 pm

RE: Lua Help.....randomize the deletion of a unit

Post by CCIP-subsim »

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
Posts: 2547
Joined: Fri Mar 21, 2014 11:40 am
Location: Miami, Florida

RE: Lua Help.....randomize the deletion of a unit

Post by DWReese »

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
Posts: 175
Joined: Thu Apr 27, 2017 1:04 pm

RE: Lua Help.....randomize the deletion of a unit

Post by Dan109 »

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
Posts: 2547
Joined: Fri Mar 21, 2014 11:40 am
Location: Miami, Florida

RE: Lua Help.....randomize the deletion of a unit

Post by DWReese »

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
Posts: 175
Joined: Thu Apr 27, 2017 1:04 pm

RE: Lua Help.....randomize the deletion of a unit

Post by Dan109 »

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
Post Reply

Return to “Command: Modern Operations series”