Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: corrected code sample

...

To be able to call FFI-functions, the following header should be placed at the top of the Lua script:

Code Block
languagelua
local ffi = require("ffi")
local C = ffi.C
ffi.cdef[[
   -- add FFI-functions here
]]

...

A call to the actual function then looks like this

Code Block
languagelua
local component = C.GetPickedMapComponent(myholomap) -- note the "C." prefix here which refers to the local C = ffi.C specified in the header
[...]
local details = C.GetOwnerDetails(componentID)
local faction = ffi.string(details.factionName)
local icon = ffi.string(details.factionIcon

...

In some cases you don't know what kind of ID you are being passed. To determine whether a variable is an FFI-ID, you can use the follow code

 

Code Block
languagelua
-- ffi setuplocalsetup
local ffi = require("ffi")
ffi.cdef[[
    typedef uint64_t UniverseID;
]]

[...]
local myUniverseID = [...]
[...]
local ffiIDType = ffi.typeof("UniverseID")
local isFFIID = ffi.istype(ffiIDType, myUniverseID)
 
-- isFFIID will be true, if myUniverseID is an FFIID, otherwise this will be false

 

In exceptional cases you might also have to create FFI datatypes directly in the Lua script. Normally this is not necessary, since passing certain Lua values to FFI will be converted to the appropriate type directly. However, in cases where you have to construct objects of an FFI type, you can do so using ffi.new

 

Code Block
languagelua
local myFFIObject = ffi.new("UniverseID", 123)

 

The code above will create an object of type UniverseID and initialize it with the value of 123.