Does having multiple enterFrame events impact on performance? if so how much?
Thanks!
Does having multiple enterFrame events impact on performance? if so how much?
Thanks!
Why would you even need to do this? Just have 1 and call all your functions from within the listener.
You cannot always do this. Sometimes you want to add runtime listener for a while and then remove it later. Sometimes you do not know how many objects will require runtime. Breaking one to multiple will give you good event driven programming. But of course balance and moderation is needed. If you throw hundreds or thousands listeners or some time-consuming then it will affect performance.
I use one enterFrame function that dispatches several custom events. So I can easily turn parts of them on and off. It also reduces the errors that can occur, cause you only have one enterFrame loop that is responsible for everything.
local function enterFrame() if gameActive == true then player:dispatchEvent("game\_tick") end end runtime:addEventListener("enterFrame", enterFrame)
It’s okay to have multiple enterFrame listeners. Performance is determined by how much work each listener does. It’s just a function call at the end of the day, so you’re adding the overhead of that call.
Now thousands upon thousands of enterFrame listeners might start impacting things.
As to why you would want them? @piotzSS pretty much nailed it. Let’s say you want a Mario Bro’s type game and you want each coin to spin. You could have a big array of coins and have one enterFrame loop through all of them that spins them. But its more convenient to attach an enterFrame listener to the coin that just spins is.
I personally try to keep enterFrame listeners to a minimum, but I also don’t hesitate to use them when I need them either. That may seem counter-intuitive, but if you recognize that enterFrames run frequently and you don’t use it as a crutch to better ways, then when you do need them, you shouldn’t need to worry about them.
Thanks for the replies!
I don’t have thousands of enterFrame events but i do have a couple like 2-4. And in my situation i can’t have one enterFrame event doing all the stuff because my game is getting complex day by day so i have to seperate some dirty codes like dPad.
Why would you even need to do this? Just have 1 and call all your functions from within the listener.
You cannot always do this. Sometimes you want to add runtime listener for a while and then remove it later. Sometimes you do not know how many objects will require runtime. Breaking one to multiple will give you good event driven programming. But of course balance and moderation is needed. If you throw hundreds or thousands listeners or some time-consuming then it will affect performance.
I use one enterFrame function that dispatches several custom events. So I can easily turn parts of them on and off. It also reduces the errors that can occur, cause you only have one enterFrame loop that is responsible for everything.
local function enterFrame() if gameActive == true then player:dispatchEvent("game\_tick") end end runtime:addEventListener("enterFrame", enterFrame)
It’s okay to have multiple enterFrame listeners. Performance is determined by how much work each listener does. It’s just a function call at the end of the day, so you’re adding the overhead of that call.
Now thousands upon thousands of enterFrame listeners might start impacting things.
As to why you would want them? @piotzSS pretty much nailed it. Let’s say you want a Mario Bro’s type game and you want each coin to spin. You could have a big array of coins and have one enterFrame loop through all of them that spins them. But its more convenient to attach an enterFrame listener to the coin that just spins is.
I personally try to keep enterFrame listeners to a minimum, but I also don’t hesitate to use them when I need them either. That may seem counter-intuitive, but if you recognize that enterFrames run frequently and you don’t use it as a crutch to better ways, then when you do need them, you shouldn’t need to worry about them.
Thanks for the replies!
I don’t have thousands of enterFrame events but i do have a couple like 2-4. And in my situation i can’t have one enterFrame event doing all the stuff because my game is getting complex day by day so i have to seperate some dirty codes like dPad.