Skip to content

Instantly share code, notes, and snippets.

@asarium
Last active May 29, 2020
Embed
What would you like to do?
Basic mod data for the async tutorial
local mnProm = require("missionPromise")
engine.addHook("On Gameplay Start", function()
async.run(function()
mn.sendPlainMessage("Start of the coroutine!")
local ship = async.await(mnProm.waitForShipAsync("TestShip"))
mn.sendPlainMessage("Ship has arrived! Name: " .. ship.Name)
end)
end)
#Mission Info
$Version: 0.10
$Name: XSTR("Untitled", -1)
$Author:
$Created: 05/21/20 at 23:11:05
$Modified: 05/29/20 at 16:37:24
$Notes:
This is a FRED2_OPEN created mission.
$End Notes:
$Mission Desc:
XSTR("Put mission description here
", -1)
$end_multi_text
+Game Type Flags: 1
+Flags: 1024
+Fog Near Mult: 1.000000
+Fog Far Mult: 1.000000
+Disallow Support: 0
+Hull Repair Ceiling: 0.000000
+Subsystem Repair Ceiling: 100.000000
+Viewer pos: 0.000000, 150.000000, -200.000000
+Viewer orient:
1.000000, 0.000000, -0.000000,
0.000000, 0.866025, 0.500000,
0.000000, -0.500000, 0.866025
$AI Profile: FS2 RETAIL
#Command Briefing
#Briefing
$start_briefing
$num_stages: 0
$end_briefing
#Debriefing_info
$Num stages: 0
#Players ;! 1 total
$Starting Shipname: Alpha 1
$Ship Choices: (
"GTF Ulysses" 5
"GTF Hercules" 5
"GTF Hercules Mark II" 5
"GTF Ares" 5
"GTF Erinyes" 5
"GTF Loki" 5
"GTF Pegasus" 5
"GTF Perseus" 5
"GTF Myrmidon" 5
"SF Mara (terrans)" 5
"GTB Artemis" 5
"GTB Artemis D.H." 5
"GTB Medusa" 5
"GTB Ursa" 5
"GTB Zeus" 5
"GTB Boanerges" 5
"GVF Ptah" 5
)
+Weaponry Pool: (
"Subach HL-7" 17
"Subach HL-D" 16
"Mekhu HL-7" 16
"Mekhu HL-7D" 16
"Akheton SDG" 16
"Morning Star" 16
"MorningStar D" 16
"Prometheus R" 17
"Prometheus S" 16
"Prometheus D" 16
"Maxim" 16
"Maxim D" 16
"UD-8 Kayser" 16
"UD-D Kayser" 16
"Circe" 16
"Lamprey" 16
"Rockeye" 500
"Rockeye D" 500
"Tempest" 500
"Tempest D" 500
"Hornet" 500
"Hornet D" 500
"Tornado" 500
"Tornado D" 500
"Harpoon" 516
"Harpoon D" 500
"Trebuchet" 500
"TAG-A" 500
"TAG-B" 500
"Piranha" 500
"Stiletto II" 500
"Infyrno" 500
"Infyrno D" 500
"Cyclops" 500
"Cyclops#short" 500
"Helios" 500
"EMP Adv." 500
)
#Objects ;! 3 total
$Name: Alpha 1 ;! Object #0
$Class: GTF Ulysses
$Team: Friendly
$Location: 0.000000, 0.000000, 0.000000
$Orientation:
1.000000, 0.000000, 0.000000,
0.000000, 1.000000, 0.000000,
0.000000, 0.000000, 1.000000
$AI Behavior: None
$Cargo 1: XSTR("Nothing", -1)
+Initial Velocity: 33
+Initial Hull: 100
+Subsystem: Pilot
$Arrival Location: Hyperspace
$Arrival Cue: ( false )
$Departure Location: Hyperspace
$Departure Cue: ( false )
$Determination: 10
+Flags: ( "cargo-known" "player-start" )
+Flags2: ( )
+Respawn priority: 0
+Group: 0
+Use Table Score:
+Score: 8
$Name: TestShip ;! Object #1
$Class: GTF Ulysses
$Team: Friendly
$Location: -106.270683, 0.000000, 75.915436
$Orientation:
1.000000, 0.000000, 0.000000,
0.000000, 1.000000, 0.000000,
0.000000, 0.000000, 1.000000
$AI Behavior: None
$Cargo 1: XSTR("Nothing", -1)
+Initial Velocity: 33
+Initial Hull: 100
+Subsystem: Pilot
$Arrival Location: Hyperspace
$Arrival Cue: ( key-pressed "1" )
$Departure Location: Hyperspace
$Departure Cue: ( false )
$Determination: 10
+Flags: ( "cargo-known" )
+Flags2: ( )
+Respawn priority: 0
+Use Table Score:
+Score: 8
$Name: NotTestShip ;! Object #2
$Class: GTF Ulysses
$Team: Friendly
$Location: 93.849892, 0.000000, 77.233765
$Orientation:
1.000000, 0.000000, 0.000000,
0.000000, 1.000000, 0.000000,
0.000000, 0.000000, 1.000000
$AI Behavior: None
$Cargo 1: XSTR("Nothing", -1)
+Initial Velocity: 33
+Initial Hull: 100
+Subsystem: Pilot
$Arrival Location: Hyperspace
$Arrival Cue: ( key-pressed "2" )
$Departure Location: Hyperspace
$Departure Cue: ( false )
$Determination: 10
+Flags: ( "cargo-known" )
+Flags2: ( )
+Respawn priority: 0
+Use Table Score:
+Score: 8
#Wings ;! 1 total
$Name: Alpha
$Waves: 1
$Wave Threshold: 0
$Special Ship: 0 ;! Alpha 1
$Arrival Location: Hyperspace
$Arrival Cue: ( true )
$Departure Location: Hyperspace
$Departure Cue: ( false )
$Ships: ( ;! 1 total
"Alpha 1"
)
+Hotkey: 0
+Flags: ( )
#Events ;! 0 total
#Goals ;! 0 total
#Waypoints ;! 0 lists total
#Messages ;! 0 total
#Reinforcements ;! 0 total
#Background bitmaps ;! 0 total
$Num stars: 500
$Ambient light level: 7895160
+Nebula: Nebula01
+Color: Blue
+Pitch: 116
+Bank: 310
+Heading: 148
#Asteroid Fields
#Music
$Event Music: None
$Briefing Music: None
#End
local module = {}
function module.waitForShipAsync(ship_name)
return async.promise(function(resolve, reject) end)
end
return module
local module = {}
local arrivalChecks = {}
local hooksRegistered = false
function registerHooks()
engine.addHook("On Warp In", function()
local arrivedShip = hv.Self
local callbacks = arrivalChecks[arrivedShip.Name]
-- No one is interested in this ship :(
if callbacks == nil then
return
end
for _, v in ipairs(callbacks) do
v.resolve(arrivedShip)
end
arrivalChecks[arrivedShip.Name] = nil
end)
engine.addHook("On Mission End", function()
for _, checks in pairs(arrivalChecks) do
for _, funcs in ipairs(checks) do
funcs.reject("Mission has ended")
end
end
end)
hooksRegistered = true
end
function module.waitForShipAsync(ship_name)
local existingShip = mn.Ships[ship_name]
if existingShip:isValid() then
-- Ship is already in mission, immediately resolve the promise
return async.resolved(existingShip)
end
if not hooksRegistered then
registerHooks()
end
-- Ship isn't here yet.
-- TODO: Use the ship status caching from the engine to check if it may already have departed
return async.promise(function(resolve, reject)
local existingList = arrivalChecks[ship_name] or {}
table.insert(existingList, { resolve=resolve, reject=reject })
arrivalChecks[ship_name] = existingList
end)
end
return module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment