Initial Setup

Like having a initial setup for a whole program, is there a way to have initial setup for a gameState using finite state machines. Right now, what is happening is that a physics object is being drawn for the run state but is invisible.The problem here is that I’m using @Reefwing’s tutorial and making spheres fall down from the top during the menu state. I only need the physics object (a wall) to be drawn once, so that is why I’m not putting it in the draw function for the actual run state. please help!! I’m new to codea and am getting really frustrated with these sort if things. Sorry if I’m spamming with these questions. :frowning:

Why don’t you try physics.pause()?
And if you need a setup for each game state, you should have a function to change from one state to another, so put all your setup code in there.

pausing the physics won’t work as it will pause all of the physics. I’m interested in creating a function from changing from once state to another. right now, I’m just using function draw() to change from one state to another and drawing the different states. How do you think I would go about doing this? :-?

How are you using draw() to change states?

How are you using draw() to change states?
I usually do it like =

function setup()
    STATE_MENU = 1
    STATE_PLAYING = 2
    STATE_PAUSED = 3
    STATES = {
        drawMenu,
        drawGame,
        drawPause
    }
    ...
end

function draw()
    STATES[STATE]()
    ...
end

function drawMenu()
    ...
    -- I usually use my own button function to do this, it is basic,
    -- but it automatically runs the function passed to it when
    -- it is called. The syntax is button(x, y, w, h, action) (x, y, w, h
    -- draw a rect with the current rectMode)
    button(WIDTH/2, HEIGHT/ 2, 300, 200, startGame)
end

function startGame()
    -- Destroy your menu physics here, and create the new ones too.
    STATE == STATE_PLAYING
end

...

I left out the rest of the functions as I am making this up as I go along (My dad has poached my iPad for a week) so if there is a syntax error, I apologize.

I started off using @Reefwing’s minesweeper game and broke it down to he basics. I change states like this.


function draw()

   -- The main drawing function, called 60 times per second if possible.
   -- This sets a black background color

   background(255, 255, 255, 255)

   -- Calculate and display FPS and track game time if game running.

   frameCount = frameCount + 1
   timeInterval = timeInterval + DeltaTime

   if timeInterval > 1 then
       if gameTimerOn then
           -- update gameTime roughly every second.
           gameTime = gameTime + 1
       end
       FPS = math.round(frameCount / timeInterval)
       timeInterval = 0
       frameCount = 0
   end
    if gameState ~= stateMenu then
        -- reinitialise variables if not in the Menu screen
        menuTime = 0
        menuMineCtr = 0
    end
   -- Draw appropriate screen based on gameState

   if gameState == stateSplash then
       splashScreen: draw()
   elseif gameState == stateMenu then
      
       drawMenu()
       
   elseif gameState == stateRun then
       Game:draw()
       mLocX3 = WIDTH - 259     
       pauseButton:draw()

   elseif gameState == gameOver then
     
       
   elseif gameState == stateHowToPlay then        
       drawDirections()
   elseif gameState == statePause then 
       
       drawPauseScreen() 
   elseif gameDifficulty == stateEasy then
   
   elseif gameDifficulty == stateMedium then
        
   elseif gameDifficulty == stateHard then
   
   
        
   end
end

But where is your code that says gameState = stateRun or gameState = stateMenu?

@Jordan here in the setup function

   stateSplash = 0
   stateMenu = 1
   stateRun = 2
   stateGameOver = gameOver==true 
   stateHowToPlay = 4
   statePause = 5
   gameState = stateSplash
   

I do it like this:

-- VirtPet Project

-- set Finite State Machine:
-- States available are "title", "menu", "cat", "dog", "petmenu"
gameState = "title"


function setup()
    displayMode(FULLSCREEN)
    rectMode(CENTER)
    
    buttons = {} -- A place to store buttons (buttons will be draw in individual classes
    titleScreen:init()
    LoadSaveScreen:init()
    mainMenu:init()
    virtDog:init()
end


function draw()

    background(255);fill(0)

    -- ONLY DRAW CLASSES IF FSM IS SET TO DRAW THEM
    if gameState == "title" then
        titleScreen:draw()
    elseif gameState == "ls" then
        LoadSaveScreen:draw()
    elseif gameState == "menu" then
        mainMenu:draw()
    elseif gameState == "dog" then
        virtDog:draw()
    end
    
end

function touched(touch)
    -- ONLY ALLOW TOUCH FUNCTIONS IF FSM IS SET TO INDIVIDUAL CLASSES
    if gameState == "title" then
        titleScreen:touched(touch)
    elseif gameState == "ls" then
        LoadSaveScreen:touched(touch)
    elseif gameState == "menu" then
        mainMenu:touched(touch)
    elseif gameState == "dog" then
        virtDog:touched(touch)
    end
    
end

function keyboard(key)
    if gameState == "ls" then
        LoadSaveScreen:keyboard(key)
    end
end

I prefer string names to numbers, so I won’t be confused if I choose to pick up an abandoned project

No, as in

if buttonPressed == true then
    gameState = stateRun
end

here’s something like that for my title screen:

function titleScreen:touched(touch)
    -- Codea does not automatically call this method
    if touch.state == ENDED then
        gameState = "menu"
    end
end

So in that if clause, initialize all your physics objects.

@Jordan how would u do that and what is the code for that? Does it go in a separate class? My code is very similar to @RichGala1 as in changing state like that.

You would just cut the code from your setup function where you make the objects, and give them their properties, and paste it to the if clause. If I provided code for you it would be messy, (probably) full of mistakes (My dad has taken the iPad for a while…), and you would have to adapt it to fit your code. If you really want me to put up code could you post yours?

if I were to paste my code, I would have to paste a total of seven classes. I’m in a car right now and my iPad doesn’t have wifi. I’m doing this through my iPhone

I got it working guys I just put the function for creating the wall where I set the different game parameters for each difficulty

O.o Ok. I’m stuck for now…
P.S. You can copy an entire project by selecting the entire project from the Codea menu, hold your finger over it, and select copy. It will be copied as text.