Using modules that return two instances of a class

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
interested_party
Prole
Posts: 13
Joined: Sun Mar 30, 2025 5:00 am

Using modules that return two instances of a class

Post by interested_party »

I'm running into an issue that really is throwing me for a loop. My code is ugly and I'll only share some snippets to give the general issue clarity. This is for basic card game mechanics. I have a class that defines a Hand, and the module returns two instances of this class (player1Hand and player2Hand).

Code: Select all

local Class = require("hump.class")
local love = require("love")

local hand = Class{
    init = function(self)
        self.x = 0
        self.y = 760
        self.width = 1920
        self.length = 320
        self.cards = {}
        self.numCards = 0
    end
}

local player1Hand = hand()
local player2Hand = hand()
player2Hand.y = 0

return player1Hand, player2Hand
There are some methods in this class as well but I've omitted them as they are not the source of my issue. What I can't figure out is that when I require these Hands in my gamestate, player1Hand works perfectly normal as intended, but if I copy the exact same code and sub player2Hand in it throws the error "attempt to index upvalue "player2Hand" (a nil value).

Code: Select all

local player1Hand, player2Hand = require("Elements/Hand")

function play:update()
    player1.x, player1.y = love.mouse.getPosition()
    for i = 1, player1Hand.numCards do
        player1Hand.cards[i]:isHovered(player1.x, player1.y)
        player1Hand.cards[i]:move(player1.x, player1.y)

    end
    --for i = 1, player2Hand.numCards do
    --player2Hand.cards[i]:isHovered(player1.x, player1.y)
    --player2Hand.cards[i]:move(player1.x, player1.y)

    --end
end
Again I've omitted a lot of irrelevant code. When the lower for loop is commented out, the program runs fine. But it throws that error when it's included. I'm at a loss because both instances of the Hand class are identical and are implemented the same way.
interested_party
Prole
Posts: 13
Joined: Sun Mar 30, 2025 5:00 am

Re: Using modules that return two instances of a class

Post by interested_party »

I am an idiot. Sorry I posted this before taking adequate time to think about it. Just realized that a module can't return multiple values unless they are in a table. Simple fix, just return the two hands in a table and extract them into local variables in the gamestate module. If anyone is as dumb as me there is your answer.
User avatar
pgimeno
Party member
Posts: 3713
Joined: Sun Oct 18, 2015 2:58 pm

Re: Using modules that return two instances of a class

Post by pgimeno »

Modules typically define classes. Returning instances is somewhat ugly.

Anyway, a long time ago, I was told a trick to return multiple values from a module: return a function that returns these values. In your case, it would involve replacing the last line with this:

Code: Select all

return function() return player1Hand, player2Hand end
and instead of just require("Elements/Hand"), you'd add an extra couple parens to invoke the function:

Code: Select all

local player1Hand, player2Hand = require("Elements/Hand")()
interested_party
Prole
Posts: 13
Joined: Sun Mar 30, 2025 5:00 am

Re: Using modules that return two instances of a class

Post by interested_party »

Ah that makes even more sense than a table solution. Appreciate it! I'm definitely still learning the do's and don't's!
User avatar
zorg
Party member
Posts: 3480
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Using modules that return two instances of a class

Post by zorg »

An alternative solution if it's warranted, is to use love.filesystem.load instead of require, but while the former supports multiple return values, only the latter supports caching of the module required, meaning that loading a file multiple times *will* create separate instances of it, while requiring it won't.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], MrFariator and 6 guests