Page 1 of 1

What's best to keep a value for only 1 frame?

Posted: Tue Oct 05, 2021 4:32 am
by Eglaios
I'm storing keypresses detected from love.keypressed() into the var "KEY" (for other functions), but only need it for 1 frame, and so I added "KEY = nil" at the end of love.update.

So, is it better to have "KEY = nil" there, or "if KEY then KEY = nil end"? Would there be a simpler or "official" way to keep values only for a single frame?

Thanks!

Re: What's best to keep a value for only 1 frame?

Posted: Tue Oct 05, 2021 5:43 am
by zorg
KEY = nil is fine, nothing bad happens if you nil something that was nil already.

That said, with this method, you can only store one specific key; if you'd press multiple during one frame, only one of them would be saved.

Re: What's best to keep a value for only 1 frame?

Posted: Tue Oct 05, 2021 6:42 am
by Eglaios
zorg wrote: Tue Oct 05, 2021 5:43 am if you'd press multiple during one frame, only one of them would be saved.
Guess I'll use a table for that then... hopefuly, there're not many places in my game where I have to change this (so far...)