Page 1 of 1
Functions
Posted: Tue Jun 27, 2023 12:46 am
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?
Re: Functions
Posted: Tue Jun 27, 2023 4:29 am
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
Re: Functions
Posted: Tue Jun 27, 2023 3:14 pm
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
Re: Functions
Posted: Tue Jun 27, 2023 5:11 pm
by Itswolfcity811
Thank you for the help! It is now working. And I will put these posts in the right forum next time!