![]() |
|
|
||||||
| Operation Flashpoint: Dragon Rising - Mission Editing and Modding Chat Zone A little area for all you mission editors and modders to chat and discuss ideas. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
25-11-2009, 02:11 AM
|
#1 (permalink) |
|
Senior Member
Join Date: Oct 2009
Location: New York
Posts: 536
|
I've been meaning to post this code for awhile, but I've just figured out a few things that make it easier to use and thought now was as good a time as any. Particularily since it seems that more and more issues are turning up with OFP timers.
This is a timer object (for lack of a better term, it's not an object in the C++/Java sense) that can be used to schedule arbitrary functions to be executed at some time in the future. You can download the code here. I've also included a two sample mssn files that that show how to use the timer. The first randomly spawns a number of enemy echelons around your position and respawns them 10 to 20 seconds after they have been killed. The second mission calls in three JDAM airstrikes five seconds apart. INSTALLATION
USAGE To use the timer in your own scripts your level.lua script must contain a onMissionStart function. You do not need to import the secondary script or start the timer or do anything else. Just have an onMissionStart function (even an empty one). This is required so the Timer can inject itself into your code. SCHEDULING There are three functions that can be used to schedule events.
And five functions for timer control (these are mostly untested)
Note that all times are given in seconds and not milliseconds like the OFP timers. Use floating point numbers to specify fractions of a second, i.e 1.5, 0.25 (although see the caveats below before going to crazy). EVENTS The 'event' used can either be the name a function or a table that contains a function named 'run'. Any Lua function can be called by the Timer and you do not need to define any onTimer_XXX functions unless you start your own OFP timers. EXAMPLES Code:
function onMissionStart()
Timer:Schedule(once, 5) -- runs 'once' in five seconds
Timer:Repeating(always, 30) -- runs 'always' every 30 seconds.
Timer:RepeatIf(maybe, 2) -- runs 'maybe' every two seconds, maybe.
end
function once()
OFP:displaySystemMessage("Timer called once")
end
function always()
OFP:displaySystemMessage("Timer called always")
end
maybe = {
count = 3
run = function(self)
OFP:displaySystemMessage("Count down " .. self.count)
self.count = self.count - 1
return self.count > 0
end
end
PROS
Code:
function onDeath(victim, unit)
local echelon = OFP:getEchelon(victim)
if not OFP:isAlive(echelon) then
local setID = ... -- get the entity set ID for the echelon
local setName = ... -- get the name of the entity set to spawn
local respawn = function()
OFP:destroyEntitySet(setId)
OFP:activateEntitySet(setName)
end
-- respawn between 10 and 20 seconds from now
local time = math.random(10, 20)
Timer:Schedule(respawn, time)
end
end
Hopefully others will find this useful. I use it all the time, but that is because I wrote it ;)
__________________
In theory there is no difference between theory and practice. But in practice there is. Intro to LUA, Fun with tables, Undocumented Functions, Saving Game State Timer library, Sector Control Framework Finite State Machines (part 1) Last edited by Haywood Slap; 30-11-2009 at 02:42 AM. Reason: Updated for latest version (1.0b3) |
|
|
25-11-2009, 02:25 AM
|
#2 (permalink) |
|
Senior Member
Join Date: Oct 2009
Posts: 2,497
|
whoa, awesome stuff. I sure hope CM work out a way to import user scripts for Patch 2 so adding these types of things is alot easier
__________________
Island War - Free Roam Download Release Candidate here The ENTIRE Island is at your feet! Explore, conquer, have fun! COOP with up to 3 friends! ---------------------------------------- Templar's Completely Awesome Entity Database Real Muzzle Velocity, Rate Of Fire, Effective Range, Recoil. Better Aimed Position. No zooming on sights! ---------------------------------------- PAYPAL DONATE |
|
|
26-11-2009, 12:45 AM
|
#5 (permalink) | |
|
Senior Member
Join Date: Oct 2009
Location: New York
Posts: 536
|
Well I've already found and fixed one bug (I should know better than to add code moments before releasing it... sigh). You can download the fixed version, or if you already have downloaded the code you can apply the fix yourself.
On line 180, in the Timer:Resume function is the line Code:
if not paused then return end Code:
if not self.paused then return end Quote:
It would also be nice if CM came up with a way to install library scripts that allowed the script authors to provide the XML needed to update the Autocomplete.xml and LuaSyntax.xml files so that the editor would provide online help, autocompletion and syntax coloring. For example, if you want the Timer script to look really official go to the "Mission Editor/Database/LuaEditor" folder and open the Autocomplete.xml file. Go to the bottom of the file and just above the "</AutoList"> line add the following XML so the end of the file looks like: Code:
<word text="connectToWaypoint" />
<word text="disconnectFromWaypoint" />
<!-- Timer functions added here -->
<word text="Timer:Create" insert="+()" desc="Initializes the timer object. Must be called before a timer can be used."/>
<word text="Timer:Schedule" insert="+()" desc="Schedules an event to be executed once.">
<input>
<argument name="event" type="function" desc="The name of a function or table with a run function defined."/>
<argument name="delay" type="number" desc="The delay (in seconds) until the event is executed."/>
</input>
</word>
<word text="Timer:Repeating" insert="+()" desc="Schedules an event to be executed repeatedly.">
<input>
<argument name="event" type="function" desc="The name of a function or table with a run function defined."/>
<argument name="delay" type="number" desc="The delay (in seconds) until the event is executed."/>
</input>
</word>
<word text="Timer:RepeatIf" insert="+()" desc="Schedules an event to be executed repeated for as long as the event returns true.">
<input>
<argument name="event" type="function" desc="The name of a function or table with a run function defined."/>
<argument name="delay" type="number" desc="The delay (in seconds) until the event is executed."/>
</input>
</word>
<word text="Timer:Pause" insert="+() " desc="Pauses the timer. No events will be executed while the timer is paused."/>
<word text="Timer:Resume" insert="+() " desc="Resumes a paused timer. Any pending events in the timer's queue will be executed."/>
<word text="Timer:Clear" insert="+() " desc="Removes all pending events from the Timer's queue"/>
<word text="Timer:Destroy" insert="+() " desc="Releases the OFP timer and removes all pending events from the timer's queue."></word>
</AutoList>
Code:
<word text="OFP:displaySystemMessage" insert="+()" desc="This will allows to send a message to the chat buffer to be displayed on screen. " > Then open the LuaSyntax.xml file and find the end of the <Patterns> section (around line 287) and add the following: Code:
Timer
Timer:Create
Timer:Schedule
Timer:RepeatIf
Timer:Repeating
Timer:Clear
Timer:Pause
Timer:Resume
Timer:Destroy
</Patterns> <!-- don't add this line, it's just to show you where to paste the above -->
I was thinking of including the modified Autocomplete.xml and LuaSyntax.xml files in the zip, but that would overwrite any other changes people may have made to the files.
__________________
In theory there is no difference between theory and practice. But in practice there is. Intro to LUA, Fun with tables, Undocumented Functions, Saving Game State Timer library, Sector Control Framework Finite State Machines (part 1) |
|
|
|
27-11-2009, 04:36 PM
|
#6 (permalink) |
|
Senior Member
Join Date: Oct 2009
Location: New York
Posts: 536
|
I've updated the first post with a link to the modified Autocomplete.xml and LuaSyntax.xml files need to enable inline help, autocompletion, and syntax coloring in the editor.
__________________
In theory there is no difference between theory and practice. But in practice there is. Intro to LUA, Fun with tables, Undocumented Functions, Saving Game State Timer library, Sector Control Framework Finite State Machines (part 1) |
|
|
30-11-2009, 02:47 AM
|
#7 (permalink) |
|
Senior Member
Join Date: Oct 2009
Location: New York
Posts: 536
|
I've updated the first post with a link to a newest version (1.0b3)
The newest version makes it easier to use the Timer object: simply paste the timer.lua script into a secondary script. There is no need to "import" it, start it, or do any other bookkeeping. You can just start scheduling events. The new version also includes updated Autocomplete.xml and LuaSyntax.xml files (including all the changes for the undocumented functions) that you can copy to you LuaEditor folder. Let me know if anyone has any questions, finds any bugs, or can't get it to work.
__________________
In theory there is no difference between theory and practice. But in practice there is. Intro to LUA, Fun with tables, Undocumented Functions, Saving Game State Timer library, Sector Control Framework Finite State Machines (part 1) Last edited by Haywood Slap; 30-11-2009 at 02:50 AM. |
|
|
07-01-2010, 07:03 AM
|
#8 (permalink) |
|
Junior Member
Join Date: Jan 2010
Posts: 1
|
I like this approach to handling timer events. I'm a dev for Bitfighter (bitfighter.org), and I was just sitting down to write something like this, but I'm going to borrow parts of your code instead.
I'll be sure to attribute the code to you. |
|
|
08-01-2010, 04:21 AM
|
#9 (permalink) |
|
Senior Member
Join Date: Sep 2009
Posts: 226
|
We could do with a sticky for your secondary script and a kind soul to manage the latest update to it.
I have 2 functions that I have created and use in a secondary script in which I have Haywoods. It would be easier if every now and then I could click on the sticky and download the latest version that contains everyones. Might prove unmanageable but I like the idea of a library that easier to access that searching through the forums all of the time.
__________________
"The most rewarding things you do in life are often the ones that look like they cannot be done." Arnold Palmer Despawner function - despawns units/echelons/groups from 1 simple function getTracking function - tracks entities returning intercept spawn coordinates Op Intel_igent Core - intel entry .mssn file shooting range - need the practice? |
|
|
09-01-2010, 05:35 PM
|
#10 (permalink) |
|
Senior Member
Join Date: Oct 2009
Location: New York
Posts: 536
|
Thanks! It is all open source (Apache 2.0 license) so you are free to use code. That is why I posted it
![]() I have been hoping that the mythical second patch will introduce some features to make using and sharing secondary and library scripts much easier. It is a bit of a pain at the moment.
__________________
In theory there is no difference between theory and practice. But in practice there is. Intro to LUA, Fun with tables, Undocumented Functions, Saving Game State Timer library, Sector Control Framework Finite State Machines (part 1) |
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|