Difference in calling a function vs activating a group?

Discussion and feedback on Construct 2

Post » Mon Jun 13, 2016 11:53 pm

Is there a large difference in calling a Function vs Activating/Deactivating a group?

The only difference I have noticed is one extra event where you have to deactivate a group. I actually only used groups without any issues before knowing of Functions.

I may be using Functions wrong as well.
It seems they are meant for single use only. As in spawning an object.
Will using the same function constantly(non-stop) for something long term or possibly a loop cause issues? Possibly as in a timer that restarts when hitting 0, or controlling an animation frame based off of a variable?


All of the Capx files I have downloaded used function or nothing at all. I haven't seen anybody activating/deactivating groups as I have. Am I missing something? Is there a difference in performance?
B
59
S
33
G
13
Posts: 223
Reputation: 12,105

Post » Tue Jun 14, 2016 12:26 am

There are plenty of differences, and valid use cases for both.

Functions are a mostly to minimize repetitive code. Things that you're going to be doing over and over, sometimes with maybe just a few variables. They are also triggers, which means they fire once and done. Using them in a loop should not cause problems and is common.

Enabling/Disabling Groups can be used for a variety of things. Unlike a function, you can have non-triggered events running in a group. So that when a group is active, everything in it is running. This makes groups useful for turning off 'functions' (groups of events) that might be unnecessarily using up resources if they're running when they don't need to be (which, in my opinion, usually means your code could should probably be more effecient).

Many people activate/deactivate groups though, I assure you. There's absolutely nothing wrong with that.

Hope that helps!
B
86
S
43
G
71
Posts: 601
Reputation: 43,619

Post » Tue Jun 14, 2016 12:56 am

In addition to what @spacedoubt said:

- You can pass a parameter to the function, which you cannot do for groups.
- Toggling a group takes effect in the next tick while triggering a function takes effect in the same tick

It is true that you can in most common cases use functions or groups and you will have the same effect. You can add "Trigger once" to the gorup and the code will run only once - like in a function. But it's not always replaceable and it's much about semantic to use both in a right way.

Groups help you encapsulate your code and mainain the overall performance by smart toggling. Functions are more like helpers for common (for your project) actions.
ImageImage
B
27
S
16
G
68
Posts: 934
Reputation: 38,577

Post » Tue Jun 14, 2016 1:07 am

From my understandings if a group is activated but not in use, it checks the main event and skips it's sub events.
If a group is deactivated is it skipped on every tick or is the main event analyzed and ignored?

What about Functions? is it skipped on ticks unless called or analyzed as well?

I will most likely adopt Functions because it uses less events to call than to activate/deactivate a group and they seem like they act faster when called.
B
59
S
33
G
13
Posts: 223
Reputation: 12,105

Post » Tue Jun 14, 2016 9:50 am

The main difference is how they are run: a function will call the "On function" event, run it to completion, and then return to the "call function" action and continue from there. In other words it calls other events from within an action.

Activating a group just enables a bunch of events so that they can be read the next tick. It doesn't actually run anything at the point you say "activate group".
Scirra Founder
B
387
S
230
G
87
Posts: 24,249
Reputation: 192,240

Post » Tue Jun 14, 2016 3:40 pm

Ashley wrote:The main difference is how they are run: a function will call the "On function" event, run it to completion, and then return to the "call function" action and continue from there. In other words it calls other events from within an action.

Activating a group just enables a bunch of events so that they can be read the next tick. It doesn't actually run anything at the point you say "activate group".


Is it worth toggling groups to gain performance, or as I remember conditions that are not met just don't run?
My professional Royalty Free Music at Scirra Assets Store
--------------------------------
Specs: i5 2500, 16gb of ram, gtx 770, win 7, Focusrite Scarlett 8i6, Mackie mr8mk2, Alesis 320, browsing the net on chrome.
B
85
S
27
G
21
Posts: 1,966
Reputation: 19,159

Post » Tue Jun 14, 2016 8:27 pm

Disabling groups can help improve performance, since all the events in the group must still test their conditions against (possibly many) instances to determine that nothing meets the condition. It can keep executing through to later conditions in the same event as well before it determines the event won't run. So disabling the event can save all that work from happening. Or, it might make no difference at all. As ever you should measure it to determine if it's worthwhile.
Scirra Founder
B
387
S
230
G
87
Posts: 24,249
Reputation: 192,240

Post » Tue Jun 14, 2016 11:35 pm

Ashley wrote:Disabling groups can help improve performance, since all the events in the group must still test their conditions against (possibly many) instances to determine that nothing meets the condition. It can keep executing through to later conditions in the same event as well before it determines the event won't run. So disabling the event can save all that work from happening. Or, it might make no difference at all. As ever you should measure it to determine if it's worthwhile.


Thanks for the answer. Could I ask you one more question re this. What would be best way of toggling groups with single chain events like this one: For each Door, boo = 1? I'd think toggling based on distance, but that again would require to check all Door objects.
My professional Royalty Free Music at Scirra Assets Store
--------------------------------
Specs: i5 2500, 16gb of ram, gtx 770, win 7, Focusrite Scarlett 8i6, Mackie mr8mk2, Alesis 320, browsing the net on chrome.
B
85
S
27
G
21
Posts: 1,966
Reputation: 19,159


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 11 guests