Works with one thing but not the other?

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.
RunningGamesStudios
Prole
Posts: 46
Joined: Fri May 31, 2013 9:42 pm

Works with one thing but not the other?

Post by RunningGamesStudios »

main.lua

Code: Select all

function love.load()
	--require lib's
	require ("player")
	require ("mlib")
	require ("projectile")
	_w = love.graphics.getWidth()
	_h = love.graphics.getHeight()
	_mx = love.mouse.getX()
	_my = love.mouse.getY()	
	
	math.randomseed(os.time()) --for testing
	_intProjectiles()
	_intPlayer() --load player
	playerintd = true
end

function love.update(dt)	
	--update instances
	_moveProjectiles()	
	_movePlayer()
	
	_fDT = dt --frame delta time
	_mx = love.mouse.getX()
	_my = love.mouse.getY()
	
	--player controls
	if love.keyboard.isDown("right") then
		_rotatePlayer("right")
	elseif love.keyboard.isDown("left") then
		_rotatePlayer("left")		
	end
	
	-- fireing
	if love.keyboard.isDown("a") then
		_fireProjectile()
	end
end


function love.draw()
	love.graphics.setColor(255,255,255)
	_drawPlayer()
	love.graphics.setColor(128, 128, 128)
	_drawProjectiles()
end




player.lua

Code: Select all

function love.load()
	require("main")
end

_intPlayer = function()
	_player = {
		x = _w / 2,
		y = _h / 2,
		angle = 180,
		size = 20,
		speed = 10
	}
end

_movePlayer = function()
	_player.x = _player.x + math.cos(math.rad(angle) * _player.speed * dt)	
	_player.y = _player.y + math.sin(math.rad(angle) * _player.speed * dt)
end

_rotatePlayer = function(key)
	if key == "right" then
		angle = angle + 1 * dt
	elseif key == "left" then
		angle = angle - 1 * dt
	end
end

_drawPlayer = function()
	love.graphics.setColor(255,255,255)
	love.graphics.polygon("fill",
		_player.x + 
		_player.size, 
		_player.y +
		_player.size, 
		_player.x, 
		_player.y - 
		_player.size,
		_player.x - 
		_player.size,
		_player.y + 
		_player.size
	)
end


got this error

main.lua:13: attempt to call global 'intPlayer' a nil value


Help
User avatar
NightKawata
Party member
Posts: 294
Joined: Tue Jan 01, 2013 9:18 pm
Location: Cyberspace, Room 6502
Contact:

Re: Works with one thing but not the other?

Post by NightKawata »

Why are you calling love.load twice in a project, and why are you requiring the main file? If those two decisions aren't ringing bells, I don't know what is.
"I view Python for game usage about the same as going fishing with a stick of dynamite. It will do the job but it's big, noisy, you'll probably get soaking wet and you've still got to get the damn fish out of the water." -taylor
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Works with one thing but not the other?

Post by Jasoco »

Also, don't require from within the love.load function. Require from the top of the main.lua and don't require main. It's already being run.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Works with one thing but not the other?

Post by Robin »

In this case, three wrongs make a right.

Wrong 1: requiring modules inside love.load()
Wrong 2: defining love.load() twice
Wrong 3: requiring "main"

Because of 1, the second love.load() is only defined when love.load() is already run (and so is not called again), and "main" is not required.

Not that a lot of things would have happened if "main" actually was required, but still.
Help us help you: attach a .love.
RunningGamesStudios
Prole
Posts: 46
Joined: Fri May 31, 2013 9:42 pm

Re: Works with one thing but not the other?

Post by RunningGamesStudios »

Tried this


main.lua

Code: Select all

require ("player")
require ("mlib")
require ("projectile")


function love.load()


	_w = love.graphics.getWidth()
	_h = love.graphics.getHeight()
	_mx = love.mouse.getX()
	_my = love.mouse.getY()	
	
	math.randomseed(os.time()) --for testing
	_intProjectiles()
	_intPlayer() --load player
	playerintd = true
end

function love.update(dt)	
	--update instances
	_moveProjectiles()	
	_movePlayer()
	
	_fDT = dt --frame delta time
	_mx = love.mouse.getX()
	_my = love.mouse.getY()
	
	--player controls
	if love.keyboard.isDown("right") then
		_rotatePlayer("right")
	elseif love.keyboard.isDown("left") then
		_rotatePlayer("left")		
	end
	
	-- fireing
	if love.keyboard.isDown("a") then
		_fireProjectile()
	end
end


