Functions

Discuss any ports of LÖVE to different platforms.
Post Reply
Itswolfcity811
Prole
Posts: 7
Joined: Tue Jun 27, 2023 12:38 am

Functions

Post by Itswolfcity811 »

I need help with a function I'm writing it is suppose to draw a circle around the player relative to my mouse and when I try to make it a specific distance away from the player it is being weird. Please also note that I'm bad at spelling and some words in my code my be misspelled.

Code: Select all

local love = _G.love

function Weapon(Weapons, _Id, Player, _Distance)
    return {
        Id = _Id or 1,
        Distance = _Distance + Player.Radius,
        print(Player.Radius),
        print(Distance),
        print(_Distance),

        Draw = function(self)
            love.graphics.circle("fill", self.Player.x + Distance, self.Player.y + Distance, self.Player.Radius / 10)
        end,
    }
end

return Weapon
When I run the code with "lovec ." it says this:
20
nil
50

Can anyone help?
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Functions

Post by Andlac028 »

When you are creating table (return { … }), you can not reference keys or values in that table, as you are just creating it. Move the Distance = … outside before that return. And if you want to return also Distance, just add Distance = Distance in that table.

P.S.: please use support and development forum instead of ports forum. Ports forum is for things about porting love to other platforms, not about general problems
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Functions

Post by darkfrei »

Itswolfcity811 wrote: Tue Jun 27, 2023 12:46 am I need help with a function I'm writing it is suppose to draw a circle around the player relative to my mouse and when I try to make it a specific distance away from the player it is being weird. Please also note that I'm bad at spelling and some words in my code my be misspelled.

Code: Select all

local love = _G.love

function Weapon(Weapons, _Id, Player, _Distance)
    return {
        Id = _Id or 1,
        Distance = _Distance + Player.Radius,
        print(Player.Radius),
        print(Distance),
        print(_Distance),

        Draw = function(self)
            love.graphics.circle("fill", self.Player.x + Distance, self.Player.y + Distance, self.Player.Radius / 10)
        end,
    }
end

return Weapon
When I run the code with "lovec ." it says this:
20
nil
50

Can anyone help?
First small letter for local

Code: Select all

local function newWeapon (player, weapons, id, distance)
  id = id or 1
--  local weapon = weapons[id] -- why you have weapons here?
  print(player.x, player.y, player.radius),
  print(distance),
  print(distance + player.radius),
  local distance = distance + player.radius
    return {
        id = id,
        distance = distance,
        player = player, -- to access for draw function 
        draw = function(self)
            love.graphics.circle("fill", self.player.x + self.distance, self.player.y + self.distance, self.player.radius / 10)
        end,
    }
end

return newWeapon
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Itswolfcity811
Prole
Posts: 7
Joined: Tue Jun 27, 2023 12:38 am

Re: Functions

Post by Itswolfcity811 »

Thank you for the help! It is now working. And I will put these posts in the right forum next time!
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests