Page 1 of 4

Works with one thing but not the other?

Posted: Sat Jun 15, 2013 11:55 pm
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

Re: Works with one thing but not the other?

Posted: Sun Jun 16, 2013 1:37 am
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.

Re: Works with one thing but not the other?

Posted: Sun Jun 16, 2013 9:52 am
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.

Re: Works with one thing but not the other?

Posted: Sun Jun 16, 2013 12:03 pm
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.

Re: Works with one thing but not the other?

Posted: Sun Jun 16, 2013 12:31 pm
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

Re: Works with one thing but not the other?

Posted: Sun Jun 16, 2013 2:20 pm
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.

Re: Works with one thing but not the other?

Posted: Sun Jun 16, 2013 2:55 pm
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

Re: Works with one thing but not the other?

Posted: Sun Jun 16, 2013 3:16 pm
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.

Re: Works with one thing but not the other?

Posted: Sun Jun 16, 2013 3:42 pm
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.

Re: Works with one thing but not the other?

Posted: Sun Jun 16, 2013 4:06 pm
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?