Passing a table as a function argument

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
pwniknoobz
Prole
Posts: 11
Joined: Fri Jul 16, 2010 3:10 pm

Passing a table as a function argument

Post by pwniknoobz »

I am trying to pass a table as a function argument, and reference a member variable of the given table in the function to which it's being passed.

Such as this:

Code: Select all

require"game_object"
require"map_object"

function love.load()
   --CREATE NEW OBJECTS
   gameRisk = Game:new()
   mapNewMap = Map:new()
   
   -- SET INITIAL OBJECT VARIABLES
   gameRisk:setPlayers(4)
   mapNewMap:assignTerritories(gameRisk)
end

function love.draw()

if gameRisk.players > 0 then
    love.graphics.print("Hello World".." "..gameRisk.players, 400, 300)
end

end
game_object.lua:

Code: Select all

-- GAME CLASS PROTOTYPE

Game = {players=0,time_elapsed=0,numPlayersSet=false}

function Game:new (o)
	o = o or {}
	setmetatable(o, self)
	self.__index = self
	return o
end

function Game:setPlayers (numPlayers)
	self.players = numPlayers
	
	if self.players == numPlayers then
		numPlayersSet = true
		return true
	end
	
	return false
end
map_object.lua:

-- MAP OBJECT CLASS PROTOTYPE

Code: Select all

Map = {territories = 42}

function Map:new (o)
	o = o or {}
	setmetatable(o, self)
	self.__index = self
	return o
end

function Game:assignTerritories (curGameInstance)
	if curGameInstance.numPlayersSet== true then
		return true
	end
	
	return false
end
The error I'm getting is - attempt to call 'assignTerritories' (a nil value)

I believe this is the block in map_object.lua that is producing the issue:

Code: Select all

if curGameInstance.numPlayersSet == true then
return true
end
What is the best manner in which to pass a table as an argument to a member function of another table?
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: Passing a table as a function argument

Post by bmelts »

The biggest problem I see is that assignTerritories is a member function of Game, not Map...
pwniknoobz
Prole
Posts: 11
Joined: Fri Jul 16, 2010 3:10 pm

Re: Passing a table as a function argument

Post by pwniknoobz »

I'm such a noob.
Post Reply

Who is online

Users browsing this forum: No registered users and 59 guests