Lua code for Delayed Messages? Condition?

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
User avatar
BeirutDude
Posts: 2733
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

Lua code for Delayed Messages? Condition?

Post by BeirutDude »

Anybody have any Lua code for sending a message some time after an event has fired. In particular I want to send an EAM a few minutes after an event to simulate reaction time.
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
KnightHawk75
Posts: 1552
Joined: Thu Nov 15, 2018 7:24 pm

RE: Lua code for Delayed Messages? Condition?

Post by KnightHawk75 »

This is doesn't handle every situation that can come up or advanced situations where you want multiple things happening with the message or cases where you want or need the event name to have things added to the title etc,etc.. but if all you want is a simple 'send message 5 minutes from now' it will handle it for you. Change the style in the header as desired the cmo defaults are #333333 for bgcolor and 'Lightgrey' for text color, the defaults below are FFFFFF (white) and 000000(black).

Code: Select all

 gKH={};gKH.Events={};
 ---Creates the event and all the actions and associations needed to send a message
 ---@param eventName string @ the name to give to the event created.
 ---@param eventDetails? table @ the table of details for the event or if submitted as nil defaults will be used.
 ---@param triggerName string @ the name to give to the trigger created.
 ---@param futureTime integer @ the gametime to assign to the time trigger for when you want the message displayed.
 ---@param actionName string @ the name to give to the generated LUAScript action created.
 ---@param messagestring string @ the actual text or html of the special message.
 ---@param msgSide? string @ the name of the side to display the message, if left empty 'playerside' will be substituted.
 ---@return boolean @ true if there were no failures, otherwise false. 
 function gKH.Events:SendSimpleMessage(eventName, eventDetails, triggerName, futureTime,actionName,messagestring,msgSide)
     local function convertime(t)
         return 621355968000000000 + (tonumber(t) * 10000000)
     end
     local function getRandomSuffix()
       local a = math.random(65,90); --generate random number from 65-90  {utf caps}
       local b = math.random(97,122); --generate random number from 97-122 {utf lower}
       local c = math.random(1000,99999)--generate random number from 1000-99999
       local d = math.random(65,90);--generate random number from 65-90  {utf caps}
       local e = math.random(97,122); --generate random number from 97-122 {utf lower}
       return '(' .. string.char(a,b) .. tostring(c) .. string.char(d,e) .. ')' 
     end
     local r = getRandomSuffix() --tack on a unique id so names are not duplicated. 
 

Code: Select all

 
     if eventName==nil or triggerName==nil or futureTime ==nil or actionName==nil or messagestring == nil then
         print("invalid or missing parameters"); return false;
     end
     eventName=eventName .. r; triggerName=triggerName .. r; actionName=actionName .. r;
     if futureTime ~=nil then futureTime = convertime(futureTime) end
     if (msgSide ==nil) or msgSide == "" then msgSide='playerside'; end
     local newEvent;
     if eventDetails == nil then
         newEvent = {Description=eventName,mode="add",isRepeatable=false,isShown = false, isActive = true} ---@type CMO__EventUpdate
     else
         newEvent = eventDetails;
     end
     local header = '<HTML><style>body {background-color: #FFFFFF; font-family: """New Times Roman""",arial,verdana,san-serif; color:#000000;}</style><body>'
     local footer = '</body></HTML>'
     local newtriggerdetails = {Description = triggerName, Mode="add", Type="Time", Time=futureTime} ---@type CMO__TriggerUpdate
     local newactiondetails = {Description=actionName, Mode="add", Type= "LuaScript", 
           ScriptText = 'ScenEdit_SpecialMessage(\"' .. msgSide .. '\",\''.. string.format("%s%s%s",header,messagestring,footer) ..'\');'} ---@type CMO__ActionUpdate
     local tca = {mode="add",description=""} ---@type CMO__EventTCAUpdate;
     local retval,retval2; local triggersuccess,actionsuccess,eventsuccess,tassignmentsuccess,aassignmentsuccess = false,false,false,false,false;
 

Code: Select all

     retval,retval2 = pcall(ScenEdit_SetTrigger, newtriggerdetails); if retval ==true and retval2 ~=nil then triggersuccess = true; end
     retval,retval2 = pcall(ScenEdit_SetAction, newactiondetails); if retval ==true and retval2 ~=nil then actionsuccess = true; end
     retval,retval2 = pcall(ScenEdit_SetEvent, eventName,newEvent); if retval ==true and retval2 ~=nil then eventsuccess = true; end
     tca.description = triggerName;
     retval,retval2 = pcall(ScenEdit_SetEventTrigger,eventName, tca); if retval ==true and retval2 ~=nil then tassignmentsuccess = true; end
     tca.description = actionName;
     retval,retval2 = pcall(ScenEdit_SetEventAction, eventName, tca); if retval ==true and retval2 ~=nil then aassignmentsuccess = true; end
 
     if triggersuccess and actionsuccess and eventsuccess and triggersuccess and aassignmentsuccess then return true; else return false; end
 end
 

Code: Select all

 Usage (sample creates and event that will run 5 minutes from current game time): 
 ----------------------------------------
 local msg = '<p>black text on white Hello World. <p>Line 2 Hello World'; --make sure this is properly string escaped so that the escaping ends up in the luascript. 
 local msg2 = '<p>black text on white Hello World with a \\\"double quote around this\\\" . <p>Line 2 Hello World'; --escaped example. 
 
 if gKH.Events:SendSimpleMessage("SomeMessageEventName 1",nil,"SomeMessageTimeTriggerName 1",(ScenEdit_CurrentTime() + 300),"SomeMessageActionName 1",msg) == true then
   print("event for message created.");
 else
   print("Something went really wrong.");
 end
 

I have some more advanced stuff it really needed (let me know) but comes with dependencies.
edit: added example of how you would insert escaped characters into the message text code.
User avatar
BeirutDude
Posts: 2733
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Lua code for Delayed Messages? Condition?

Post by BeirutDude »

Knighthawk75,

Thanks for the help. Here is what I would like to do...

If an event fires, call it "Nuclear Detonation over Golan" is to send a Special Message with the event. And then follow it up 4 or 5 minutes later with a NUDET EAM.

That's it.

Thanks.
Al

"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
KnightHawk75
Posts: 1552
Joined: Thu Nov 15, 2018 7:24 pm

RE: Lua code for Delayed Messages? Condition?

Post by KnightHawk75 »

Then above should work for you without issue, add the code to run on every sceneload.
If you don't like the black on white just change those values back to the original values.
Then attach an addition lua action in your Nuclear Detonation over Golan even, after the other displayed messaged, that runs something like the example usage:

local msg = 'Type out your EAM message here using basic html formatting if you like (or not) just making sure to escape quotes and double qoutes if used or other special characters like if you want a single backslash then use a double as it's the escape character itself so two means yes I really want one \\'

gKH.Events:SendSimpleMessage("Nuclear Detonation over Golan - Delayed EAM",nil,"TimeTrigger: NUDET Golan EAM",(ScenEdit_CurrentTime() + 300),"LUA: NUDET EAM Message",msg) --delays message 5 minutes.
User avatar
BeirutDude
Posts: 2733
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Lua code for Delayed Messages? Condition?

Post by BeirutDude »

Thanks I will give it a try!
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
Post Reply

Return to “Lua Legion”