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.