Calls table value as function. Can't seem to figure it out.

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
JJSax
Prole
Posts: 47
Joined: Fri Apr 04, 2014 3:59 am

Calls table value as function. Can't seem to figure it out.

Post by JJSax »

[spoiler] My first post, trying to figure out how to do spoilers for the future.
anyone know how?
please ignore this. [/spoiler]

I have a super basic shooter game. I've just been loosely following a tutorial I found on Youtube to learn Love so most this code isn't mine.
also if you have to look through the code, I believe that there is no need for anything in the image folder, but in case I just forgot about one, they're in there.

When trying to move the bullet (below) it says it's thinking v is a function value. It's probably really simple but I'm still a noob ;)

Code: Select all

function bullet.move(dt)
	for i,v in pairs(bullet) do
		if v.dir == "up" then -- errors here
			v.y = v.y - bullet.speed * dt
		end
		if v.dir == "down" then
			v.y = v.y + bullet.speed * dt
		end
		if v.dir == "left" then
			v.x = v.x - bullet.speed * dt
		end
		if v.dir == "right" then
			v.x = v.x + bullet.speed * dt
		end
	end
end
thanks in advance! :)
my first shooter.love
(913.77 KiB) Downloaded 69 times
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Calls table value as function. Can't seem to figure it

Post by Germanunkol »

Hi!

Welcome to the forums!

The line:

Code: Select all

 for i,v in pairs(bullet) do 
does the following:
It iterates over ALL values in the bullet table (the table you create in line two of bullet.lua).
In this table, you then put values like width, height, speed etc. And then you also put function values into the table (3, to be exact: bullet.spawn, bullet.draw, bullet.move). So now you have three function values in the table "bullet", which is no problem so far. But then when you iterate over the bullets: What you're trying to do is move every bullet you've created - which fails because there's not only bullets in the table, but also functions.
If you've ever written code in any object-oriented language, you can think of it like this: The way you use the table "bullets" makes it something like a class. But then you also insert the objects into that class ( using the call table.insert(bullet, ...) ) and so now bullet is a mix of class and instance (this comparison only holds to some extend. To make bullet a real "class", some things are still missing - but I hope this works well as an example).

So, how to fix it:
The easiest way I can think of is this:
Create a new table "bulletList" or similar and insert the objects into that list. At the same time, leave all the "class values", the functions etc in the old "bullet" table. I'll walk you through it:
  • Under bullet = {}, add another line:
    bulletsList = {}
  • In line 9, change the "bullet" to "bulletList" (so you're adding the new bullet to the "bulletList" table instead of adding it to the "bullet" table)
  • In line 13 and in line 20, change the "pairs(bullet)" to "pairs(bulletList)" since we want to draw the instances of bullets, not the class.
Couple of other remarks:
  • When all of this is done, the game won't run yet since you seem to be doing the same mistake in other files as well (enemy.lua is the next one that pops up). This should be solvable by using the same trick: add enemyList = {} to enemy.lua and insert enemies into that list.
  • You don't need the functions DRAW_BULLET and UPDATE_BULLET. Instead, you can simply call bullet.draw() and bullet.move() in the other files, since "bullet" is a global variable (in Lua, everything that isn't specifically declared as local is automatically global. Ugly, but sadly true.)
  • It's considered good practice to make everything you can local. Making the bullet table local would require a bit of code restructuring (and wouldn't do you much good since you need it as a global in main.lua anyways) but bulletList could be made local - since you only need this table in bullet.lua. So just prepend the line "bulletList = {}" with "local", to get "local bulletList = {}".
    Also keep this in mind when adding more code - make sure to make everything local that doesn't need to be global. Otherwise you can run into nasty stuff like functions modifying each other's variables.
Hope this helps!

P.S.
I've never seen spoiler tags here, I don't think they've been added to the forums. So no talking about season 3 or 4 of Game of Thrones! I haven't watched them yet... :P
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Calls table value as function. Can't seem to figure it

Post by kikito »

sıɥʇ ǝʞıʃ sɹǝʃıods op sʎɐʍʃɐ uɐɔ no⅄
When I write def I mean function.
JJSax
Prole
Posts: 47
Joined: Fri Apr 04, 2014 3:59 am

Re: Calls table value as function. Can't seem to figure it

Post by JJSax »

Germanunkol wrote:Hi!

Welcome to the forums

==============
--SNIP--
==============
That did it. Good advice. Normally I try to use locals but in case the guy I was following had plans and needed them global I didn't.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests