HUMP - yet another set of helpers
Re: HUMP - yet another set of helpers
So... why is there no mousepressed callback in the Gamestate thing? I tried implementing it myself and it just doesn't work. I don't understand...?
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: HUMP - yet another set of helpers
Araqiel wrote:So... why is there no mousepressed callback in the Gamestate thing? I tried implementing it myself and it just doesn't work. I don't understand...?
Code: Select all
--[[Copyright (c) 2010 Matthias Richter
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
Except as contained in this notice, the name(s) of the above copyright
holders shall not be used in advertising or otherwise to promote the
sale, use or other dealings in this Software without prior written
authorization.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]]--
Gamestate = {}
local function __NULL__() end
function Gamestate.new()
return {
enter = __NULL__,
leave = __NULL__,
update = __NULL__,
draw = __NULL__,
keyreleased = __NULL__,
keypressed = __NULL__,
mousereleased = __NULL__,
mousepressed = __NULL__,
}
end
function Gamestate.switch(to, ...)
if not to then return end
if Gamestate.current then
Gamestate.current:leave()
end
local pre = Gamestate.current
Gamestate.current = to
Gamestate.current:enter(pre, ...)
end
local _update
function Gamestate.update(dt)
if _update then _update(dt) end
Gamestate.current:update(dt)
end
local _keypressed
function Gamestate.keypressed(key, unicode)
if _keypressed then _keyreleased(key) end
Gamestate.current:keypressed(key, unicode)
end
local _keyreleased
function Gamestate.keyreleased(key)
if _keyreleased then _keyreleased(key) end
Gamestate.current:keyreleased(key)
end
local _mousereleased
function Gamestate.mousereleased(x,y,btn)
if _mousereleased then _mousereleased(x,y,btn) end
Gamestate.current:mousereleased(x,y,btn)
end
local _mousepressed
function Gamestate.mousepressed(x,y,btn)
if _mousepressed then _mousepressed(x,y,btn) end
Gamestate.current:mousepressed(x,y,btn)
end
local _draw
function Gamestate.draw()
if _draw then _draw() end
Gamestate.current:draw()
end
function Gamestate.registerEvents()
_update = love.update
love.update = Gamestate.update
_keypressed = love.keypressed
love.keypressed = Gamestate.keypressed
_keyreleased = love.keyreleased
love.keyreleased = Gamestate.keyreleased
_mousereleased = love.mousereleased
love.mousereleased = Gamestate.mousereleased
_mousepressed = love.mousepressed
love.mousepressed = Gamestate.mousepressed
_draw = love.draw
love.draw = Gamestate.draw
end
Re: HUMP - yet another set of helpers
@nevon I did exactly that... no luck. I ended up just calling Gamestate.current:mousepressed(...) in my own love.mousepressed(...).
EDIT: Oops, I used a . where I needed a :. It's been too long since I've used Lua.
EDIT: Oops, I used a . where I needed a :. It's been too long since I've used Lua.
Last edited by Araqiel on Sun Oct 31, 2010 6:22 pm, edited 1 time in total.
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: HUMP - yet another set of helpers
Err... That's very odd. I'm doing exactly that in one of my games, and it's working just fine, as long as you remember to run Gamestate.registerEvents() in your love.load().Araqiel wrote:@nevon I did exactly that... no luck. I ended up just calling Gamestate.current:mousepressed(...) in my own love.mousepressed(...).
Re: HUMP - yet another set of helpers
Can you give some (comprehensive) code example that does not work?Araqiel wrote:I tried implementing it myself and it just doesn't work.
Edit: added some missing callbacks (mousereleased, joystick*) and removed a bug (Gamestate.keypressed called love.keyreleased when using registerEvents). I also made the gamestates ignorant to the arguments passed to the callbacks, so you could use it in your custom love.run loops.
Re: HUMP - yet another set of helpers
may I ask a simple change in Gamestate?
passing "to" gamestate when calling "Gamestate.current:leave()"
thank you...
passing "to" gamestate when calling "Gamestate.current:leave()"
thank you...
Bartoleo
Re: HUMP - yet another set of helpers
I am not sure about that. state:leave() should usually only contain cleanup code, such as stopping audio sources.bartoleo wrote:passing "to" gamestate when calling "Gamestate.current:leave()"
The only use I see is to do different cleanup tasks depending on the following gamestate. But that would result in a big an cluttered leave() depending on how many follwing states you have.
If you just want to carry stuff over to the following gamestate, it is better to use the pre-parameter in state:enter() instead.
That said, you can easily do this modification yourself by changing line 62 of gamestate.lua to:
Code: Select all
Gamestate.current:leave(to)
Re: HUMP - yet another set of helpers
yes...
but I was using 'stacked states'
state1->state2->return to original state1 (game->pause->game or game->options->game or game->inventory->game...)
it' s working storing pre in enter 'event' plus with another optional parameter
but if I want to use leave callback
in this 'scenario' it shouldn't clean "all" but only sounds or similar...
another question:
I have a game gamestate
in this gamestate I create a 'physics world'
addCollision,persistCollision,remCollision,resultCollision
are defined as 'global' 'function' :
if I define it as 'class' function:
and I use setCallbacks:
why it gives me this error?
but I was using 'stacked states'
state1->state2->return to original state1 (game->pause->game or game->options->game or game->inventory->game...)
it' s working storing pre in enter 'event' plus with another optional parameter
but if I want to use leave callback
in this 'scenario' it shouldn't clean "all" but only sounds or similar...
another question:
I have a game gamestate
in this gamestate I create a 'physics world'
Code: Select all
-- physics
state.world = love.physics.newWorld(0,0,love.graphics.getWidth( ),love.graphics.getHeight()+state.trans_y+100,0, pLevel*100+200,true)
state.world:setMeter(30)
state.world:setCallbacks(addCollision, persistCollision, remCollision, resultCollision)
are defined as 'global' 'function' :
Code: Select all
function addCollision(a, b, coll)
Code: Select all
function state:addCollision(a, b, coll)
Code: Select all
state.world:setCallbacks(state:addCollision, state:persistCollision, state:remCollision, state:resultCollision)
Code: Select all
function arguments expected neat ','
Bartoleo
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: HUMP - yet another set of helpers
That's Lua:bartoleo wrote:and I use setCallbacks:why it gives me this error?Code: Select all
state.world:setCallbacks(state:addCollision, state:persistCollision, state:remCollision, state:resultCollision)
Code: Select all
function arguments expected neat ','
Code: Select all
a = {}
function a:b() end
--equivalent to:
function a.b(self) end
a:b()
--equivalent to:
a.b(a)
So use state.addCollision, etc.
Help us help you: attach a .love.
Re: HUMP - yet another set of helpers
Instead of "stacked" states (which I find not to be a good concept, because states encapsulate - well - state and stacking them stores implicit state in the order of stacking), you could use what would be best described as interruption.
An interruption hijacks all callbacks until it is finished, much like a modal dialog in GUIs. You can achieve this like so:Im guessing if something like this would actually make a good addition to hump. What do you think?
An interruption hijacks all callbacks until it is finished, much like a modal dialog in GUIs. You can achieve this like so:
Code: Select all
Pause = {}
function interrupt(interruption)
interruption.old_draw = love.draw
interruption.old_update = love.update
interruption.old_keyreleased = love.keyreleased
(...)
love.draw = function(...) interruption:draw(...) end
love.update = function(...) interruption:update(...) end
love.keyreleased = function(...) interruption:keyreleased(...) end
end
function continue(interruption)
love.draw = interruption.old_draw
love.update = interruption.old_update
love.keyreleased = interruption.old_keyreleased
end
function Pause.draw()
-- draw old screen content, but darkened
self.old_draw()
love.graphics.setColor(0,0,0, 128)
love.graphics.rectangle('fill', 0,0, love.graphics.getWidth(), love.graphics.getHeight())
-- draw pause stuff
love.graphics.setColor(255,255,255)
love.graphics.print('PAUSE', 25, 25)
draw_more_pause_stuff()
end
function Pause.keyreleased(key)
if key == 'p' then continue(Pause) end
end
What Robin said. In addition to his proposal, you could also use closures:bartoleo wrote:why it gives me this error?
Code: Select all
state.world:setCallbacks(
function() state:addCollision end,
function() state:persistCollision end,
function() state:remCollision end,
function() state:resultCollision end)
Who is online
Users browsing this forum: Bing [Bot] and 2 guests