1.Object = {}
2.function Object:Box(x,y,w,h)
3.local t = {x=x,y=y,w=w,h=h,id = id or #Object +1}
4.table.insert(Object, t)
5.return t
6.end
7.function Object:Draw()
8.for i,v in ipairs(Object) do
9.love.graphics.rectangle("fill",v.x,v.y,v.w,v.h)
10.end
11.end
12.function Object:Update(oX,oY,oWidth,oHeight,dt)
13. X = oX
14. Y = oY
15. W = oWidth
16. H = oHeight
17.for i,v in ipairs(Object) do
18. if X + W > v.x and X < v.x + v.w and Y + H > v.y and Y < v.y + v.h then
19.
20. end
21. end
22.end
return Object
this is my code for the module that should make the work easier for me but line 18 is the problem or basically the X, Y, W, H are the problem I dont know, thanks.
Help (Need Help For My Module)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Help (Need Help For My Module)
It's really hard to help you if you can not tell us what is wrong.
That gets even harder if the code provided runs like a charm. I included this code to a very very leightweight main as this:
Initialization and a simple toggle to use something overlapping and something not overlapping within the update function. That's it.
It properly prints out the overlap when the button is pressed.
With the snippet you provided it's impossible to tell what's going wrong in your case. I would guess that you didn't provide all parameters to the creation of the box since you have no default and that would crash when you try to do arithmetic with such values.
That gets even harder if the code provided runs like a charm. I included this code to a very very leightweight main as this:
Code: Select all
Object = {}
function Object:Box(x,y,w,h)
local t = {x=x,y=y,w=w,h=h,id = id or #Object +1}
table.insert(Object, t)
return t
end
function Object:Draw()
for i,v in ipairs(Object) do
love.graphics.rectangle("fill",v.x,v.y,v.w,v.h)
end
end
function Object:Update(oX,oY,oWidth,oHeight,dt)
X = oX
Y = oY
W = oWidth
H = oHeight
for i,v in ipairs(Object) do
if X + W > v.x and X < v.x + v.w and Y + H > v.y and Y < v.y + v.h then
print("Test")
end
end
end
function love.load()
Object:Box(5, 5, 10, 10)
end
function love.update(dt)
if love.keyboard.isDown("a") then
Object:Update(10, 10, 10, 10, dt)
else
Object:Update(20, 20, 10, 10, dt)
end
end
function love.draw()
Object:Draw()
end
It properly prints out the overlap when the button is pressed.
With the snippet you provided it's impossible to tell what's going wrong in your case. I would guess that you didn't provide all parameters to the creation of the box since you have no default and that would crash when you try to do arithmetic with such values.
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Help (Need Help For My Module)
The problem is the x,y,w,h is a nil value in line 18
Re: Help (Need Help For My Module)
In your object's update you are just overwriting globals, not changing per entity variables... Also, when using semicolon syntax the first parameter passed is self.
Object.update(Object, dt) == Object:update(dt)
Object.update(Object, dt) == Object:update(dt)
Code: Select all
function Object:draw()
love.graphics.rectangle('fill', self.x, self.y, self.w, self.h)
end
-- is the same as:
function Object.draw(self)
love.graphics.rectangle('fill', self.x, self.y, self.w, self.h)
end
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Help (Need Help For My Module)
Still doesnt do anything and I used : in other functions like enemy which did not cause any problems btw here is my file
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Help (Need Help For My Module)
And to check the error click start then the first box
- ThonyVezbe
- Prole
- Posts: 2
- Joined: Thu Jun 22, 2017 8:53 am
Re: Help (Need Help For My Module)
Isn't your 'player' var local in main.lua? Or is it even declared / set before update is called?Error
Modules/Object.lua:14: attempt to index global 'player' (a nil value)
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Help (Need Help For My Module)
Thats the point if you check the object code you can see player.x,y,w,h if you erase that and write in this way x = x then in object.updateP(x,y,w,h,dt) add xywh now go to level1 and go to level1.update and add or edit object.updatep(player.x,player.y,player.w,player.h,dt)
My goal is just to make a simple collision
I know bounding box
My goal is just to make a simple collision
I know bounding box
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Help (Need Help For My Module)
Can someone help me pls?
Re: Help (Need Help For My Module)
Your kinda broken English is not helping a lot but alright. I'll try.
If you use player. Or any global variable that isn't supposed to be available all the time (for example if you store the window data in between somewhere or have a global data table for saved variables). You might as well not make it a module. The whole point of modules is to fully encapsulate functionality.
If you just want to separate your code into multiple files you can just act like it's inserted when called with "require". That does not need a return value.
The goal is to simply provide functions with parameters that take outside data (like player location).
Right now it breaks because player is a local variable in level1.lua. This means it is not available in your Object.lua. Because it's local. To level1.lua.
If you provide the player as variable to update this error goes away. Though player doesn't seam to have the variable w.
But as I was trying to explain. You should really try to not use any global variables in your modules. Because that makes the whole abstraction into modules pointless.
If you use player. Or any global variable that isn't supposed to be available all the time (for example if you store the window data in between somewhere or have a global data table for saved variables). You might as well not make it a module. The whole point of modules is to fully encapsulate functionality.
If you just want to separate your code into multiple files you can just act like it's inserted when called with "require". That does not need a return value.
The goal is to simply provide functions with parameters that take outside data (like player location).
Right now it breaks because player is a local variable in level1.lua. This means it is not available in your Object.lua. Because it's local. To level1.lua.
If you provide the player as variable to update this error goes away. Though player doesn't seam to have the variable w.
But as I was trying to explain. You should really try to not use any global variables in your modules. Because that makes the whole abstraction into modules pointless.
Who is online
Users browsing this forum: Google [Bot], Semrush [Bot] and 3 guests