[SOLVED]Can't call functions from other 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
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

[SOLVED]Can't call functions from other class

Post by Vimm »

I'm trying to get used to using classes so I have a spawn class and a game class, the game class is supposed to call the spawn class's draw function, but it gives me the error "attempt to call function "draw" a nil value"

can anyone tell me what I need to change?
Attachments
classError.love
(1.73 KiB) Downloaded 88 times
Last edited by Vimm on Wed Apr 20, 2016 9:55 pm, edited 1 time in total.
User avatar
Ikroth
Citizen
Posts: 79
Joined: Thu Jul 18, 2013 4:44 am

Re: Can't call functions from other class

Post by Ikroth »

The problem is you're using global variables inside of your class functions.

In game.lua, you write:

Code: Select all

function Game:new()
	require "platformSpawn"
	platform = Platform()
end
platform is in the global scope by default.

Inside platformSpawn.lua, there is also this function:

Code: Select all

function createPlatform()
	platform = 
		{
			x = love.graphics.getWidth(),
			y = love.math.random(0, love.graphics.getHeight()),
			width = 256,
			height = 16
		}
	return platform
end
The platform inside of there refers to the same platform in the global scope, so it overwrites whatever platform was previously. You can confirm this by printing out platform in createPlatform before and after creating the table.

Since createPlatform is called during update, when it moves on to draw, platform is now a table that doesn't have a draw function. That's why you get that specific error.

So, you could fix it by using local variables instead of global variables wherever possible, something like:

Code: Select all

function createPlatform()
	local platform = { ... }
	return platform
end

User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: Can't call functions from other class

Post by Vimm »

Ikroth wrote:The problem is you're using global variables inside of your class functions.

In game.lua, you write:

Code: Select all

function Game:new()
	require "platformSpawn"
	platform = Platform()
end
platform is in the global scope by default.

Inside platformSpawn.lua, there is also this function:

Code: Select all

function createPlatform()
	platform = 
		{
			x = love.graphics.getWidth(),
			y = love.math.random(0, love.graphics.getHeight()),
			width = 256,
			height = 16
		}
	return platform
end
The platform inside of there refers to the same platform in the global scope, so it overwrites whatever platform was previously. You can confirm this by printing out platform in createPlatform before and after creating the table.

Since createPlatform is called during update, when it moves on to draw, platform is now a table that doesn't have a draw function. That's why you get that specific error.

So, you could fix it by using local variables instead of global variables wherever possible, something like:

Code: Select all

function createPlatform()
	local platform = { ... }
	return platform
end

*facepalm*
you're right, that was the issue, I SHOULD KNOW THIS
im disappointed in me XD
thanks! :D
Post Reply

Who is online

Users browsing this forum: No registered users and 76 guests