function love.draw()
	love.graphics.setColor(255,255,255)
	_drawPlayer()
	love.graphics.setColor(128, 128, 128)
	_drawProjectiles()
end


player.lua

Code: Select all


_intPlayer = function()
	_player = {
		x = _w / 2,
		y = _h / 2,
		angle = 180,
		size = 20,
		speed = 10
	}
end

_movePlayer = function()
	_player.x = _player.x + math.cos(math.rad(_player.angle) * _player.speed * _fDT)	
	_player.y = _player.y + math.sin(math.rad(_player.angle) * _player.speed * _fDT)
end

_rotatePlayer = function(key)
	if key == "right" then
		angle = angle + 1 * dt
	elseif key == "left" then
		angle = angle - 1 * dt
	end
end

_drawPlayer = function()
	love.graphics.setColor(255,255,255)
	love.graphics.polygon("fill",
		_player.x + 
		_player.size, 
		_player.y +
		_player.size, 
		_player.x, 
		_player.y - 
		_player.size,
		_player.x - 
		_player.size,
		_player.y + 
		_player.size
	)
end


but got this error

player.lua:14: attemp to perform arithmetic on global _fDT a nil value
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Works with one thing but not the other?

Post by Robin »

Alright, I'm going to be harsh for a minute here:
  1. Please, upload your code as a .love instead of dumping it directly in your post. Do you know how to do that?
  2. What's with the underscores you start every variable name with? It's ugly, makes your code harder to read and serves no point.
  3. Globals are bad. Don't do it like that. Let _intPlayer return the player table instead of assigning to a global name. For that matter, use arguments. If you had passed dt to _movePlayer as an argument instead of assigning it to the global name _fDT, you would not have had the problem you're having now. And as for that:
  4. You assign dt to _fDT after calling _movePlayer, instead of before, so the _fDT is still nil the first time you try to move the player.
Help us help you: attach a .love.
RunningGamesStudios
Prole
Posts: 46
Joined: Fri May 31, 2013 9:42 pm

Re: Works with one thing but not the other?

Post by RunningGamesStudios »

Robin wrote:Alright, I'm going to be harsh for a minute here:
  1. Please, upload your code as a .love instead of dumping it directly in your post. Do you know how to do that?
  2. What's with the underscores you start every variable name with? It's ugly, makes your code harder to read and serves no point.
  3. Globals are bad. Don't do it like that. Let _intPlayer return the player table instead of assigning to a global name. For that matter, use arguments. If you had passed dt to _movePlayer as an argument instead of assigning it to the global name _fDT, you would not have had the problem you're having now. And as for that:
  4. You assign dt to _fDT after calling _movePlayer, instead of before, so the _fDT is still nil the first time you try to move the player.

yeah I figured the last one out. thanks i'll uplaod the .love anyway. i'll change the variables later I don't have time right now
Attachments
test.love
(2.47 KiB) Downloaded 176 times
User avatar
severedskullz
Prole
Posts: 36
Joined: Thu May 30, 2013 1:55 am

Re: Works with one thing but not the other?

Post by severedskullz »

Robin wrote: Wrong 1: requiring modules inside love.load()
Quite curious as to why. Apparently I'm making this "wrong" as well but I have had no issues in doing so.

The only thing I could think of that would cause issues is if functions or variables outside load try and use stuff that isn't required (in memory) yet... but I have already taken that into consideration when writing my modules.

Either that or it is just a minor bad practice which I can understand as well.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Works with one thing but not the other?

Post by T-Bone »

severedskullz wrote:
Robin wrote: Wrong 1: requiring modules inside love.load()
Quite curious as to why. Apparently I'm making this "wrong" as well but I have had no issues in doing so.

The only thing I could think of that would cause issues is if functions or variables outside load try and use stuff that isn't required (in memory) yet... but I have already taken that into consideration when writing my modules.

Either that or it is just a minor bad practice which I can understand as well.
I'm wondering this as well. All my modules return their values upon requirement (rather than writing to global variables), so I always thought it made the most sense to put it love.load (or similarly used state.load functions). I would be very interested in knowing any negative aspects of this style.
RunningGamesStudios
Prole
Posts: 46
Joined: Fri May 31, 2013 9:42 pm

Re: Works with one thing but not the other?

Post by RunningGamesStudios »

Fixed it but I got another problem...

When you hold eather left or right keys the angle goes past 360 and for some reason when the angle is a high number the movement starts to slow down. Help?
Attachments
test.love
(2.47 KiB) Downloaded 173 times
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 16 guests