How to reset value (Full Version)

All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion



Message


Sharana -> How to reset value (11/25/2018 10:16:29 PM)

Hello,
I have simple event to help detect SAM sites using satellites. There is simple unit enters the area trigger that runs the following lua:
quote:


a = math.random(1,100)
if a <= 15 then
ScenEdit_SetUnit({guid="b61acb6c-45c3-4b40-b467-5f5e50e40506", autodetectable="True"}) --S-300PMU-2 (Damascus North) #2
ScenEdit_SetUnit({guid="d12fc6f0-f565-4f89-b4b1-8368fa8c0a12", autodetectable="True"}) --S-300 decoy #3
...
elseif a >15 and a <= 30 then
...
elseif a >30 and a <= 50 then
...
elseif a >50 and a <= 70 then
...
elseif a >70 and a <= 90 then
...
elseif a >90 and a <= 100 then
...
else
end


The point is to randomly locate number of SAMs (making them auto detectable while another trigger makes them no longer auto detectable when the satellite moves out of range).

The problem is this works the first time. As there are 10 or so satellite passes during the scenario I need to reset "a", so that on the next satellite pass another set of SAMs is selected with random number roll. If that turns out to be impossible how can I change stuff to get the effect I'm looking for?




Whicker -> RE: How to reset value (11/25/2018 11:28:45 PM)

2 things:

1) my understanding is that you need to preface the variable name with `local` or else it persists. So change the top line to:
local a = math.random(1,100)
I don't understand this 100%, but I preface everything with local no matter what.

2) random numbers are weird, are you doing `math.randomseed(os.time()) ` anywhere? I think you need to do that on the first line, and then it should work. If you don't do that it picks the same random number each time - it is a random number, it just only has one value unless you seed it with something that changes - like the time.

No idea if your problem is one or the other or both - though I am pretty sure if you are not doing #2 `a` will be the same each time.




Whicker -> RE: How to reset value (11/25/2018 11:30:23 PM)

I guess the issues is really number 2, since you are setting the value of a each time. Still a good idea to declare it as a local variable though.




Sharana -> RE: How to reset value (11/26/2018 9:00:29 AM)

Thanks, I will try that later when I'm home and will let you know if it now works as intended. The problem was that it was returning the same random value (whatever it picked on the 1st pass.

math.randomseed(os.time())
local a = math.random(1,100)


Also do you know of any way to get timestamp and execute something exactly 2 ingame minutes later? The problem I faced was that if you make the units auto detectable just for a second with 1 event they stay as unknown. 1 to 2 ingame minutes need to pass before they turn from unknown to SA-2/3/6 etc. For that reason I have another event that sets them back to not be autodetectable and I was wondering if I can skip it and go only with that 1 event that randomly spots them.




Sharana -> RE: How to reset value (11/26/2018 4:36:09 PM)

The random part worked just fine, thanks.

For the time part I found this example, but I don't understand it in order to try and apply it to the case in question. Some help will be appreciated if someone understand it :)

quote:


-- Set mission to start n minutes from current game time "now"
function SetMissionStartTime(sideName,missionName,minutesFromNow)
local currentTime = ScenEdit_CurrentTime()
local currentMission = = ScenEdit_GetMission(sideName,missionName)
currentTime = currentTime + 5 * 60 * 60 + minutesFromNow * 60
currentMission.isactive = false
currentMission.starttime = os.date("%m/%d/%%I%M%p", currentTime)
end




Whicker -> RE: How to reset value (11/26/2018 8:42:52 PM)

hmm. How many sam units?

Your example code is for a mission, I don't think it would do much for the unit visibility would it?

but here are some notes on what I think the code is doing:

-- Set mission to start n minutes from current game time "now"
function SetMissionStartTime(sideName,missionName,minutesFromNow) -- this is function! it needs to be called and have the 3 parameters passed in to it

local currentTime = ScenEdit_CurrentTime() -- getting the current game time, which is in seconds from 1970 I think

local currentMission = ScenEdit_GetMission(sideName,missionName) -- get mission table/data from the passed in sidename and mission name (removed an extra = sign that doesn't make sense)

currentTime = currentTime + 5 * 60 * 60 + minutesFromNow * 60 --change that time by adding 5 hours for some reason? time zone adjuistment maybe? then adds the time from now that is requested, the *60 is to convert it to seconds - and the *60 *60 is to convert the 5 to seconds so it must be 5 hours?

currentMission.isactive = false --set mission to not active

currentMission.starttime = os.date("%m/%d/%%I%M%p", currentTime) --set star time of mission. os.date is to set the formatting of the time that was calculated into what the game is expecting - somewhere there is list of what m,d,I,M and p means. Something like month day, minutes and seconds? no idea without referring to the definitions.

end --closes function

That function would be called somewhere like:
SetMissionStartTime('Blue','bombers mission',120)

Time is fairly confusing - the 5 in this code is odd, I don't think it is usually needed.

All that said what it looks like you would be trying to do is create a new event, with a trigger of 2 minutes from now, and lua to reverse the unit visibility. Events and time are different still, there is quite a bit of info in here about that where I was asking about how to do this.

If you want another good example - the downed pilot code originally by Angster has all kinds of stuff in it, like creating new events and triggers at x minutes from now. I have played with it quite a bit and wrote about it here (link below) as well as posted the full code of it. I highly recommend it as a learning experience. I understood about half of what he did, and built on that. Some of how he did it is still a mystery to me:
https://commandops.github.io/posts/survivors-script/


It may be a better idea to create a list of sams, and check them every 5 minutes, and if they are set to auto detect change it back. That would probably be a lot simpler, but would not do exactly what you want - it might run 20 seconds after your other code changed it. Maybe also you could do something with the same satellite leaving the area? not too sure about that.




Sharana -> RE: How to reset value (11/26/2018 9:25:56 PM)

There is no trigger for leaving the area - there is remains in area for X amount of time. I'm currently using that one, but because the sattelites have different patchs sometimes they exit the arena before the minimal time needed and the trigger doesn't fire leaving the SAMs autodetectable till the next pass.

Thanks for the SAR one, I will have a look at it. Making another event sounds like good idea, I saw this example for special action - just need to figure out how to set it up for 2 minutes without the input box :)
quote:

