Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

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

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Lua Help.....randomize the deletion of a unit Page: [1]
Login
Message << Older Topic   Newer Topic >>
Lua Help.....randomize the deletion of a unit - 6/7/2017 3:46:05 AM   
DWReese

 

Posts: 1725
Joined: 3/21/2014
Status: offline
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
Post #: 1
RE: Lua Help.....randomize the deletion of a unit - 6/7/2017 4:13:23 AM   
CCIP-subsim


Posts: 695
Joined: 11/10/2015
Status: offline
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)

< Message edited by CCIPsubsim -- 6/7/2017 4:14:38 AM >

(in reply to DWReese)
Post #: 2
RE: Lua Help.....randomize the deletion of a unit - 6/7/2017 4:30:33 AM   
DWReese

 

Posts: 1725
Joined: 3/21/2014
Status: offline
Thank you so much. It's nice to have an easy one once in a while.

Doug

(in reply to CCIP-subsim)
Post #: 3
RE: Lua Help.....randomize the deletion of a unit - 6/7/2017 12:06:48 PM   
kevinkins


Posts: 2225
Joined: 3/8/2006
Status: offline
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

(in reply to DWReese)
Post #: 4
RE: Lua Help.....randomize the deletion of a unit - 6/7/2017 1:34:40 PM   
CCIP-subsim


Posts: 695
Joined: 11/10/2015
Status: offline
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



< Message edited by CCIPsubsim -- 6/7/2017 1:35:16 PM >

(in reply to kevinkins)
Post #: 5
RE: Lua Help.....randomize the deletion of a unit - 6/11/2017 9:41:01 PM   
DWReese

 

Posts: 1725
Joined: 3/21/2014
Status: offline
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

(in reply to CCIP-subsim)
Post #: 6
RE: Lua Help.....randomize the deletion of a unit - 6/11/2017 10:21:43 PM   
Dan109

 

Posts: 175
Joined: 4/27/2017
Status: offline
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')


(in reply to DWReese)
Post #: 7
RE: Lua Help.....randomize the deletion of a unit - 6/11/2017 10:49:50 PM   
DWReese

 

Posts: 1725
Joined: 3/21/2014
Status: offline
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

(in reply to Dan109)
Post #: 8
RE: Lua Help.....randomize the deletion of a unit - 6/12/2017 7:29:54 PM   
Dan109

 

Posts: 175
Joined: 4/27/2017
Status: offline
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

(in reply to DWReese)
Post #: 9
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Lua Help.....randomize the deletion of a unit Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.234