Page 1 of 1

I don't know wtf is going on with this

Posted: Wed Feb 22, 2012 11:19 pm
by jradich
Main.lua

Code: Select all

function love.keypressed(k)
	if k=='1' then 
		require"story1"
	end
end
story1.lua

Code: Select all

function love.load()
   q=''
   w=''
   e=''
   r=''
   t=''
   y=''
   z='1'
end
function love.update(dt)

end
function love.keypressed(k)
   if k=='x' and z=='1' then
      q='Why hello there'
   elseif k=='x' and z=='2' then
      w='You wake up in a dark, muddy hole. What do you do?!  A: Get out B: Die'
   elseif k=='a' and z=='3' then
      e='You climbed out! Now what do you do?  A:Search for food B:Search for civilization'
   elseif k=='b' and z=='3' then
      e='Wow.'
   elseif k=='a' and z=='4' then
      r='You stumble upon a big sign that says "Good job!  You looked at the Early Version!"'
   elseif k=='b' and z=='4' then
      r='You stumble upon a big sign that says "Good job!  You looked at the Early Version!"'
   end
end
function love.keyreleased(k)
   if k=='x' and z=='1' then
      z='2'
   elseif k=='x' and z=='2' then
      z='3'
   elseif k=='a' and z=='3' then
      z='4'
   elseif k=='b' and z=='3' then
      love.event.push('q')
   else if k== 'a' or 'b' and z=='4' then
      love.event.push('q')
      
      end
   end
end
function love.draw()
   love.graphics.print('Press x',0,0)
   love.graphics.print(q,0,10)
   love.graphics.print(w,0,20)
   love.graphics.print(e,0,30)
   love.graphics.print(r,0,40)
   love.graphics.print(t,0,50)
   love.graphics.print(y,0,60)
end
For some reason, this doesn't work when I load story1.lua. Can someone help? When i try it, I get:
Error

story1.lua:45 bad argument #1 to 'print' (string expected, got nil)

Traceback

[C]: in function 'print'
story1.lua:45: in function 'draw'
[C]: in function 'xpcall'

Re: I don't know wtf is going on with this

Posted: Thu Feb 23, 2012 2:37 am
by rvj
After a quick look and no testing I've got this: when do you expect love.load() to be called? Not when the script loads.. right? Because if you wanted it to be called when the script loaded, you would have put it in the main.lua.

So it doesn't get called ever. Your story1.lua gets loaded, but the load() function doesn't ever get called. Since all those variables are global anyway I think you could fix it by just having:
q=''
w=''
e=''
r=''
t=''
y=''
z='1'

instead of
function love.load()
q=''
w=''
e=''
r=''
t=''
y=''
z='1'
end

edit: put simply: love.load() gets called when the program starts; not when your file is 'require'd

Re: I don't know wtf is going on with this

Posted: Thu Feb 23, 2012 4:13 am
by MarekkPie
I think you're obscuring your actual point, rvj.

There are two things wrong here. One, is what rvj is saying, is that you never actually call your load function inside story1.lua. "require" exposes variables within the main scope of the file, and nothing more. So when you do "require story1.lua", LOVE/Lua goes:

Code: Select all

function love.load() -- <= I recognize love.load() as a function variable, and I expose its use to the file that required it
    a = "a"  -- <= I will not see these variables until someone calls love.load()
    b = "b"
end
The reason it works inside main.lua is because in the background, the actual game engine is calling it. See love.run().

So your options are to do what rvj suggested, which is expose those variables to the file's main scope, or to call love.load() inside main.lua. Like:

Code: Select all

function love.keypressed(k)
    if k == "1" then
        require "story1"
        love.load()
    end
end
This, however, leads to our second issue. Suppose I wanted to make several stories ("story1", "story2", ...) If I named all of the functions inside each story "love.load()" or "love.update(dt)", then LOVE would not be able to distinguish one from another. This is one of the reasons why LOVE only likes to have one instance of its main callbacks ("love.load()", "love.update(dt)", "love.draw()", etc.).

There are two ways you can fix this. You can give each story function a unique name, like "story1load", "story2load", or you can encapsulate the function in what is called a namespace (which is just a fancy renaming of a table):

Code: Select all

Story1 = {}

function Story1.load()
end

function Story1.update(dt)
end

function Story1.draw()
end
That way, when you make more stories, you have a common theme to build around. You could even make nicer ways to traverse between stories:

Code: Select all

-- story1.lua
local Story1 = {} -- notice the local

function Story1.load()
end

function Story1.update(dt)
end

function Story1.draw()
end

return Story1

Code: Select all

-- story2.lua
local Story2 = {}

-- same as story1.lua, but change the 1 to a 2

return Story2

Code: Select all

-- main.lua

function love.load()
    Stories = {require "story1", require "story2"} -- story1 is given an index of 1, story2 is given an index of 2, etc...
end

function love.keypressed(k)
    if k == "1" or k == "2" then
        Stories[k].load()
    end
end

Re: I don't know wtf is going on with this

Posted: Thu Feb 23, 2012 12:13 pm
by jradich
Thank you, Marekk Pie. That will help indeed.