Before telling what my questions actually are, here's some explanation about the game I'm making:
- It's a card game, so there are common rules to every card, but they have their particular effects.
- Not every card needs a separate file for their effect, because some of them don't have any.
Now, what I need some help with:
1. I've been thinking of putting each card "code" in a file, containg all specific effects/conditions/rules. Running those files is easy, I just have to use require and keep it somewhere with the card id or in a table. If one of those files pop an error, the game stops, as expected, but that's not good, there should be a way to ignore the error, stop only that specific card from working, print that error in a log or something (to help debugging) and let the game move on. The abscence of a file should also be ignored. How can this be done?
2. Each card is unique, but they all obey a certain group of rules. These rules could be in another file to be run later, but is there a way to make these functions available to every card without using require on every single file?
Any advice or suggestion would be helpful Thank you
Multiple .lua files, error handling and common functions
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Multiple .lua files, error handling and common functions
1a. For error handling, you can tailor love.errhand to suit your needs and include it in main.lua.
1b. With the problem of absent files, you could instead enumerate all files within a specified directory containing only the cards' data files.
2. Not exactly sure what you mean by this, but I'll take a stab at it... You can setup your card as a "class" and inherit functions, etc. off a card template so to speak.
Sorry, short on time or I'd try to help more.
1b. With the problem of absent files, you could instead enumerate all files within a specified directory containing only the cards' data files.
2. Not exactly sure what you mean by this, but I'll take a stab at it... You can setup your card as a "class" and inherit functions, etc. off a card template so to speak.
Sorry, short on time or I'd try to help more.
Re: Multiple .lua files, error handling and common functions
You can also pcall(require, modulename) to protect it from crashing. Check [manual]pcall[/manual] in the manual, it returns whether it worked as the first return value.
Re: Multiple .lua files, error handling and common functions
Yep, pcall is the 'standard' Lua solution for handling errors but it should be used with caution.
pcall will capture ANY type of error, so I don't think it's well suited for dealing with game logic.
The problem is, there is no generic way to handle ANY type of Lua error apart from trying to reset the game and even that is not very practical.
For example, there is no sensible way to recover from: 'love.graphics = nil'
My advice would be to look into the 'strict.lua' library and to include ALL of your scripts during development
pcall will capture ANY type of error, so I don't think it's well suited for dealing with game logic.
The problem is, there is no generic way to handle ANY type of Lua error apart from trying to reset the game and even that is not very practical.
For example, there is no sensible way to recover from: 'love.graphics = nil'
My advice would be to look into the 'strict.lua' library and to include ALL of your scripts during development
Re: Multiple .lua files, error handling and common functions
pcall is actually a good solution, I can even print the message on a console inside the game. This is useful because some user typed scripts are expected.
I'm kinda short on time now (actually I couldn't do anything on the game these days, that's why I hadn't replied earlier) so I can't explain it better .-.
Yeah, I wasnt clear, what I meant was: to allow the card scripts to be more readable and to avoid using require on every one of them, I need some default functions to be available to every card. You suggested putting inside the card class, but I'm not sure how they would be accessed.Beelz wrote:2. Not exactly sure what you mean by this, but I'll take a stab at it... You can setup your card as a "class" and inherit functions, etc. off a card template so to speak.
I'm kinda short on time now (actually I couldn't do anything on the game these days, that's why I hadn't replied earlier) so I can't explain it better .-.
Re: Multiple .lua files, error handling and common functions
Simple example:
* Disclaimer: Written on phone, not tested.
* Disclaimer: Written on phone, not tested.
Code: Select all
local cards = {}
function cards:loadAll()
-- Put all cards into a folder
local path = 'Folder/Containing/Card/Files'
local files= love.filesystem.getDirectoryItems(path)
for k, file in ipairs(files) do
local name = string.sub(file, 1, string.len(file) - 4) -- get reference name from file name
local pass, card = pcall(require, path..'.'..name)
if pass then
cards[name] = card
print('Pass:\t'..name)
else
print('Fail:\t'..name)
end
end
end
function cards:exists(name)
if cards[name] then return true end
end
return cards, cards:loadAll()
Who is online
Users browsing this forum: Bing [Bot] and 8 guests