function TimerTimeFromNowDotNetTime(addSeconds) -- Time helper cause event time is weird
local time = ScenEdit_CurrentTime()
local offSet = 62135596801 --number of seconds from 01-01-0001 to 01-01-1970
local newTime = (time + offSet + addSeconds)10000000
local timeToUse = string.format("%18.0f",newTime)
return timeToUse
end

function CreateTimerEvent(minutes,message)
local t = ScenEdit_CurrentTime() --need to add something to the names to make them unique, using time
ScenEdit_SetEvent(minutes.." Minute Timer ("..message..")"..t, {mode="add",IsRepeatable=0})
ScenEdit_SetTrigger({mode="add", type="Time", name=minutes.." Minute Timer Trigger ("..message..")"..t, Time=TimerTimeFromNowDotNetTime(minutes60) })
ScenEdit_SetAction({mode="add", type="LuaScript", name=minutes.." Minute Timer Action ("..message..")"..t,
scriptText='ScenEdit_SpecialMessage (ScenEdit_PlayerSide(),"Your Timer is Up: '..message..' ")'})
ScenEdit_SetEventTrigger(minutes.." Minute Timer ("..message..")"..t, {mode="add", name=minutes.." Minute Timer Trigger ("..message..")"..t})
ScenEdit_SetEventAction(minutes.." Minute Timer ("..message..")"..t, {mode="add", name=minutes.." Minute Timer Action ("..message..")"..t})
end

local timerMinutes = ScenEdit_InputBox('How long do want the timer to be in minutes?')
print('Response was ' ..timerMinutes)
local timerMessage = ScenEdit_InputBox('What do you want to call the timer?')
print('Response2 was ' .. timerMessage)
CreateTimerEvent(timerMinutes,timerMessage)




Whicker -> RE: How to reset value (11/26/2018 11:33:49 PM)

ah yes - I wrote that - in game timer.

That should be easy to modify:

quote:


function TimerTimeFromNowDotNetTime(addSeconds) -- Time helper cause event time is weird
local time = ScenEdit_CurrentTime()
local offSet = 62135596801 --number of seconds from 01-01-0001 to 01-01-1970
local newTime = (time + offSet + addSeconds)10000000
local timeToUse = string.format("%18.0f",newTime)
return timeToUse
end

function CreateTimerEvent(minutes)
local t = ScenEdit_CurrentTime() --need to add something to the names to make them unique, using time
ScenEdit_SetEvent(minutes.." Minute Timer"..t, {mode="add",IsRepeatable=0})
ScenEdit_SetTrigger({mode="add", type="Time", name=minutes.." Minute Timer Trigger"..t, Time=TimerTimeFromNowDotNetTime(minutes60) })
ScenEdit_SetAction({mode="add", type="LuaScript", name=minutes.." Minute Timer Action-"..t, scriptText='XX your lua code here'})
ScenEdit_SetEventTrigger(minutes.." Minute Timer"..t, {mode="add", name=minutes.." Minute Timer Trigger"..t})
ScenEdit_SetEventAction(minutes.." Minute Timer"..t, {mode="add", name=minutes.." Minute Timer Action"..t})
end

CreateTimerEvent(2)


The above code may work, can't test it though. You need to add your lua code to where it says: scriptText='XX your lua code here' - where the setAction line is.

Other than this I have not done much code like this (creating a lua action in lua), you may have trouble inserting the lua code. You can enclose stuff with either ' or " - I think you need to use one on the outside and the other on the inside so the game can know which quotes are paired if that makes sense. You can sort of see how I did that on that line.




Whicker -> RE: How to reset value (11/27/2018 12:44:52 AM)

I was thinking about the code I posted above - if it works you would still need to be careful with the actions lua code to make it correct for whichever sam needs to be reset - it won't be the same each time as I understand it. Probably not too hard to do, but it is additional complexity.

I still think making something that runs every 5 minutes and checks a list of units and resets them if needed would be easier. But this is a great example to learn a bunch of stuff.




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.796875