Jump to content

attempt to call global 'engineRequestModel'


dqlepy

Recommended Posts

local vehicles = {}
function createAllocatedVehicle( thePlayer, command, objectModel )
    local x, y, z = getElementPosition(thePlayer)
    local id = engineRequestModel("vehicle", 400)
    vehicles[id] = createVehicle(id, x+0.5, y, z+0.5)
    outputChatBox("New vehicle with ID "..id.." created.")
end
addCommandHandler("cav", createAllocatedVehicle, false, false)

function skinAllocatedVehicles()
    local txd, dff;
    for id,vehicle in pairs(vehicles) do
        if fileExists("vehicles/" .. id .. ".txd") and fileExists("vehicles/" .. id .. ".dff") then
            txd = engineLoadTXD("vehicles/" .. id .. ".txd")
            engineImportTXD(txd, id)
            dff = engineLoadDFF("vehicles/" .. id .. ".dff")
            engineImportDFF(dff, id)
            outputChatBox("Model ID "..id.." changed correctly.")
        else
            outputChatBox("Model ID "..id.." couldn't change. REASON: vehicles/".. id ..".txd or vehicles/".. id ..".dff does not exist.")
        end
    end
end
addCommandHandler("sav", skinAllocatedVehicles, false, false)

function onStop()
    for id,vehicle in pairs(vehicles) do
        engineFreeModel(id)
    end
end
addEventHandler("onClientResourceStop", resourceRoot, onStop)

ERROR: createVehicle\global.lua:4: attempt to call global 'engineRequestModel' (a nil value)

every time I input cav in chat ( create allocated vehicle ) it spurts out an ERROR, the one above..

https://wiki.multitheftauto.com/wiki/EngineRequestModel

reference site, used to make this script, at the top it says " After release 1.5.8 r20716 this function supports "vehicle" and "object" too. " so I tried to recreate it with vehicles, because that's what I need, but it didn't really work out.. Any help appreciated..

Edited by dqlepy
Link to comment

Reference site also mentions that this function is client-only, with the big red "Client-side function" text on the top-right of the page. This means you cannot use this function in a server-side script. "global.lua" is, I assume, both client and server: the server's debug log is telling you that this function is not defined (i.e. is a nil value).

Link to comment
4 minutes ago, Addlibs said:

Reference site also mentions that this function is client-only, with the big red "Client-side function" text on the top-right of the page. This means you cannot use this function in a server-side script. "global.lua" is, I assume, both client and server: the server's debug log is telling you that this function is not defined (i.e. is a nil value).

okay yea, you replied to my issue, as I was typing.. basically, I realised my mistake, made it main.lua, fixed up the meta.xml, and now, it works with vehicles, but.. it spawns the same vehicle, and the ID's are always the same, plus you cannot enter the vehicle, or damage it..

spacer.png

spacer.png

the cars do not respond to anything, like trying to enter it, damage, nothing, plus it spawns one model only, I'd like to be able to make multiple ID's of singular SA Models, like one ID, lets say, " 401 " and make it so lets say random_IDs = 401, with 401 being bravura, so you can set .txd and .dff to random_IDs, and it changes random_IDs each car to each .txd and .dff files, with 401 being unaffected but, random_IDs being the same SA model as 401.. I hope you get my point.. like make bravura x5, each having their own ID " F.I. 1996, 1997, 1998, 1999, 2000 " and being able to set .dff, .txd to each of them..

Link to comment

Unable to enter

You cannot enter the vehicles, because they are client-side only vehicles -- as it says on the reference wiki, "Note: Vehicles (and other elements) created client-side are only seen by the client that created them, aren't synced and players cannot enter them. They are essentially for display only."

To solve this, you need to spawn the vehicles on the server side, preferably with the parent ID as the model, then send information to all clients (and remember the vehicle's model for newer clients connecting later on) that the client-side model ID needs to be changed, and setElementModel client-side over that vehicle. The easiest way to do this is with element data: setElementData on the server, onClientElementDataChange and onClientElementStreamIn with getElementData on the client.

Models not changing

You seem to be allocating a new model ID every time you want to spawn a vehicle, rather than reusing an ID which has been assigned a modded DFF and TXD. To solve this, you should be using engineRequestModel in the same function you load the mods, that is, where engineImportTXD and engineReplaceModel are.

For example,

-- vehicles = {
--   ["txd_and_dff_name_with_ext"] = {parentID = 429, dynamicID = nil --[[ this entry gets filled at runtime ]]}
-- }


function allocateAndSkinVehicles() -- renamed function to better reflect its new purpose
  local txd, dff;
  for id,vehicle in pairs(vehicles) do
    if fileExists("vehicles/" .. id .. ".txd") and fileExists("vehicles/" .. id .. ".dff") then
      local modelid = engineRequestModel("vehicle", vehicle.parentID) -- request a new ID for this vehicle
      txd = engineLoadTXD("vehicles/" .. id .. ".txd")
      engineImportTXD(txd, id)
      dff = engineLoadDFF("vehicles/" .. id .. ".dff")
      engineImportDFF(dff, id)
      outputChatBox("Model ID "..id.." changed correctly.")
      vehicle.dynamicID = id -- save the allocated ID to the vehicle's entry
    else
      outputChatBox("Model ID "..id.." couldn't change. REASON: vehicles/".. id ..".txd or vehicles/".. id ..".dff does not exist.")
    end
  end
end
addEventHandler("onClientResourceStart", resourceRoot, allocateAndSkinVehicles, false) -- changed triggering to happen on resource start

This way, when the server tells you to change a vehicle's client-side model, either by element data or though events, you need to look up vehicles[customID].dynamicID to get the actual allocated model ID with the particular modification on the vehicle. Note this implementation allows you to use strings for customID, doesn't have to be a number; all it requires is there are .txd and .dff files with that exact name in the ./vehicles directory.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...