DriFtyZ

[BUG] Error creating variables based on Lua table indexes

Recommended Posts

Hello, i am trying to create a vehicle loader script that loads all the vehicle models from a Lua table instead of adding them manually one by one but i am getting "error unexpected symbol near .."
Now i know my syntax is wrong in line 18 and 20 where i try to create a variable based from the name index on the Lua table but i don't know how to accomplish it. Any help would be appreciated. Thanks
 

local vehiclesFileNames = {    
    AE86                = 589,
    ToyotaSupra         = 559,
    ToyotaAltezza   	= 402,
    Datsun240Z	        = 475,
    Nissan180SX 		= 602,   
	SilviaS13	        = 401,
    SilviaS14	        = 576,
    SilviaS15	        = 474,
    Skyline2000	        = 535,
    SkylineR32	        = 555,
    SkylineR34          = 562,
    SkylineGTR 		    = 558,
}

function loadVehicleFiles()
    for name, id in ipairs(vehiclesFileNames) do
        local name..txd = engineLoadTXD ( "Cars/"..name.."/"..name..".txd")
        engineImportTXD ( name..txd, id )
        local name..dff = engineLoadDFF ( "Cars/"..name.."/"..name..".dff")
        engineReplaceModel ( name..dff, id )
        outputDebugString("name = "..name.."  |  id = "..id..".")
    end
end

addEventHandler("onClientResourceStart", resourceRoot, loadVehicleFiles())

 

Share this post


Link to post

You can't create a local variables dynamically, those need to be typed. You can however do it with global variables like this, because you can access them in a table

_G[name.."dff"] = engineLoadTXD ( "Cars/"..name.."/"..name..".txd")

Also you need to use pairs for that loop to work.

And on line 26, you need to use the function name.

I suggest doing it this way in your own table

local vehiclesFileNames = {    
    AE86                = 589,
    ToyotaSupra         = 559,
    ToyotaAltezza   	= 402,
    Datsun240Z	        = 475,
    Nissan180SX 		= 602,   
	SilviaS13	        = 401,
    SilviaS14	        = 576,
    SilviaS15	        = 474,
    Skyline2000	        = 535,
    SkylineR32	        = 555,
    SkylineR34          = 562,
    SkylineGTR 		    = 558,
}
local vehiclesData = {}

function loadVehicleFiles()
    for name, id in pairs(vehiclesFileNames) do
        local txd = engineLoadTXD ( "Cars/"..name.."/"..name..".txd")
        engineImportTXD ( txd, id )
        local dff = engineLoadDFF ( "Cars/"..name.."/"..name..".dff")
        engineReplaceModel ( dff, id )
        outputDebugString("name = "..name.."  |  id = "..id..".")
		
        vehiclesData[name] = {}
        vehiclesData[name].txd = txd
        vehiclesData[name].dff = dff
    end
end
addEventHandler("onClientResourceStart", resourceRoot, loadVehicleFiles)

 

 

Share this post


Link to post
On 06/11/2019 at 03:38, Zango said:

You can't create a local variables dynamically, those need to be typed. You can however do it with global variables like this, because you can access them in a table


_G[name.."dff"] = engineLoadTXD ( "Cars/"..name.."/"..name..".txd")

Also you need to use pairs for that loop to work.

And on line 26, you need to use the function name.

I suggest doing it this way in your own table


local vehiclesFileNames = {    
    AE86                = 589,
    ToyotaSupra         = 559,
    ToyotaAltezza   	= 402,
    Datsun240Z	        = 475,
    Nissan180SX 		= 602,   
	SilviaS13	        = 401,
    SilviaS14	        = 576,
    SilviaS15	        = 474,
    Skyline2000	        = 535,
    SkylineR32	        = 555,
    SkylineR34          = 562,
    SkylineGTR 		    = 558,
}
local vehiclesData = {}

function loadVehicleFiles()
    for name, id in pairs(vehiclesFileNames) do
        local txd = engineLoadTXD ( "Cars/"..name.."/"..name..".txd")
        engineImportTXD ( txd, id )
        local dff = engineLoadDFF ( "Cars/"..name.."/"..name..".dff")
        engineReplaceModel ( dff, id )
        outputDebugString("name = "..name.."  |  id = "..id..".")
		
        vehiclesData[name] = {}
        vehiclesData[name].txd = txd
        vehiclesData[name].dff = dff
    end
end
addEventHandler("onClientResourceStart", resourceRoot, loadVehicleFiles)

 

 

thanks for the answer i realized the variables in pairs get replaced every time so i just named them dff and txd also my second fault was for using ipairs when i wanted both indexes of the table so changing ipairs to pairs worked and now the script is working good.

Share this post


Link to post

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.