erroneous arithmetic operations with a global variable

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
McFree
Prole
Posts: 12
Joined: Thu Aug 11, 2016 7:31 am

erroneous arithmetic operations with a global variable

Post by McFree »

Error: map.lua:14: attempt to perform arithmetic on global 'mapPosX' (a nil value)
It can not change the global variable is initialized in the same lua file, but only when you start the game

map.lua

Code: Select all

function initMap()
  mapShow = false
  mapPosX = -80;
  mapPosY = -50;
  colorChanged = false;
end

function drawMap()
	lg.draw(gameMap, gameMapQuad, mapPosX, mapPosY);
	lg.draw(gameMapView, gameMapQuad, mapPosX, mapPosY);
end

function moveMap(X,Y)
	nextPosX = mapPosX+X; --Then an error occurs
 	nextPosY = mapPosY+Y; -- Here, too, there is an error
	dx = 0;
	dy = 0;
	r, g, b, a = gameMapData:getPixel(windowWidth/2-nextPosX+dx,windowHeight/2-nextPosY+dy);
	if a >= 50 then
		mapPosX = nexpPosX;
		mapPosY = nextPosY;
	else 
		r, g, b, a = gameMapData:getPixel(windowWidth/2-nextPosX+dx,windowHeight/2-mapPosY+dy)
	   	if a >= 50 then
	    	mapPosX = nextPosX;
	    end
	    r, g, b, a = gameMapData:getPixel(windowWidth/2-mapPosX+dx,windowHeight/2-nextPosY+dy)
	    if a >= 50 then
	    	mapPosY = nextPosY;
	    end
  	end
end
I attach a file to the topic .love Link
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: erroneous arithmetic operations with a global variable

Post by 4aiman »

I'll put the same answer here as well. Just in case. Moderators will do what has to be done anyway :)

You've got a little tiny typo on line #20 of map.lua.
It's "nextPosX", not "nexpPosX". :)
McFree
Prole
Posts: 12
Joined: Thu Aug 11, 2016 7:31 am

Re: erroneous arithmetic operations with a global variable

Post by McFree »

4aiman wrote:I'll put the same answer here as well. Just in case. Moderators will do what has to be done anyway :)

You've got a little tiny typo on line #20 of map.lua.
It's "nextPosX", not "nexpPosX". :)
Thank you for at least one bug fixed :D
User avatar
Rucikir
Party member
Posts: 129
Joined: Tue Nov 05, 2013 6:33 pm

Re: erroneous arithmetic operations with a global variable

Post by Rucikir »

You should use luacheck. It's a tool for linting and static analysis of Lua code, and I beleive it could have detected your mistake.
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: erroneous arithmetic operations with a global variable

Post by 4aiman »

Rucikir wrote:You should use luacheck. It's a tool for linting and static analysis of Lua code, and I beleive it could have detected your mistake.
Does it work with love2d? I couldn't find any info on that.
Right now it seems to me that this tool will complain about every other love.* function out there.
(Shall we start a separate thread? Anyone?)
User avatar
Rucikir
Party member
Posts: 129
Joined: Tue Nov 05, 2013 6:33 pm

Re: erroneous arithmetic operations with a global variable

Post by Rucikir »

luacheck detects global variables like 'love'. You can simply add your own global definitions by the command line or with a .luacheckrc file.
As LÖVE uses LuaJIT and exports all its functions in the global table 'love', a good start could be:

Code: Select all

luacheck main.lua --std luajit --globals love
An example on a -very- simple code that looks like OP's problem:
$ luacheck -
local a = b
print(a)
Checking stdin 1 warning

stdin:1:11: accessing undefined variable b

Total: 1 warning / 0 errors in 1 file
The luacheck + LÖVE duo could be difficult to work with, I noticed that a lot of projects make extensive use of globals.
Last edited by Rucikir on Tue Aug 16, 2016 7:01 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: erroneous arithmetic operations with a global variable

Post by zorg »

Or just use strict.lua, since that's what it's for. Well, almost.
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.
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: erroneous arithmetic operations with a global variable

Post by 4aiman »

Ah.... The strict.lua!
The thing which had made my life harsh for a couple of weeks back in early 2015 :)

Anyway, "--globals love" is *not* detection. It's a good thing one can tune up luacheck to accept unknown vars, though.
Post Reply

Who is online

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