Page 1 of 1

Attempt to perform Arithmetic on Global var

Posted: Sat Oct 16, 2010 4:01 am
by weilies
hi gurus,

seeking for help on basic lua runtime error
Attempt to perform Arithmetic on Global 'my_variable'
Code

Code: Select all

function createLandObject()

	love.graphics.print("distance " .. vecX, vecX, 10)

	temp = temp + 1
	temp = vecX
	if temp >= 300 then
		temp2 = temp
		love.graphics.print("create object " .. vecX, vecX, 20)		
		table.insert(balls, 1, {x = temp2, y = y})		
		love.graphics.draw(arrow, temp2, y)
		
		temp = 0
	end
	
end
But if i change it to below, will not have any issue

Code: Select all

function createLandObject()

	love.graphics.print("distance " .. vecX, vecX, 10)

	--temp = temp + 1
	temp = vecX
	if temp >= 300 then
		temp2 = temp
		love.graphics.print("create object " .. vecX, vecX, 20)		
		table.insert(balls, 1, {x = temp2, y = y})		
		love.graphics.draw(arrow, temp2, y)
		
		temp = 0
	end
	
end
Question
- what so different between an assignment and operation (+-*/)
- how to resolve this if i need "temp" to be increase by 1 for every call


Thanks for the help

Re: Attempt to perform Arithmetic on Global var

Posted: Sat Oct 16, 2010 4:15 am
by mikembley
Where and how have you declared the temp variable?

Re: Attempt to perform Arithmetic on Global var

Posted: Sat Oct 16, 2010 4:28 am
by weilies
mikembley wrote:Where and how have you declared the temp variable?
found it!
looks like every variable need to be declare in

Code: Select all

function love.load()

end
before we use.

Re: Attempt to perform Arithmetic on Global var

Posted: Sat Oct 16, 2010 10:43 am
by Robin
weilies wrote:looks like every variable need to be declare in

Code: Select all

function love.load()

end
before we use.
Nope. You don't need to declare variables in Lua -- just make sure it has a value suitable for what you are going to do with it. (For example, if you are going to do math on it, you better make sure it points to a number or a compatible type.)

Using nil variables can make perfect sense in some contexts, just not in this one. ;)