Best practice for separating logic/draw calls?

,

Hey all, just a basic Lua/SDK best practice question (I'm new to this whole coding thing).

  1. Is there a best practice for separating logic and drawing in your code?
    Currently I have various screens/scenes being managed using a code block that looks like this:
function pd.update()
  timer.updateTimers()

  if gameRunning then
    playGame()
    drawGame()
  elseif gameStarting then
    startingGame()
  elseif failGame then
    failState()
  elseif showSettings then
    settingsMenu()
  elseif showHighScores then
    highScoreMenu()
  else
    resetGame()
    mainMenu()
  end
end

Naming conventions aside (I'll fix those shortly), I started with just gameRunning, with playGame() and drawGame() to separate concerns, which is working well. Then when I started adding simple screens for things like highScores, or mainMenu, I didn't separate these, and am now in the situation where I'm not sure whether it's better to have a single draw function which handles the drawing of everything from all the other screens, or whether it's better to have everything split into separate draw calls per-screen?

Once a Lua codebase gets large enough, I find it helpful to break things down into sort of mini-applications - each one having their own button handling and 'update' function.

Sort of like (paraphrased):

main.lua:

import 'menu_main'

updateOriginal = nil
updatePrevious = nil
:
function onGameStart()
  updateOriginal = playdate.update
  updatePrevious = playdate.update
:
end

function AssignUpdate(update)
  updatePrevious = playdate.update
  playdate.update = update
end

function AssignInput(aButton, bButton, upButton, downButton, leftButton, rightButton)
  playdate.AButtonUp = aButton
  playdate.BButtonUp = bButton
  playdate.upButtonUp = upButton
  playdate.downButtonUp = downButton
  playdate.leftButtonUp = leftButton
  playdate.rightButtonUp = rightButton
end

function playdate.update()
  MenuMain:AssumeUpdate()
end

menu_main.lua:

MenuMain = {}
:
local function updateMenuMain()
  -- this is the 'update' function for the main menu, ours has a list view
end
:
local function AButtonUp()
:
  if menuOptions[listview:getSelectedRow()] == "Resume Game" then
    Level.loadLevel(CurrentLevel, PlayerX, PlayerY, PlayerHeading)
    Level:AssumeUpdate()
 end
:
end
:
function MenuMain:AssumeUpdate()
  AssignInput(AButtonUp, BButtonUp, upButtonUp, downButtonUp, nil, nil)
  AssignUpdate(updateMenuMain)
end

and 'level.lua' has it's own 'AssumeUpdate(...)' and 'AssignInput(...)' etc. etc.

Our current game project has nearly 100 'Foo:AssumeUpdate(...)' update function assignments (where 'Foo' is a given module with its own 'update' function').

Just one approach, I'm sure there are other ways folks are dealing with complexity :slight_smile:

I thought I'd read a thread related to this topic last year... here it is:

Best practices for having different update() loop code for different “scenes”?

1 Like

Thank you! Tried to search before posting but didn't find that one.

1 Like