Page 1 of 1
erroneous arithmetic operations with a global variable
Posted: Thu Aug 11, 2016 2:01 pm
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
Re: erroneous arithmetic operations with a global variable
Posted: Thu Aug 11, 2016 4:05 pm
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 "nex
tPosX", not "nexpPosX".
Re: erroneous arithmetic operations with a global variable
Posted: Thu Aug 11, 2016 5:00 pm
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 "nex
tPosX", not "nexpPosX".
Thank you for at least one bug fixed
Re: erroneous arithmetic operations with a global variable
Posted: Fri Aug 12, 2016 7:41 pm
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.
Re: erroneous arithmetic operations with a global variable
Posted: Sun Aug 14, 2016 5:58 am
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?)
Re: erroneous arithmetic operations with a global variable
Posted: Tue Aug 16, 2016 6:24 pm
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.
Re: erroneous arithmetic operations with a global variable
Posted: Tue Aug 16, 2016 6:55 pm
by zorg
Or just use
strict.lua, since that's what it's for. Well, almost.
Re: erroneous arithmetic operations with a global variable
Posted: Wed Aug 17, 2016 8:33 am
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.