Okay I'll try that. But I think I use the event "onGamemodeMapStart" to get the map root to create objects from custom map elements.
Is there a way I can get the main map root from inside another resource?
I have my main gamemode, GTC. Inside the GTC resource I use "onGamemodeMapStart" to get the map root, which is then stored in the variable MAP_ROOT, then I export a function called 'getMapRoot()' which I'm trying to call from another resource, 'gtc_sprayshops', so I can get all 'sprayshop' elements from the map and create markers for them.
But I can't seem to use the map root passed by that function.
When I try this:
gtc_sprayshops > sprayshops_s.lua
-- Loads and creates the pay n spray shops when the resource is started.
--
addEventHandler("onResourceStart", resourceRoot,
function ()
local sprayshops = getElementsByType("sprayshop", exports.GTC:getMapRoot())
for _, sprayshop in pairs(sprayshops) do
local x = getElementData(sprayshop, "posX")
local y = getElementData(sprayshop, "posY")
local z = getElementData(sprayshop, "posZ")
local sprayMark = createMarker(x,y,z, "cylinder", 4, 110,140,200, 200)
setElementData(sprayMark, "markType", "sprayshop")
createBlip(x,y,0, 63, 2, 255,0,0, 255, 0, 400.0)
end
end
)
It gives me this error:
[2015-03-23 15:05:24] WARNING: [GTC]\gtc_sprayshops\sprayshops_s.lua:12: Bad argument @ 'getElementsByType' [Expected element at argument 2, got table]
[2015-03-23 15:05:24] ERROR: [GTC]\gtc_sprayshops\sprayshops_s.lua:14: bad argument #1 to 'pairs' (table expected, got boolean)
This is how I get the map root in the gamemode resource:
GTC > gtc_s.lua
MAP_ROOT = { }
-- [EXPORTED] Allows other resources to access the map root.
--
function getMapRoot()
return MAP_ROOT
end
-- Loads up all map related data and create the needed elements.
--
addEventHandler("onGamemodeMapStart", root,
function (startedMap)
MAP_ROOT = getResourceRootElement(startedMap)
-- Freezes all peds/clerks that are present in the map.
local peds = getElementsByType("ped", MAP_ROOT)
local clerks = getElementsByType("clerk", MAP_ROOT)
for _, ped in ipairs(peds) do setElementFrozen(ped, true) end
for _, clerk in ipairs(clerks) do setElementFrozen(clerk, true) end
end
)
I don't want to create all my Pay n Spray markers, car spawn markers, etc, in my gamemode resource. I want those objects to be created by the resources that deal with them, but I've tried many things attempting to get the map root from outside the gamemode resource and I always get this error.
How can I load all "sprayshop" elements from the map root in the resource "gtc_sprayshops"?