deltadaedalus/vudu

Having trouble getting vudu.hook() to work

Closed this issue · 2 comments

Vudu by itself now loads and runs fine with Love2D 11.3
Winfield (Love2D) physics wrapper runs fine with Love2D 11.3

However, when I try and combine the two, Love 2D crashes. It seems to be crashing due to the use of the vudu.hook() function which I understand from the readme that I need to use when using love.update or love.draw. If I just load (require) the Vudu library, then my test Winfield project runs fine (but I can't get Vudu to do anything). If I put one vudu.hook() in (e.g. at love.update), then the project starts to run the Winfield physics demo briefly then crashes (no error message-- just puts me back at the Love2d directory of .love files screen). If I put two vudu.hook()s in (e.g. in love.update and love.draw functions) then the Winfield project very briefly starts then crashes similarly without an error code and back to to the Love2d directory of .love files screen. Here is my main.lua script:

wf = require('libraries/windfield')
vudu = require('libraries/vudu')
vudu.initialize()
world = wf.newWorld(0, 0, true)
world:setGravity(0, 512)
box = world:newRectangleCollider(400 - 50/2, 0, 50, 50)
    box:setRestitution(0.8)
    box:applyAngularImpulse(5000)

    ground = world:newRectangleCollider(0, 550, 800, 50)
    wall_left = world:newRectangleCollider(0, 0, 50, 600)
    wall_right = world:newRectangleCollider(750, 0, 50, 600)
    ground:setType('static') -- Types can be 'static', 'dynamic' or 'kinematic'. Defaults to 'dynamic'
    wall_left:setType('static')
    wall_right:setType('static')
	photo = love.graphics.newImage("assets/IMG_1168.JPG")
--
function love.update(dt)
    world:update(dt)
	vudu.hook()
end
--
function love.draw()
    world:draw() -- The world can be drawn for debugging purposes
	px,py = box:getPosition()
	love.graphics.draw(photo,px,py,0,0.45,0.45,photo:getWidth()/2,photo:getHeight()/2)
	vudu.hook()
end

Am I using vudu.hook() incorrectly? Any other suggestions on how to get vudu to work with the Winfield Love2d physics wrapper library?

Thanks!

Thanks for putting your code up, that helped me a ton to figure out the issue!

vudu.hook only needs to be called after you do something like "love.update = x", since re-overriding these callbacks causes vudu to lose the hooks it has into the game loop that allow it to run inside your game. Calling it here doesn't fit that case, and indeed seems to be causing the quiet crash, I'd theorize because of some call stack corruption caused by the monkey-patch vudu.hook does.

The reason vudu wasn't coming up for you in the first place is that you're calling initialize at the start of the file, instead of in love.load(). In order to work, vudu needs the love callbacks to have already been defined, which they haven't been at the start of the file. For what it's worth, best practice is generally that all the code you have after your require statements and before love.update should be inside of a love.load().

When I remove the love.hook() calls and move vudu.initialize() into love.load(), the code provided works (although with thick lines from a dirty graphics state, which you can fix with love.graphics.setLineWidth, and which I've noted down that I should fix in the general case at some point)

Thanks!

Works perfectly now with those changes, thanks for the tips! I can now learn more about how love.physics works by looking at the program variables in real time :-)