Quick performance question - Event Layout

For questions about using Classic.

Post » Sun Dec 11, 2011 3:46 am

Hello,
I was just hoping to know about the effect on performance a layout
of functions might have between 2 setups.

I have a large number of events sharing some conditions, and I wanted
to know whether it would be better to keep them somewhat separated
as they are now, or branch them all under a single event?
Because there would be a lot of sub-events and I'm unaware if this
is a problem, like would it have trouble constantly checking through
so many sub-events and sub-sub events, or would it be easier for it, or?

I'd appreciate any responses about this please ^^;sparkfeather2011-12-11 03:47:06
B
3
G
2
Posts: 58
Reputation: 1,166

Post » Sun Dec 11, 2011 8:20 am

If a base event's conditions are all true, construct runs the actions of that event and proceeds to check the conditions of any subevents of that base event. If instead any of the conditions of that base event are not true, then it skips running the actions and doesn't check any of the subevents, skipping them completely and running the next base event.

If you have two events:

global variable = 1
space is pressed

global variable = 1
space is not pressed

They can be made more efficient as:

global variable = 1
- space is pressed
- space is not pressed

Resulting in events 2 and 3 only being checked if the global variable = 1.

Sub events used that way can make things very efficient, with huge trees of code only running if they need to. I have over 5000 events in my battle engine in my RPG and it runs smooth as silk because they're set up so that only the relevant event trees are running at any one time with subevents. Does that make sense?
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Sun Dec 11, 2011 8:34 pm

Thanks for the well explained response ^^.

That does make sense, however I'm wandering if that works with conditions
that are true most of the time.
So if a condition is true most of the time, but the other conditions in
the events that would be below it aren't, would it be better to have that
condition cover them all, or would it be better to place that condition into each individual sub-event?


As an example:
===============
space is not pressed
- global variable = 1
- global variable = 2
===============
or-
===============
space is not pressed
global variable = 1

space is not pressed
global variable = 2
===============

Does that make sense?
B
3
G
2
Posts: 58
Reputation: 1,166

Post » Sun Dec 11, 2011 10:24 pm

Think of it this way - each condition requires construct takes some processing time to determine if it's true or not. Some conditions are more resource intensive than others, like collision detection. There's no point to your second example because it just wastes cpu time checking if space is not pressed twice.

Event one:
If sprite1 is overlapping sprite2
Global variable = 1

Event two:
If sprite1 is overlapping sprite2
Global variable = 2

That would result in two collision checks when only one is needed.

Event one:
If sprite1 is overlapping sprite2
Sub event one:
- Global variable = 1
Sub event two:
- Global variable = 2

The way above would only check for collisions once. If no sprite1's are overlapping sprite2's, then sub events one and two are not checked at all. This way the runtime doesn't have to waste time checking the same conditions over and over again.

Another option for efficiency in this case is reversing the order of the conditions, because checking a global variable is far, far faster than collision detection.

Event one:
Global variable = 1
Sub event one:
- If sprite1 is overlapping sprite2

Event two:
Global variable = 2
Sub event one:
- If sprite1 is overlapping sprite2

This way the collision check is only run once as well even though there are two conditions for it. Another benefit is now the collision check is only run when it's needed, not every frame. In this case however, it's not even necessary to have sub events, as this example is functionally the same:

Event one:
Global variable = 1
If sprite1 is overlapping sprite2

Event two:
Global variable = 2
If sprite1 is overlapping sprite2

If the global variable = 3, then neither of the collision checks are run, because since the first condition was not true, it skips the rest of the conditions and actions in the event.Arima2011-12-11 22:27:48
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Tue Dec 13, 2011 12:28 am

I'm really sorry for such a delay in my response after you responded so quickly, been a bit crazy here atm.


That explains everything further, thank you so much!
However, it was almost perfect until it contradicted my main reasoning
for my questioning. But that would be my fault for having my examples
have the second condition as a sub-event.


One of the things I wanted to know was whether it would be better to
separate events with the same conditions to different areas,
as in your first example there, or whether that would reduce performance.
You explained that it was silly because it would have to check it twice,
answering me that I should keep them joined so I'll make sure to do
that in the future and keep it in mind when revising my event layout
in the current project.

Another question in my mind I tried to express, poorly, I'm sorry about that.
Was whether it would be more intensive for the project to have a large
number of sub-events under a single event, or whether it would be better
to keep them all separate and have that overall event as just another condition in each sub-event (making them main events), as is shown
in your last example.
Because you have said both in your explanation that it's better to
have it only check the condition once, but then your final example
it checks it both when global variable 1, and 2 are true alongside it.


So may I please ask, which is your preferred solution of these examples?

=========== #1 =
Event one:
Global variable = 1
Sub event one:
- Global variable = 2
Sub event two:
- Global variable = 3

=========== #2 =
Event one:
Global variable = 1
Global variable = 2

Event two:
Global variable = 1
Global variable = 3
B
3
G
2
Posts: 58
Reputation: 1,166

Post » Tue Dec 13, 2011 6:47 am

Sorry I confused you. I was getting a bit tricky with the examples where I switched the order of the conditions - basically forget that, because it seems to have only confused you.

I'll make it simple:

[QUOTE=sparkfeather]
===============
space is not pressed
- global variable = 1
- global variable = 2
===============
or-
===============
space is not pressed
global variable = 1

space is not pressed
global variable = 2
===============[/QUOTE]

Example 1 is better.Arima2011-12-13 06:48:24
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Tue Dec 13, 2011 1:03 pm

Awesome, great to know, thank you very much for that ^^


Thank you for all the time you have taken with your examples and explanations,
each part has helped in some manor and I'm vary grateful for your effort
in bringing me a conclusion.
It was all a lot of help! :)!!
B
3
G
2
Posts: 58
Reputation: 1,166


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 4 guests