global variable - to be used by modified source code header - error

Hello guys,

I want to make a global variable available for the method FPhysSubstepTask::UpdateTime located in ** PhysSubstepTasks.cpp**.
This global variable should switch the behaviour of the method form original to “modified”.

So I have created these two files (with the simple content):

GlobalVariable.h
#pragma once
extern int32 Alex;

GlobalVariable.cpp
#pragma once
int32 Alex = 3000;

I can #include GlobalVariable.h in PhysSubstepTask.h
That compiles fine, no problem

But as soon as I use the variable in PhysSubstepTask.cpp
(for example Alex=5)

I get the the following error when I try to compile:

2>C:\Program Files\Unreal Engine Controlled\Engine\Binaries\Win64\UE4Editor-Engine.dll : fatal error LNK1120: 1 unresolved externals
2>UnrealBuildTool : error : UBT ERROR: Failed to produce item: C:\Program Files\Unreal Engine Controlled\Engine\Binaries\Win64\UE4Editor-Engine.dll
2> (see …/Programs/UnrealBuildTool/Log.txt for full exception trace)

The Solution Configuration is set to “Development Editor”

What am I doing wrong here? Any help is most appreciated!

Thanks in advance :slight_smile:

Wrap your statics into an exported UObject class.
In Header you declare it, in cpp you init default values.

.H:



UCLASS()
class MYMODULE_API UMyStatics : public UObject {
    GENERATED_BODY()
public:

    static int32 Alex;

};


.CPP:



int32  UMyStatics::Alex = 0;


Also instead of use static you can use Config classes, and then retrieve your value with GetDefault<UMyConfig>(); or GetMutableDefault

.H



UCLASS(Config="MyConfig")// this will generate a new config file with this name in the saved/Config/ folder
class MYMODULE_API UMyConfig: public UObject
{    
      GENERATED_BODY()
public:      
      UPROPERTY(Config,BlueprintReadOnly, Category = "MyCategory")          
      int32 Alex;
}; 

Hi guys!

thank you for the quick answers, big thank you!
Here is what I did and please correct a mistake:

in Visual Studio:

  • added an empty GlobVar.h an empty GlobVar.cpp file (new->item) and moved both into the source folder
  • rightclicked the unreal project file and did “Generate Visual Studio Files”

My project name is “FirstTryout”

  • started Visual Studio
  • I tried out BrUnO XaVIeR s version:

MyStatics.h
#pragma once
#include “MyStatics.generated.h”

UCLASS()
class FIRSTTRYOUT_API UMyStatics : public UObject {
GENERATED_BODY()
public:
static int32 Alex;
};

MyStatics.cpp
#include “MyStatics.h”
int32 UMyStatics::Alex = 0;

It works fine if I access MyStatics from my project.

If I try to now include it into the PhysSubstepTask.h, I get the error:
*Cannot open include file: ‘MyStatics.h’: No such file or directory *

If I include the MyStatics.h with its complete path into PhysSubstepTask.h, then MyStatics.h it is found but as it contains #include “MyStatics.generated.h” I get the error
cannot open include file: ‘MyStatics.generated.h’: No such file or directory :frowning:

I have added the path of the sourcefiles of my project to the UE4 VC++Directories:

But PhysSubstepTask.h doesn’t see it.

Sorry to bother again, maybe one of you has a tip. :rolleyes:

Hi you have to include your module inside the Engine.Build.cs rules, I’m assuming you are using a custom version, don’t you? otherwise you cannot modify engine stuff that easy

Yes ZkarmaKun, I use the source code Version from github. I can modify the souce code file, that works fine. I just don’t know how to make UE4 check also the directory of my project to find the include files.
My project is on a different hard drive.

You can’t have a foundation dependent on the roof, a modular ecosystem doesn’t work like .NET namespaces.

… well all I want to do really is

**to modify a source code method and make it use a variable of my project. **

Or the other way:

**generate a variable in the souce code method and access a pointer to it. **

So how can I access a variable (in this case a pointer to a float) in the source code method and use it in my project?

Maybe you can give me a hint.

ahhh I have got a good hint and solved the problem:

https://forums.unrealengine.com/development-discussion/engine-source-github/1466115-access-variable-in-ue4-source-code-by-method-of-my-project-need-help

Thank you!!

Is it possible to do this:

But with the UWorld* ?
Im trying to get the value passed by this function that is called by a Javascript function to my gamemode:


extern "C" void EMSCRIPTEN_KEEPALIVE receiveString(char *str)
{
FString returnedDataString(str);

GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, returnedDataString);


}

PS Solved:
The solution was to use a static variable, bnut i didn’t know that you shoud initialize it before any code, like this: