var bool Initialized

Discussions about Coding and Scripting
User avatar
Gadavre
Experienced
Posts: 140
Joined: Sun Jan 18, 2015 7:55 am

var bool Initialized

Post by Gadavre » Sat Dec 16, 2017 4:00 pm

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;
}

JackGriffin
Godlike
Posts: 3765
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: -Retired-

Re: var bool Initialized

Post by JackGriffin » Sat Dec 16, 2017 4:07 pm

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.
So long, and thanks for all the fish

User avatar
Barbie
Godlike
Posts: 2107
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: var bool Initialized

Post by Barbie » Sat Dec 16, 2017 4:41 pm

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.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett

nogardilaref
Masterful
Posts: 577
Joined: Tue Jun 20, 2017 1:00 pm
Personal rank: ⚋⚊⚌☰⚞⌖⚟☰⚌⚊⚋

Re: var bool Initialized

Post by nogardilaref » Sat Dec 16, 2017 5:40 pm

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.

User avatar
Gadavre
Experienced
Posts: 140
Joined: Sun Jan 18, 2015 7:55 am

Re: var bool Initialized

Post by Gadavre » Sat Dec 16, 2017 6:32 pm

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-
Last edited by papercoffee on Sat Dec 16, 2017 9:06 pm, edited 1 time in total.
Reason: double post

Higor
Godlike
Posts: 1832
Joined: Sun Mar 04, 2012 6:47 pm

Re: var bool Initialized

Post by Higor » Sat Dec 16, 2017 6:58 pm

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.
ImageImage
Image unreal://23.111.157.138:7777
Image unreal://46.228.199.205:7788

User avatar
Gadavre
Experienced
Posts: 140
Joined: Sun Jan 18, 2015 7:55 am

Re: var bool Initialized

Post by Gadavre » Sat Dec 16, 2017 7:09 pm

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?

nogardilaref
Masterful
Posts: 577
Joined: Tue Jun 20, 2017 1:00 pm
Personal rank: ⚋⚊⚌☰⚞⌖⚟☰⚌⚊⚋

Re: var bool Initialized

Post by nogardilaref » Sat Dec 16, 2017 8:04 pm

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. :)