Multiple .lua files, error handling and common functions

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
PiFace
Prole
Posts: 28
Joined: Mon Sep 05, 2016 5:35 pm
Location: Brazil

Multiple .lua files, error handling and common functions

Post by PiFace »

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
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Multiple .lua files, error handling and common functions

Post by Beelz »

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. :P

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
pgimeno
Party member
Posts: 3640
Joined: Sun Oct 18, 2015 2:58 pm

Re: Multiple .lua files, error handling and common functions

Post by pgimeno »

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.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Multiple .lua files, error handling and common functions

Post by ivan »

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
User avatar
PiFace
Prole
Posts: 28
Joined: Mon Sep 05, 2016 5:35 pm
Location: Brazil

Re: Multiple .lua files, error handling and common functions

Post by PiFace »

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.
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.
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.

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 .-.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Multiple .lua files, error handling and common functions

Post by Beelz »

Simple example:
* 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()

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests