Help with Tables

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
AngusWP
Prole
Posts: 9
Joined: Mon Jan 22, 2018 11:35 pm

Help with Tables

Post by AngusWP »

I am creating a top down shooter, and I have created everything except the actual enemies, bullets and hit detection. I am trying to create the bullet system but when I have debugged to see if my for loop runs, it doesn't. This stops the bullets from being rendered. If anyone can see the issue, please let me know ASAP.

NOTE: Where I have written print is where the issues occur, as these for loops never run.

Code: Select all

bullet = {}
player = player or require("scripts.player");
Options = Options or require("scripts.options");
Game = Game or require("scripts.game");

bulletSpeed = 100;
image = love.graphics.newImage("images/bullet.jpg");

bullet.x = -10;
bullet.y = -10;
bullet.velX = 0;
bullet.velY = 0;

fire = love.audio.newSource("sounds/fire.mp3");

function bullet.load()
       bulletTable = {}
end

function bullet.create(x, y, velX, velY)
          bullet.x = x;
          bullet.y = y;
          bullet.velX = velX;
          bullet.velY = velY;
end

b = 0;

function bullet.update(dt)

         for i, v in ipairs(bulletTable) do
             if v.remove then
             table.remove(bulletTable, i);

             print ("4");
             i = i - 1;
             else
               v:update(dt);
               print("5")
             end
         end
end

function bullet.spawn(dt) 
     if (love.mouse.isDown(1)) then
             if (player.canShoot == true) then
                if (GameState.getGameState() == "GAME" and (Game.game_time ~= 90 and Game.game_time ~= 180 and Game.game_timer ~= 300)) then

               if (player.canShoot) then
                    if (Options.getSound() == "ON") then
                          fire:play();
                   end

                  velX = 0;
                  velY = 0;
                   
                  table.insert(bulletTable, (Bullet.create(player.getX() + 23, player.getY() + 5, velX, velY)));

                  player.canShoot = false;
                  player.shootTime = 2;
               end
          end
        end
     end
end

function bullet.draw()

	   for i, v in ipairs(bulletTable) do
	   	   print("2");
           v:render();
	   	end
end

function bullet.render()
       love.graphics.draw(image, v.x, v.y, 0, 1);
end



return bullet;
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Help with Tables

Post by zorg »

Here are some pointers:
- You probably don't need to write x = x or require('y') like that; a simple x = require('y') would be enough.
- Is your bullet table just defaults?
- if so, bullet.create overwrites those, but doesn't do anything else (important for later)
- you trying to manipulate i in your ipairs loop in bullet.update is unfortunately wrong; it won't do what you want it to; you'd need to use a while loop for that.
- table.insert(bulletTable, Bullet.create(...)) looks weird from this code snippet alone, since previously, you defined bullet.create, with the b lowercase, and even then, it didn't return any values, so effectively, you'd always assign nil to bulletTable[1]. (Again, if it doesn't error, then you probably defined Bullet.create somewhere as well, but i can't see that here.)
- Also, do pay attention to correct indentation; that can save lives. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
AngusWP
Prole
Posts: 9
Joined: Mon Jan 22, 2018 11:35 pm

Re: Help with Tables

Post by AngusWP »

zorg wrote: Wed Jan 24, 2018 1:33 pm - You probably don't need to write x = x or require('y') like that; a simple x = require('y') would be enough.
I had issues with this already where if it was called twice the game wouldn't run and this fixed it.
zorg wrote: Wed Jan 24, 2018 1:33 pm - Also, do pay attention to correct indentation; that can save lives. :3
It's very readable for me and guess who's working on it.

I have updated the code but it says that I can't index a number value. Any help with this?

Code: Select all

function bullet.update(dt)
         local i = 0;
         local v = table.getn(bulletTable);

         while (i < v) do
               i = i + 1;
               bulletTable[i]:update(dt); -- this is the error line
         end
end
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Help with Tables

Post by zorg »

The first one sounds like a hack, but i guess if it works and it's good enough for you, then so be it.

Also, guess who's answering your questions? :P But seriously, proper indentation helps; and besides, it was only one location where it was weird anyway, so it's not that big of a deal.

As for your code,
table.getn is deprecated (read: old as hell), do use #bulletTable instead; v was used as bulletTable[ i ] before with ipairs, just so you're aware; i'd use n for the number of entries instead (and it's somewhat standard), but again, just something to be aware of.

Apparently, you're inserting numbers, not tables, into bulletTable, that's why you can't index it.

The shown amount of code wasn't enough to ascertain the true issue though.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
AngusWP
Prole
Posts: 9
Joined: Mon Jan 22, 2018 11:35 pm

Re: Help with Tables

Post by AngusWP »

zorg wrote: Wed Jan 24, 2018 4:27 pm
The shown amount of code wasn't enough to ascertain the true issue though.


I rewrote alot of it, and the thing works! Stuff is firing, and when I debugged removing, it did. Now for the trickier part... how do I get my bullet to fire towards where my mouse clicked? Thanks for your help so far, it means alot.

Code: Select all

bullet = {}
player = player or require("scripts.player");
Options = Options or require("scripts.options");
Game = Game or require("scripts.game");
image = love.graphics.newImage("images/bullet.jpg");

fire = love.audio.newSource("sounds/fire.mp3");

function bullet.load()
         bulletTable = {}
end

function bullet.get(x, y, velX, velY)
          bullet.x = x;
          bullet.y = y;
          bullet.velX = velX;
          bullet.velY = velY;

          return x, y, velX, velY;
end

function bullet.update(dt)
  for index, b in ipairs(bulletTable) do
           bullet.x = bullet.x + bullet.velX;
           bullet.Y = bullet.y + bullet.velY;

           if (bullet.x > love.graphics.getWidth()) then
              table.remove(bulletTable, index);
           end
           
           if (bullet.x < 0) then
              table.remove(bulletTable, index);
           end

           if (bullet.y > love.graphics.getHeight()) then
               table.remove(bulletTable, index);
           end

           if (bullet.y < 0) then
               table.remove(bulletTable, index);
           end
  end
end

function bullet.spawn(dt) 
     if (love.mouse.isDown(1)) then
             if (player.canShoot == true) then
                if (GameState.getGameState() == "GAME" and (Game.game_time ~= 90 and Game.game_time ~= 180 and Game.game_timer ~= 300)) then

               if (player.canShoot) then
                    if (Options.getSound() == "ON") then
                          fire:play();
                   end


                  speed = 50;

                  bullet.velX = speed;
                  bullet.velY = love.graphics.getHeight() / 2;

                  print(tostring(bullet.velX));

                  table.insert(bulletTable, (bullet.get(player.getX() + 23, player.getY() + 5, bullet.velX, bullet.velY)));

                  player.canShoot = false; 
                  player.shootTime = 2;
               end
          end
        end
     end
end

function bullet.draw()
         for index, b in ipairs(bulletTable) do
               love.graphics.draw(image, bullet.x, bullet.y, 0, 1);
         end
end



return bullet;

Post Reply

Who is online

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