A Slight Improvement Of The Tick Code

While the presenter’s code is more intuitive and easier to see what is going on:
if (triggerActor)
{
if (!Activated)
Activated = true;
}
else
{
if(Activated)
Activated = false;
}
This replacement would be faster and more efficient:
Activated = triggerActor != nullptr;

Hi and welcome to the community.

What you have isn’t the same. What you need is something more along the lines of

If (triggerActor != nullptr)
{
  Activated = !Activated;
}

The code you pastes toggles activated when the trigger Actor isn’t null. What you were doing is setting activated based on whether or not the trigger actor is null

I hope that makes sense

1 Like

I don’t think that is right. Whenever a triggerActor is on the pressure plate, Activated is set to true. Otherwise it should always be false.
Activated = !Activated; would alternate between true and false every tick.

The only advantage of my suggestion is that it eliminates 2 conditional checks, (if statements) and possibly 1 jump command, (else) statement. It saves a few clock cycles each tick. Not a big deal if there are only a few pressure plates but if there were 100s, it would be significant. In Tick functions, I always try to make the code as efficient as possible regardless of how many objects use it.

Ok, The code formatting you pasted threw me off and missed the fact there was no curly braces around the setting of Activated. FYI, should always have them in if statements - seen too many logic issues because a user added or removed a line of code and totally changed the behaviour of the code as a result. I only picked this up when I looked at the gitlab code. You were right when you said you could just apply activated based on the state of the trigger actor. I did think it strange it was checking to toggle.

There is a reason to do it however. if you want to have debug messages being output, you only want a single message when the state changes. This is what is shown in the code for this lecture so setting it in a single line without any logging is fine. If you want to see the logging, you need to use the if statements.

After reviewing the next two lessons, there is an even bigger reason to stick with the original code. The status of the pressure plates is broadcast to the moving platform. Broadcasting that every tick, which would happen with my code would cause code in the transporter class to run every tick. That would more than negate any savings from my code change. The original code is better. Thanks for your responses.

1 Like

Privacy & Terms