Log In  

I am trying to use values in a table to dictate which scene is being shown in the game. I defined the table 'S' at the top of the code but a piece at the bottom referring to it comes back nil. What's the problem?

function _init()
cls()
music(0)

s={}
s.menu=1
s.start=0
s.t1=0
s.one=0

    map_setup()
    make_player()

end
    if s.menu>0 then
        screen_menu() end
    if s.start>0 then
        screen_start() end
    if s.t1>0 then
     screen_t1() end
    if s.one>0 then
        screen_scene1()
end 
P#98887 2021-10-20 09:37

It's hard to tell from just what you've shown. Here's some ideas just in case any of them help:

  • Check to make sure you didn't accidentally reused the name "S" somewhere else
  • Check which line number the error is indicating and see which statement that is, then look to see if you deleted the relevant value later.
  • Try commenting out portions of the bottom code to see if only some of it gives errors

In general, I would advise posting the actual error message you get when asking for help with errors.

P#98898 2021-10-20 11:21

_init() is called after code evaluation. if the second code snippet you posted is executed before _init(), the table 's' would not yet have been defined.

P#98901 2021-10-20 12:42

there are 3(4) special functions.

_init() - will be run onced, after the code is loaded and all commands outsides functions are executed

_update() / _update60() - this function will be called every 1/30 or 1/60 second. you should place your gamelogik here

_draw() - this function will be called to draw a screen. Important: It is not definied how often - it could be every 1/60s, 1/30s or even 1/15s. you should not place any gamelogik here.

by the way - you seems to want to create a "gamestate" - show menu, play game, endscreen and so on. I use this dispatcher:

function _init()
  start="mainmenu"
end

function _update()
  -- for the draw-routine, state could be changed!
  oldstate=state 
  -- report an error, if the state doesn't exist
  assert(update[state]!=nil,"unknwon state:"..state)
  -- call the state-dependend update-function
  -- remember you can store functions in tables!
  update[state]()  
end

function _draw()
  if (draw[oldstate]!=nil) draw[oldstate]()
end

-- initalize the table
update, draw = {},{}

-- state "mainmenu"
function update.mainmenu()
  -- do menu stuff, at some point change the state
  state="play"
end
function draw.mainmenu()
  -- draw the menu
end

-- state "play"
function update.play()
  -- playing and finally
  state="endscreen"
end
function draw.play()
  -- drawing
end

-- state "endscreen"
function update.endscreen()
  -- hiscore and so on
  state="reset_to_menu"
end
function draw.endscreen()
  -- drawing
end

-- state "reset to menu"
function update.reset_to_menu()
  -- do some reset  
  state="menu"
end
-- a draw.reset_to_menu is not required
P#98908 2021-10-20 17:06 ( Edited 2021-10-20 17:19)

[Please log in to post a comment]