PathListDir: Difference between revisions
Jump to navigation
Jump to search
Fernando187 (talk | contribs) (Improve example code) |
Fernando187 (talk | contribs) mNo edit summary |
||
| Line 23: | Line 23: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- from https://gist.github.com/kgriffs/124aae3ac80eefe57199451b823c24ec | -- from https://gist.github.com/kgriffs/124aae3ac80eefe57199451b823c24ec | ||
local function | local function stringEndsWith(str, ending) | ||
return ending == "" or | return ending == "" or str:sub(-#ending) == ending | ||
end | end | ||
| Line 39: | Line 39: | ||
local filePath = 'models/'..file | local filePath = 'models/'..file | ||
if file | if stringEndsWith(file, '.col') then | ||
local colData = engineLoadCOL(filePath) | local colData = engineLoadCOL(filePath) | ||
if colData then | if colData then | ||
| Line 45: | Line 45: | ||
end | end | ||
end | end | ||
if file | if stringEndsWith(file, '.txd') then | ||
local txdData = engineLoadTXD(filePath) | local txdData = engineLoadTXD(filePath) | ||
if txdData then | if txdData then | ||
| Line 51: | Line 51: | ||
end | end | ||
end | end | ||
if file | if stringEndsWith(file, '.dff') then | ||
local dffData = engineLoadDFF(filePath) | local dffData = engineLoadDFF(filePath) | ||
if dffData then | if dffData then | ||
| Line 60: | Line 60: | ||
end | end | ||
end | end | ||
end) | end, false) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | </section> | ||
Revision as of 11:46, 23 July 2024
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