News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: Lua Tutorial: Check If a File Exists in Directory  (Read 4004 times)

0 Members and 1 Guest are viewing this topic.

Offline Altamurenza

  • Full Member
  • ***
  • Posts: 118
  • Gender: Male
  • I love cheat, unique, realistic, & expansion mod.
    • View Profile
Lua Tutorial: Check If a File Exists in Directory
« on: April 07, 2022, 03:25:42 PM »
- INTRODUCTION -

Hey, back with me again in another thread of my Lua tutorial.
As you expect from the title. On this occasion, i will tell you about how can we check if a file exists or not in our beloved game Bully.

If you guys are familiar with Lua scripting, checking a file existence is probably easy with io.open function provided by Lua standard library. But, since Bully use a modified-standard library of Lua where I/O facilities do not exists, so we cannot access all I/O functions such as io.open, io.read, io.write, and many more io. So.. we're going to use loadfile function in order to check if a file exists or not.

This tutorial may become obsolete for PC version in the future because some people are going to reimplement the Lua 5.0.2 standard library into Bully. However, this method maybe useful for those who want to mod in traditional way without mod dependencies or additional tools to check a file.

- CHECK IF A FILE EXISTS -

Let's be honest here, this is not really tutorial lol. I think, it's more like resource or function library.
I made a custom function based on loadfile and i will demonstrate how to use it.

1) 'FILE_EXISTS' FUNCTION
Code: [Select]
FILE_EXISTS = function(PATH, NAME, LUA)
local FILE, INFO = loadfile(PATH..NAME)
local MESSAGE = "'"..NAME.."'"

if FILE then
MESSAGE = MESSAGE.." exists!"
else
MESSAGE = MESSAGE.." "..(string.find(INFO, "parser", 1) and "exists"..(LUA and ", but not compiled!" or "!") or "does not exist!")
end

return MESSAGE
end

Function arguments:
    PATH = string, file directory (e.g. "Scripts/")
    NAME = string, file name (e.g. "Scripts.img" or "RESOURCE.lur")
    LUA = boolean, the file you want to check is Lua script or not (e.g. true or false)

2) EXAMPLE OF USE
Check if Lua script exists:
Code: [Select]
local MSG = FILE_EXISTS("Scripts/", "RESOURCE.lur", true)
TextPrintString(MSG, 4, 1)
Check if Scripts.img exists:
Code: [Select]
local MSG = FILE_EXISTS("Scripts/", "Scripts.img", false)
TextPrintString(MSG, 4, 1)
Check if Trigger.img exists:
Code: [Select]
local MSG = FILE_EXISTS("DAT/", "Trigger.img", false)
TextPrintString(MSG, 4, 1)
Check if Music.bin exists:
Code: [Select]
local MSG = FILE_EXISTS("audio/PLAYLIST/", "Music.bin", false)
TextPrintString(MSG, 4, 1)
Check if PropHXDs.dat exists:
Code: [Select]
local MSG = FILE_EXISTS("Config/dat/", "PropHXDs.dat", false)
TextPrintString(MSG, 4, 1)
Check if Bully.exe exists:
Code: [Select]
local MSG = FILE_EXISTS("", "Bully.exe", false)
TextPrintString(MSG, 4, 1)

- CLOSING STATEMENT -

This is my third tutorial in Bully-Board, i hope i'll be able to share some more experiences soon.
Make sure you read all the explanations carefully to avoid any confusion.

Check out my other tutorials:
- Lua Tutorial: Exceeding 112KB Script Limit
- Lua Tutorial: Non-STimeCycle Freeroam Mods
« Last Edit: April 07, 2022, 03:34:49 PM by Altamurenza »