can't pass table to function

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
mahmoud2000
Prole
Posts: 3
Joined: Wed Sep 22, 2021 3:15 pm

can't pass table to function

Post by mahmoud2000 »

  1. HI:
hello guy it is nice to write here, With all these colors and cartoons here and there, I think that makes it warmer place, thank you
  • THE PROJECT:
the game I am working on is breakout simply, I am just trying to understand it very well and improve it with features.
the game overview
the game overview
Screenshot from 2021-09-24 16-14-19.png (22.79 KiB) Viewed 2098 times
the game is here https://drive.google.com/file/d/1Ym_ZmL ... sp=sharing
  • THE PROBLEM:
I am actually trying to add a power up to the game, so player can pick it up in the game and enjoy extra abilities.
So I wrote a class for the power up, which is an object that move downwards and player tries to capture it, ( I used Class.lua to add class feature to lua that you can see in /lib/class.lua ) and added a collision function to the power up class,

Code: Select all

function Powerup:collide(target)

    -- if not target then return end
    -- first, check to see if the left edge of either is farther to the right
    -- than the right edge of the other
    if self.x > pup.x + pup.width or pup.x > self.x + self.width then
        return false
    end

    -- then check to see if the bottom edge of either is higher than the top
    -- edge of the other
    if self.y > pup.y + pup.height or pup.y > self.y + self.height then
        return false
    end 

    -- if the above aren't true, they're overlapping
    return true
end
BUT when calling the function in the playstate ( if don’t know what is mean, instead of putting all your code in main.lua you put code related to play state in PlayState.lua and code related to choosing paddle in SelectPaddleState.lua for example ,every state file contains initializing function, enter function, update function, render function and exit function,S and I used StateMachine.lua to add that feature to love2d) it says error that target is nil although I passed a table to the collision function.
here is the part where to call collide function in the PlayState.lua

Code: Select all

if pup then
        -- pup is is the power up object
        if pup.collide(self.paddle) then 
            pup.display = false
        end
    end
end the error is
error photo
error photo
Screenshot from 2021-09-24 16-21-51.png (35.18 KiB) Viewed 2098 times
  • WHAT I DID:
i think it concerns lua not love2d but i hope you can help,
i tried debugging but no thing solved, the only thing that solved it is to use pup (the power up object as global variable) ,
i think the problem in passing the table or the object paddle to the function
  • THE END:
thank you for your precious time and for caring :nyu: :nyu: :nyu:
User avatar
pgimeno
Party member
Posts: 2863
Joined: Sun Oct 18, 2015 2:58 pm

Re: can't pass table to function

Post by pgimeno »

Welcome to the forums. There isn't enough information in the code snippets that you have posted. However, it is clear that your collide function does not use the argument, and that is bad. I think that instead of pup.x, pup.y... it should be using target.x, target.y...

Also, next time please copy and paste the error instead of posting a screenshot. See where it says "Press Ctrl+C or tap to copy this error"? Use that and then paste here.
grump
Party member
Posts: 880
Joined: Sat Jul 22, 2017 7:43 pm

Re: can't pass table to function

Post by grump »

It's the usual . and : confusion when calling pup.collide - should be pup:collide
mahmoud2000
Prole
Posts: 3
Joined: Wed Sep 22, 2021 3:15 pm

Re: can't pass table to function

Post by mahmoud2000 »

pgimeno wrote: Sat Sep 25, 2021 10:50 am Welcome to the forums. There isn't enough information in the code snippets that you have posted. However, it is clear that your collide function does not use the argument, and that is bad. I think that instead of pup.x, pup.y... it should be using target.x, target.y...

Also, next time please copy and paste the error instead of posting a screenshot. See where it says "Press Ctrl+C or tap to copy this error"? Use that and then paste here.
Before writing ...
Thank you so much to welcome me :D ,
Actually I post the code after editing it and making pup global.
but Alright i could find the error, it was that i used . instead of : in the collide function but
thank you so much
and I'll make sure to copy error instead of post the whole screen ;)

:awesome: :awesome: :awesome:
Last edited by mahmoud2000 on Sun Sep 26, 2021 11:55 am, edited 2 times in total.
mahmoud2000
Prole
Posts: 3
Joined: Wed Sep 22, 2021 3:15 pm

Re: can't pass table to function

Post by mahmoud2000 »

grump wrote: Sat Sep 25, 2021 10:57 am It's the usual . and : confusion when calling pup.collide - should be pup:collide
That is Great!!
thank you
indeed I managed to find it earlier, but thank you for your help although
:nyu: :nyu: :nyu:
Post Reply

Who is online

Users browsing this forum: cloudberry, Froyok and 38 guests