Page 1 of 1

Cant switch back to a previous gamestate with hump

Posted: Sun Apr 16, 2017 5:24 am
by destinyschilde
So i'm using hump gamestates to go from area 1 to area 2, but i cannot use the same logic to go from area 2 to back to area 1. :cry: HELP!

here is area 1(core)

Code: Select all

local core = {}
Gamestate = require "hump.gamestate"
local outercore = require"states.outercore" 

function core:enter()
 --blablabla
end

function core:update(dt)
  input:update()
  x = x + speed * input:get 'right'
  x = x - speed * input:get 'left'
  y = y + speed * input:get 'down'
  y = y - speed * input:get'up'
    if playerHC:collidesWith(wall[6]) then
        Gamestate.switch(outercore)
    end
end

function core:draw()
--blablabla
end
return core


and here is area 2 (outercore)

Code: Select all

local outercore = {}
Gamestate = require "hump.gamestate"
local core = require"states.outercore"

function outercore:enter()
 --blablabla
end

function outercore:update(dt)
  input:update()
  x = x + speed * input:get 'right'
  x = x - speed * input:get 'left'
  y = y + speed * input:get 'down'
  y = y - speed * input:get'up'
    if playerHC:collidesWith(wall[6]) then
        Gamestate.switch(core)
    end
end


function outercore:draw()
--blablabla
end
return outercore

i have also tried(in area 2):

Code: Select all

    if playerHC:collidesWith(wall[6]) then
        return Gamestate.switch(core, outercore)
    end
but to no avail

This is destroying meeeeeee

Re: Cant switch back to a previous gamestate with hump

Posted: Sun Apr 16, 2017 6:26 am
by yetneverdone
Umm i havent used hump's gamestates before but I think it is about the wall[6] collision

Re: Cant switch back to a previous gamestate with hump

Posted: Sun Apr 16, 2017 8:55 am
by Sir_Silver
Is that problem that:

Code: Select all

local core = require"states.outercore"
Looks like what you think is core is actually your outercore, so you're just switching back to the same state you're already in?

If that's the case, just change the line to:

Code: Select all

local core = require"states.core"

Re: Cant switch back to a previous gamestate with hump

Posted: Sun Apr 16, 2017 10:35 am
by Tjakka5
Sir_Silver is correct; however, that will result a recursive require.
(Outercore tries to load core, core tries to load outercore, repeat)

The way to fix this is by loading them in in core:init

Core:

Code: Select all

local outercore

function core:init()
  outercore = require "states.outercore"
end
Outercore:

Code: Select all

local core

function outercore:init()
  core = require "states.core"
end
That should do the trick.

Re: Cant switch back to a previous gamestate with hump

Posted: Sun Apr 16, 2017 11:34 am
by zorg
Tjakka5 wrote: Sun Apr 16, 2017 10:35 am Sir_Silver is correct; however, that will result a recursive require.
(Outercore tries to load core, core tries to load outercore, repeat)
Since require keeps anything loaded in package.loaded, it won't do any extra work, so nothing (bad) will happen, this isn't c that you need to write include guards. :3

Re: Cant switch back to a previous gamestate with hump

Posted: Sun Apr 16, 2017 1:10 pm
by Tjakka5
zorg wrote: Sun Apr 16, 2017 11:34 am
Tjakka5 wrote: Sun Apr 16, 2017 10:35 am Sir_Silver is correct; however, that will result a recursive require.
(Outercore tries to load core, core tries to load outercore, repeat)
Since require keeps anything loaded in package.loaded, it won't do any extra work, so nothing (bad) will happen, this isn't c that you need to write include guards. :3
Correct me if Im wrong, but I don't think that buffer helps in this case.

Lets assume we have 2 files, foo and bar, which need to require each other:
foo.lua:

Code: Select all

local bar = require("bar")
local foo = {}

return foo
bar.lua:

Code: Select all

local foo = require("foo")
local bar = {}

return bar
If I now call 'require("foo")' in main.lua it will thus load foo.
It executes the first line, requiring bar.
It executes its first line, requiring foo.
It can not grab foo from the buffer, since it has not executed completely yet.

Thus this cycle will repeat, resulting in a recursive require, which will eventually error with a stack overflow.

Re: Cant switch back to a previous gamestate with hump

Posted: Sun Apr 16, 2017 2:36 pm
by destinyschilde
Tjakka5 wrote: Sun Apr 16, 2017 10:35 am Sir_Silver is correct; however, that will result a recursive require.
(Outercore tries to load core, core tries to load outercore, repeat)

The way to fix this is by loading them in in core:init

Core:

Code: Select all

local outercore

function core:init()
  outercore = require "states.outercore"
end
Outercore:

Code: Select all

local core

function outercore:init()
  core = require "states.core"
end
That should do the trick.
YEP! so my problem was that i wasn't declaring my gamestates in the :init function. It works now.
Thanks yall.

Re: Cant switch back to a previous gamestate with hump

Posted: Sun Apr 16, 2017 3:37 pm
by zorg
Tjakka5 wrote: Sun Apr 16, 2017 1:10 pm...
Whoops, you were right! Just tested, though it doesn't cause a stack overflow since lua's require is smart enough to detect the loop.
I probably confused this with requiring in an init function, as was suggested.

Re: Cant switch back to a previous gamestate with hump

Posted: Sun Apr 16, 2017 7:28 pm
by MasterLee
zorg wrote: Sun Apr 16, 2017 3:37 pm
Tjakka5 wrote: Sun Apr 16, 2017 1:10 pm...
Whoops, you were right! Just tested, though it doesn't cause a stack overflow since lua's require is smart enough to detect the loop.
I probably confused this with requiring in an init function, as was suggested.
That is interesting. So i tested it in python.
a.py

Code: Select all

import b
and
b.py

Code: Select all

import a
It worked without problems

now when i import a in some other file i can access everything in a with a.b.a.b.a.b.a…
But why doesn't it work in lua or better how did it work in python

Re: Cant switch back to a previous gamestate with hump

Posted: Sun Apr 16, 2017 11:46 pm
by zorg
MasterLee wrote: Sun Apr 16, 2017 7:28 pm But why doesn't it work in lua or better how did it work in python?
The simplest and most meaningless answer would be that python and lua probably does it differently.
but, here are some links i found while googling in the wilderness of the internet :3
http://stackoverflow.com/questions/7443 ... -in-python
http://effbot.org/pyfaq/how-can-i-have- ... -other.htm