Attempt to perform Arithmetic on Global var

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
User avatar
weilies
Prole
Posts: 28
Joined: Sat Oct 09, 2010 6:16 am

Attempt to perform Arithmetic on Global var

Post 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
User avatar
mikembley
Citizen
Posts: 72
Joined: Wed Dec 17, 2008 5:30 pm
Location: Blackburn, UK
Contact:

Re: Attempt to perform Arithmetic on Global var

Post by mikembley »

Where and how have you declared the temp variable?
User avatar
weilies
Prole
Posts: 28
Joined: Sat Oct 09, 2010 6:16 am

Re: Attempt to perform Arithmetic on Global var

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Attempt to perform Arithmetic on Global var

Post 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. ;)
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 7 guests