PathListDir: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Shared function}} {{Added feature/item|1.6.1|1.6.0|22470| Reads a specified directory and returns all entries inside of it. }} ==Syntax== <syntaxhighlight lang="lua"> table pathListDir ( string path ) </syntaxhighlight> {{OOP||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== <section name="Client" clas...")
 
 
(One intermediate revision by one other user not shown)
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
return ending == "" or self:sub(-#ending) == ending
end
addEventHandler('onClientResourceStart', resourceRoot, function()
addEventHandler('onClientResourceStart', resourceRoot, function()
    -- get all files from a models directory that exists in the resource root folder (resources/ResourceName)
-- get all files from a models directory that exists in the resource root folder (resources/ResourceName)
    local files = pathListDir('/models')
local files = pathListDir('/models')
    for _,file in ipairs(files) do
for _,file in ipairs(files) do
        local filePath = 'models/'..file
local filePath = 'models/'..file
        local modelName = tonumber(file:sub(1, -5)) or 0
local modelName = tonumber(file:sub(1, -5)) or 0


        if file:endsWith('.col') then
if file:endswith('.col') then
            local colData = engineLoadCOL('models/'..file)
local colData = engineLoadCOL('models/'..file)


            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('models/'..file)
local txdData = engineLoadTXD('models/'..file)


            if txdData then
if txdData then
                engineImportTXD(txdData, modelName)
engineImportTXD(txdData, modelName)
            end
end
        end
end
        if file:endsWith('.dff') then
if file:endswith('.dff') then
            local dffData = engineLoadDFF('models/'..file)
local dffData = engineLoadDFF('models/'..file)


            if dffData then
if dffData then
                engineReplaceModel(dffData, modelName)
engineReplaceModel(dffData, modelName)
            end
end
        end
end
    end
end
end)
end)
</syntaxhighlight>
</syntaxhighlight>
Line 56: Line 60:


==See Also==
==See Also==
{{Path functions}}
{{Shared_path_functions}}

Latest revision as of 22:21, 1 June 2024

BETA: NEW FEATURE (BUILD: 1.6.0 r22470)

Reads a specified directory and returns all entries inside of it.

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

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

addEventHandler('onClientResourceStart', resourceRoot, function()
	-- get all files from a models directory that exists in the resource root folder (resources/ResourceName)
	local files = pathListDir('/models')
	for _,file in ipairs(files) do
		local filePath = 'models/'..file
		local modelName = tonumber(file:sub(1, -5)) or 0

		if file:endswith('.col') then
			local colData = engineLoadCOL('models/'..file)

			if colData then
				engineReplaceCOL(colData, modelName)
			end
		end
		if file:endswith('.txd') then
			local txdData = engineLoadTXD('models/'..file)

			if txdData then
				engineImportTXD(txdData, modelName)
			end
		end
		if file:endswith('.dff') then
			local dffData = engineLoadDFF('models/'..file)

			if dffData then
				engineReplaceModel(dffData, modelName)
			end
		end
	end
end)

See Also