PathListDir: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
(Improve example code)
Line 22: Line 22:
This example loads all models from a certain directory
This example loads all models from a certain directory
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local function string:endswith(ending) -- from https://gist.github.com/kgriffs/124aae3ac80eefe57199451b823c24ec
-- from https://gist.github.com/kgriffs/124aae3ac80eefe57199451b823c24ec
local function string:endswith(ending)
return ending == "" or self:sub(-#ending) == ending
return ending == "" or self:sub(-#ending) == ending
end
end


-- get all files from a models directory that exists in the resource root folder (resources/ResourceName)
-- and load them into the game
addEventHandler('onClientResourceStart', resourceRoot, function()
addEventHandler('onClientResourceStart', resourceRoot, function()
-- get all files from a models directory that exists in the resource root folder (resources/ResourceName)
local entries = pathListDir('models') or {}
local files = pathListDir('/models')
for _, fileOrFolder in ipairs(entries) do
for _,file in ipairs(files) do
        if pathIsFile(fileOrFolder) then
local filePath = 'models/'..file
            local file = fileOrFolder
local modelName = tonumber(file:sub(1, -5)) or 0
            local modelName = tonumber(file:sub(1, -5))
            if modelName then
                -- the full path to the file
                local filePath = 'models/'..file


if file:endswith('.col') then
                if file:endswith('.col') then
local colData = engineLoadCOL('models/'..file)
                    local colData = engineLoadCOL(filePath)
 
                    if colData then
if colData then
                        engineReplaceCOL(colData, modelName)
engineReplaceCOL(colData, modelName)
                    end
end
                end
end
                if file:endswith('.txd') then
if file:endswith('.txd') then
                    local txdData = engineLoadTXD(filePath)
local txdData = engineLoadTXD('models/'..file)
                    if txdData then
 
                        engineImportTXD(txdData, modelName)
if txdData then
                    end
engineImportTXD(txdData, modelName)
                end
end
                if file:endswith('.dff') then
end
                    local dffData = engineLoadDFF(filePath)
if file:endswith('.dff') then
                    if dffData then
local dffData = engineLoadDFF('models/'..file)
                        engineReplaceModel(dffData, modelName)
 
                    end
if dffData then
                end
engineReplaceModel(dffData, modelName)
            end
end
        end
end
end
end
end)
end)

Revision as of 11:42, 23 July 2024

BETA: NEW FEATURE (BUILD: 1.6.0 r22470)

Reads a specified directory and returns all entries inside of it. These entries can be file or folder names.

Syntax

table pathListDir ( string path )

OOP Syntax Help! I don't understand this!

Method: path:listDir(...)


Required Arguments

  • path: A string containing a path you want to get entries from

Returns

Returns table with all entries in a specified directory.

Example

Click to collapse [-]
Client

This example loads all models from a certain directory

-- from https://gist.github.com/kgriffs/124aae3ac80eefe57199451b823c24ec
local function string:endswith(ending)
	return ending == "" or self:sub(-#ending) == ending
end

-- get all files from a models directory that exists in the resource root folder (resources/ResourceName)
-- and load them into the game
addEventHandler('onClientResourceStart', resourceRoot, function()
	local entries = pathListDir('models') or {}
	for _, fileOrFolder in ipairs(entries) do
        if pathIsFile(fileOrFolder) then
            local file = fileOrFolder
            local modelName = tonumber(file:sub(1, -5))
            if modelName then
                -- the full path to the file
                local filePath = 'models/'..file

                if file:endswith('.col') then
                    local colData = engineLoadCOL(filePath)
                    if colData then
                        engineReplaceCOL(colData, modelName)
                    end
                end
                if file:endswith('.txd') then
                    local txdData = engineLoadTXD(filePath)
                    if txdData then
                        engineImportTXD(txdData, modelName)
                    end
                end
                if file:endswith('.dff') then
                    local dffData = engineLoadDFF(filePath)
                    if dffData then
                        engineReplaceModel(dffData, modelName)
                    end
                end
            end
        end
	end
end)

See Also