PathListDir: Difference between revisions
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...") |
No edit summary |
||
| (9 intermediate revisions by 4 users not shown) | |||
| Line 2: | Line 2: | ||
{{Shared function}} | {{Shared function}} | ||
{{ | {{New feature/item|3.161|1.6.0|22470| | ||
Reads a specified directory and returns all entries inside of it. | Reads a specified directory and returns all entries inside of it. These entries can be file or folder names. | ||
}} | |||
{{Note| | |||
* Listing other resource directory can be done by passing '''":resourceName/."''' | |||
* For listing current resource (the one where code is executed), you can pass either '''""''' or '''":currentResourceName/."''' (preferably use first approach, because the latter will require removing this part, once result is ready) | |||
}} | }} | ||
| Line 22: | Line 26: | ||
This example loads all models from a certain directory | This example loads all models from a certain directory | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- from https://gist.github.com/kgriffs/124aae3ac80eefe57199451b823c24ec | |||
local function stringEndsWith(str, ending) | |||
return ending == "" or str: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() | addEventHandler('onClientResourceStart', resourceRoot, function() | ||
local entries = pathListDir('models') or {} | |||
local | for _, fileOrFolder in ipairs(entries) do | ||
for _, | if pathIsFile(fileOrFolder) then | ||
local | local file = fileOrFolder | ||
local modelName = tonumber(file:sub(1, -5)) | |||
if modelName then | |||
-- the full path to the file | |||
local filePath = 'models/'..file | |||
if | |||
if stringEndsWith(file, '.col') then | |||
local colData = engineLoadCOL(filePath) | |||
if colData then | |||
engineReplaceCOL(colData, modelName) | |||
end | |||
end | |||
if stringEndsWith(file, '.txd') then | |||
local txdData = engineLoadTXD(filePath) | |||
if txdData then | |||
engineImportTXD(txdData, modelName) | |||
end | |||
end | |||
if stringEndsWith(file, '.dff') then | |||
local dffData = engineLoadDFF(filePath) | |||
if dffData then | |||
engineReplaceModel(dffData, modelName) | |||
end | |||
end | |||
end | end | ||
end | end | ||
end | end | ||
end) | end, false) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | </section> | ||
==See Also== | ==See Also== | ||
{{ | {{Shared_path_functions}} | ||
Latest revision as of 16:20, 27 April 2025
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 [-]
ClientThis example loads all models from a certain directory
-- from https://gist.github.com/kgriffs/124aae3ac80eefe57199451b823c24ec
local function stringEndsWith(str, ending)
return ending == "" or str: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 stringEndsWith(file, '.col') then
local colData = engineLoadCOL(filePath)
if colData then
engineReplaceCOL(colData, modelName)
end
end
if stringEndsWith(file, '.txd') then
local txdData = engineLoadTXD(filePath)
if txdData then
engineImportTXD(txdData, modelName)
end
end
if stringEndsWith(file, '.dff') then
local dffData = engineLoadDFF(filePath)
if dffData then
engineReplaceModel(dffData, modelName)
end
end
end
end
end
end, false)
See Also