Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Does ammount of functions affects the optimization of behavior?

dekon_17
Registered User
Quote
2022-02-28 17:06:56

Working on my game, I made a weapon system (not really the Simple Weapon System that I published here, but it is based on it), and I start to think that the ammount of functions may not be good for its optimization.

So, if I have 4 functions (shooting, firerate, pressed buttons, recoil), will it make script so unoptimized that it can start work bad? And will it affect the game performance if I'll use the behavior on 10 weapons (while 2 of them can be used at the same time due to dual wielding)?


Robo
Guest
Quote
2022-03-01 03:38:55

By functions if you mean an actual function like var dosomething = function () {..xxx...} then wont be a problem at all...its better to use functions rather than re writing the same code over and over in different places.

Behaviours however run every frame and so will use up system resources if you have lots of them - better to create once off Actions instead so that it stops running when not needed.

I myself plan to inlcude most of my game code into a single Behavior with parts of it running at different timings (speed) for better performance. In general Actions work great though and best used as much as possible.


dekon_17
Registered User
Quote
2022-03-01 05:06:37

Well, then I don't really think that this will cause me the problems, because, by default, only onMouseEvent and onAnimate functions are register on frame (and I'm not even sure about onMouseEvent, because it only responds to mouse keys being pressed). The fire rate and recoil are registered on frame only when shooting is initiated (and unregistered when they done their job).

So... Is this fine or it's better to change something?


Robo
Guest
Quote
2022-03-01 21:35:30

sounds ok to me...add a 2d overlay with a frame per second counter if you want to confirm no loss of playable frames per second - I always add that in to confirm nothing is causing issues...


csp-games
Guest
Quote
2022-03-04 18:27:19

Calling a function has a tiny impact because the JS interpreter must put the parameters on a stack, also put the return address on a stack, then branch to the function, and at the end, read the return address from the stack and jump back to the line after the function call. So something like:

function min(a,b)
{
if(a<b) return a;
return b;
}

may be handy, but it also makes execution slower, and if you call this in a loop a thousand times, it may become noticeable.

Generally speaking, too many calls, esp. ccb commands such as Linepicks, can slow things down a lot, esp. in the WebGL target, but also on the other platforms, because it will also be interpreted, and not compiled.

Always try to keep it simple.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Inte?national" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact