Discord
Login
Community
DARK THEME

When do i use the Update function and when not?

So i just started making my first little projects. I just dont know when i need to use the update function and when not. It feels like you need to use it everytime and there is no point in making a normal script. Allso my sprite keeps getting drawn when i move how do i fix that?

Greetings LeBoom

Well the update function is used whenever you want to implement physics or mechanics in your game, things that need to be checked 60 times per second. For example, a player's movement and attack.

player = object
  x = 0
  y = 0
  attack = function()
    print("ATTACK!")
  end
end

update = function()
  player.x += (keyboard.RIGHT-keyboard.LEFT)*2
  player.y += (keyboard.UP-keyboard.DOWN)*2
  
  if keyboard.press.SPACE then
    player.attack()
  end
end

There's also draw, which is similar to update, except it doesn't run 60 times per second. Instead, it's used per frame. So, if you have 60 fps, it will be used 60 times; if you have 40 fps, it will be used 40 times. Therefore, its usefulness isn't for mechanics like update, but rather for updating the image you want to display with each frame. For example, from the example above, let's say I want to draw the player's position:

draw = function()
  screen.clear()
  screen.drawSprite("icon",player.x,player.y)
end

While the update function changes the player's position and detects key presses 60 times per second, draw only uses the player's already modified position to draw them on the screen.

Now for some interesting questions:

1. Why not use update within draw?

Because if you do, you're telling the system that your mechanics, or in this case, the player's position change, will be used per frame. In other words, the mechanics won't be used 60 times per second, but rather according to your frame rate (FPS). If you have 40 FPS, the mechanics will be slow; if you have 120 FPS, your mechanics will be very fast. This ruins the experience for other people who have different FPS than average. Also, if your game has some lag (since the FPS isn't stable), it will cause certain speed variations in your mechanics.

2. Why not use draw within update?

Because if you do this, you'll create unnecessary processes, since the FPS isn't stable. So, even if everything seems fine, you're essentially asking the system to draw things 60 times per second, which is equivalent to 60 FPS. Therefore, if you have 40 FPS, there will be 20 frames that won't be used for anything at all, since you're asking a system that can barely handle 40 FPS to display things at 60 FPS.

But in summary:

  • update: Place mechanics or things that need to be updated 60 times per second, such as player movement, collisions, etc.

  • draw: You place everything displayed on the screen according to the system's FPS.

Regarding your last point about the sprites continuing to be drawn, I don't quite understand, but I assume you mean that multiple sprites are displayed instead of one. To fix this, you need to clear the screen, because for each frame, MicroStudio repaints over the current image. That's why previous sprites aren't erased, so the best solution is to paint everything in a single color:

draw = function()
  screen.clear() // Clean the screen
  // Place your sprites, etc...
end

Thanks so much it really helped.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community