Cant switch back to a previous gamestate with hump

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
destinyschilde
Prole
Posts: 7
Joined: Mon Jan 16, 2017 11:48 pm

Cant switch back to a previous gamestate with hump

Post 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
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Cant switch back to a previous gamestate with hump

Post by yetneverdone »

Umm i havent used hump's gamestates before but I think it is about the wall[6] collision
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Cant switch back to a previous gamestate with hump

Post 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"
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Cant switch back to a previous gamestate with hump

Post 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.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Cant switch back to a previous gamestate with hump

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Cant switch back to a previous gamestate with hump

Post 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.
destinyschilde
Prole
Posts: 7
Joined: Mon Jan 16, 2017 11:48 pm

Re: Cant switch back to a previous gamestate with hump

Post 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.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Cant switch back to a previous gamestate with hump

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Cant switch back to a previous gamestate with hump

Post 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
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Cant switch back to a previous gamestate with hump

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 77 guests