Creating an event within Lua

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
User avatar
vettim89
Posts: 3668
Joined: Fri Jul 13, 2007 11:38 pm
Location: Toledo, Ohio

Creating an event within Lua

Post by vettim89 »

So I am trying to create an event as part of a Lua script and I hit an impasse. I pre-created the Event Action because it was just easier for me. I am trying to create the trigger then create the event with the new trigger and the pre-created action attached.

Code: Select all

math.randomseed(os.time())
math.random();
math.random(); math.random()

local currtime = ScenEdit_CurrentTime() 
print(currtime)
local delay=math.random(10800, 18000)
local futuretime = currtime + delay
print(futuretime)
local convtime = (os.date('%m-%d-%Y %H:%M:%S %p', futuretime)) --Convert the future time to DateTime
print(convtime)

ScenEdit_SetTrigger( {mode='add', type='time', name='USSR:Banak Changeover', time=convtime})
ScenEdit_SetEvent('USSR:Banak Changeover', {mode='add', isActive=true, isRepeatable=false, isShown=true, name='USSR:Banak Changeover'})
ScenEdit_SetEventTrigger('USSR:Banak Changeover', {mode=add, Description='USSR:Banak Changeover'})
ScenEdit_SetEventAction('USSR:Banak Changeover', {mode=add, Description='USSR:Banak Changeover'})
I got the following error when trying to execute:
ScenEdit_SetEventTrigger 0 : ,Unknown operation!
If I am interpreting this correctly the console doesn't seem to recognize the line as valid code. Is that right? What did I do wrong?
"We have met the enemy and they are ours" - Commodore O.H. Perry
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Creating an event within Lua

Post by KnightHawk75 »

Put quotes around the mode=add's in both SetEventTrigger and SetEventAction calls. Also I added the correct way to set time as it need to be converted.

Code: Select all

---Converts CMOScene/Lua Timestamp to .net TickTime
---@param theUnixStamp number @string or number that is the CMOScene/Lua/unix Timestamp.
---@return number @number of ticks since 1/1/1:0:0:0.
local function convertLuaTimestampToDotNetTickTime(theUnixStamp)
   return 621355968000000000 + (tonumber(theUnixStamp) * 10000000)
end

local currtime = ScenEdit_CurrentTime() 
print(currtime)
local delay=math.random(10800, 18000)
local futuretime = currtime + delay
print(futuretime)
local convtime = (os.date('%m-%d-%Y %H:%M:%S %p', futuretime)) --Convert the future time to DateTime
print(convtime)

ScenEdit_SetTrigger( {mode='add', type='Time', name='Trigger - USSR:Banak Changeover', Time=convertLuaTimestampToDotNetTickTime(futuretime)})
ScenEdit_SetEvent('USSR:Banak Changeover', {mode='add', isActive=true, isRepeatable=false, isShown=true, name='USSR:Banak Changeover'});
ScenEdit_SetEventTrigger('USSR:Banak Changeover', {mode='add', Description='Trigger - USSR:Banak Changeover'});
ScenEdit_SetEventAction('USSR:Banak Changeover', {mode='add', Description='AlreadyCreatedAction-USSR:Banak Changeover'});
Assuming in the above example an action called 'AlreadyCreatedAction-USSR:Banak Changeover' (adjust if needed) exists the above should run without error. Keep in mind during testing a 'Time' type trigger, that once it fires in a scene and the scene is saved, it's fired and will not fire again even if you roll back scene-time, you have to delete the trigger and recreate it to have it fire again.
Last edited by KnightHawk75 on Thu Apr 28, 2022 4:55 am, edited 1 time in total.
User avatar
vettim89
Posts: 3668
Joined: Fri Jul 13, 2007 11:38 pm
Location: Toledo, Ohio

Re: Creating an event within Lua

Post by vettim89 »

Bingo! I tested the code to create the trigger to make sure it worked first. The trigger was already in existence because I never deleted it.
"We have met the enemy and they are ours" - Commodore O.H. Perry
Post Reply

Return to “Lua Legion”