Log In  


Hello everyone.

As I strive to become better at programming, theres one thing that is annoyingly very recurrent in my code which are counters.

I have counters for playing a sound effect only when changing scene, or when a collision first happens.

I have counters to manage the speed of swapping animation frames

I also have counters just to tell the passage of time or trigger elements or behaviours after a while.

My general approach is: Create a global time variable with the name for the thing it's timing

animationcounter = 0

Then on update or draw I have some sort of

if animationcounter >= 20 then
   sp +=1
   animationcounter = 0
else
   animationcounter +=1
end

Is there a better way to do this that can be used for all counters necessary? With a function? or maybe an object where we instantiate counters or something?

1


I am using an object that can obtain a counter, duration, start flag, and end flag.
It also has a function that obtains a current value separate from the counter from the ratio of the count and duration.

I will be using the following libraries and adding commands and functions.

KNUTIL Library

https://www.lexaloffle.com/bbs/?tid=32411

  • SCENE MANAGER
scmd[[
anm st animationcounter 20
]]
function animationcounter()
 if _lst then
  sp+=1
  scmd[[anm st animationcounter 20]]
 end
end

No additional global variables are required, and the counter will count automatically, but one line to generate the SCENE and one line to process the SCENE together in foreach are required.

uscene=mkscenes({'anm'})

function _update()
 foreach(uscene,transition)
end

3

It depends on your programming preferences. If you're satisfied with what you're currently doing, it's probably fine to keep doing it. If not, here's a couple alternatives that come to mind:

-- Attaching the timer to the relevant object

Basically, just add a value called "counter" to the game object in question and handle it whenever you update that object.

This is the method I usually use.

-- Make a table of counter objects and update all of them at once.

A version that uses named counters

counters = {animation={0,20,animfunc}, obj1={0,20,obj1func}, obj2={0,20,obj2func}}
function update_counters()
  for _,v in pairs(counters) do
    v[1]+=1
    if(v[1]>=v[2])v[1]=0 v[3]()
  end
end

A version that uses numeric indices instead

counters = {{0,20,animfunc}, {0,20,obj1func}, {0,20,obj2func}}
function update_counters()
  for _,v in all(counters) do
    v[1]+=1
    if(v[1]>=v[2])v[1]=0 v[3]()
  end
end

1

Thank you for your repliers

kimiyoribaka, after writing this post I was starting to play with something similar. Thank you for your version. It's easy for me to understand and also validating of my thought process.

shiftalow, wow, that looks advanced for me. I have no idea what the code does, it's using some patterns I have never seen. I have had a look at the library and it looks like thats the source of that confusion. Thank you for introducing that to me. I will try and learn it



[Please log in to post a comment]