Page 1 of 1

var bool Initialized

Posted: Sat Dec 16, 2017 4:00 pm
by Gadavre
I noticed that this variable is often used in many old mutators and mods. It is necessary for modern mutators? What problems can appear without this variable?
For example:

Code: Select all

class Vampire expands Mutator;

var bool Initialized;

function PostBeginPlay()
{
if (Initialized)
return;
Initialized = True;
}

Re: var bool Initialized

Posted: Sat Dec 16, 2017 4:07 pm
by JackGriffin
It just prevents multiple copies of your mod from loading. Preservation of the mutator/actor/checkreplacement/whatever chain is very often overlooked in coding help and this helps prevent it from causing problems. It's a good idea to work in such a way that you conflict as little as possible with other potential mods but not everyone feels that way and so you have to do your stuff to compensate for that. This little bit of code helps to stop potential catastrophic problems from developing.

Re: var bool Initialized

Posted: Sat Dec 16, 2017 4:41 pm
by Barbie
This is to prevent executing code multiple times.
But the above given example is completely useless (unless Initialized is accessed anywhere else), because no additional code is executed there.

Re: var bool Initialized

Posted: Sat Dec 16, 2017 5:40 pm
by nogardilaref
This is also what is generally called a "code smell".
If you need to use this sort of thing, there's a bigger problem somewhere else, or even in this very class.

Re: var bool Initialized

Posted: Sat Dec 16, 2017 6:32 pm
by Gadavre
nogardilaref wrote:This is also what is generally called a "code smell".
If you need to use this sort of thing, there's a bigger problem somewhere else, or even in this very class.
What do you mean? I'm not a professional tester and coder to find all bugs in my Mod. It may have bugs, which is unknown to me. I would like "to insure" themselves and use the variable var bool Initialized against unknown bugs. It's a wise decision, isn't it?
Barbie wrote:This is to prevent executing code multiple times.
What do you mean? This will help in all functions of the Мod or only in function PostBeginPlay() ?

-Edit by papercoffee-

Re: var bool Initialized

Posted: Sat Dec 16, 2017 6:58 pm
by Higor
Ppl using that because they needed it somehow managed to screw up the event chain or simply made the mod for v400.
Ppl using that because they saw it somewhere else need to learn a bit more about the engine.

The engine already does that in the 'BeginPlay' events, the variable that controls that is bScriptInitialized.

Re: var bool Initialized

Posted: Sat Dec 16, 2017 7:09 pm
by Gadavre
Higor wrote: The engine already does that in the 'BeginPlay' events, the variable that controls that is bScriptInitialized.
If I understand you correctly, I do not need to use this variable in the mod?

Re: var bool Initialized

Posted: Sat Dec 16, 2017 8:04 pm
by nogardilaref
Gadavre wrote:
nogardilaref wrote:This is also what is generally called a "code smell".
If you need to use this sort of thing, there's a bigger problem somewhere else, or even in this very class.
What do you mean? I'm not a professional tester and coder to find all bugs in my Mod. It may have bugs, which is unknown to me. I would like "to insure" themselves and use the variable var bool Initialized against unknown bugs. It's a wise decision, isn't it?
I didn't mean the problem to be necessarily yours or your code, I meant mostly that the engine has some of these which are indicative of a bigger problem of how the game itself (and engine) is programmed.

Having that said, any variable that you declare in your own class, doesn't have any magic power by itself.
Some vars which are inherited from Object and Actor and others may have some collateral effect on the code when changed or may be changed by the engine itself (like bScriptInitialized as Higor mentioned), but anything you declare yourself in your own class has no implicit magic going on.
In other words, something like that by itself doesn't protect the code from anything at all, unless something is done with it in the actual code.

If you see things like this in other mods, check the rest of the code first and see where it is used and what parts of the code it is enabling or disabling, and that should give you a strong clue what kind of "problem" each one of them are addressing by doing something like that, and if you have doubts, post the respective code here and we can help you figuring out those reasons, since it will vary from mod to mod. :)