[SOLVED] Moving an object to another table

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
Teraku
Prole
Posts: 27
Joined: Mon Jun 24, 2013 10:01 am
Location: The Netherlands

[SOLVED] Moving an object to another table

Post by Teraku »

So I'm trying to change the behavior of my enemies to "reload" (Move backwards) for a while after shooting. However, I can't seem to get it to work. I've tried inserting the enemy in another table, but it gives the following error as soon as the enemies start reloading:
Error: main.lua:288: attempt to index local 'v' (a number value)
stack traceback:
main.lua:288: in function <main.lua:151>
(tail call): ?
(tail call): ?
[string "boot.lua"]:407: in function <[string "boot.lua"]:373>
[C]: in function 'xpcall'
Here's the offending code in question:

Code: Select all

for i,v in ipairs(enemies) do
  dx = player.X - v.X
  dy = player.Y - v.Y
  distance = math.sqrt(dx*dx+dy*dy)
  v.X = v.X + (dx / distance * v.speed * dt)
  v.Y = v.Y + (dy / distance * v.speed * dt)
  if distanceFrom(player.X, player.Y, v.X, v.Y) < 100 and v.isReloading == false then
    enemyShoot(v.X, v.Y)
	table.insert(reloadingenemies, i)
	v.isReloading = true
  end
  if CheckCollision(v.X,v.Y,v.width,v.height,player.X,player.Y,player.width,player.height) and player.isAlive == true then
    playerDie()
  end
end

for i,v in ipairs(reloadingenemies) do
  if v.isReloading == true then
    v.X = v.X + 50 * dt
  end
end
I'm pretty sure I'm going about this the wrong way, so could anyone give a hand?
Last edited by Teraku on Fri Nov 15, 2013 1:05 pm, edited 1 time in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Moving an object to another table

Post by micha »

Here

Code: Select all

table.insert(reloadingenemies, i)
you insert the key of the enemy into the table. It should be like this:

Code: Select all

table.insert(reloadingenemies, v)
However, it will be easier to implement, if you have only one table, with all enemies and each enemy entry "knows" if it is reloading or not. You already have the isReloading-flag, so you don't really need two tables. Two tables mean additional entry management for you and it might happen that you accidently copy an entry or something. So my suggestion is to use only one table and do something like this:

Code: Select all

for i,v in ipairs(enemies) do
  if v.isReloading then
    -- do reloading stuff
  else
    -- do normal stuff
  end
end
Oh, and you can make helping you easier for us, if you upload a .love next time. In this case, the error told us that something is wrong in line 288, but in your code snippet, we cannot see, which line this is.
Teraku
Prole
Posts: 27
Joined: Mon Jun 24, 2013 10:01 am
Location: The Netherlands

Re: Moving an object to another table

Post by Teraku »

Ah, right. That worked, thanks.

I just remembered why I came up with a separate table for reloading enemies: Due to some bug I couldn't solve, all enemies started moving backwards if one started reloading. But it's all solved now. Thanks. :awesome:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests