Announcement

Collapse
No announcement yet.

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

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    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


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

    .H:
    Code:
    UCLASS()
    class MYMODULE_API UMyStatics : public UObject {
        GENERATED_BODY()
    public:
    
        static int32 Alex;
    
    };
    .CPP:
    Code:
    int32  UMyStatics::Alex = 0;
    | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

    Comment


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

      .H
      Code:
      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;
      };

      Comment


        #4
        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



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

        Click image for larger version

Name:	Unbenannt.PNG
Views:	1
Size:	20.7 KB
ID:	1465491


        But PhysSubstepTask.h doesn't see it.


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

        Comment


          #5
          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

          Comment


            #6
            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.

            Comment


              #7
              You can't have a foundation dependent on the roof, a modular ecosystem doesn't work like .NET namespaces.
              | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

              Comment


                #8
                ... 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.




                Comment


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

                  https://forums.unrealengine.com/deve...ject-need-help

                  Thank you!!

                  Comment


                    #10
                    Is it possible to do this:

                    Originally posted by BrUnO XaVIeR View Post
                    Wrap your statics into an exported UObject class.
                    In Header you declare it, in cpp you init default values.

                    .H:
                    Code:
                    UCLASS()
                    class MYMODULE_API UMyStatics : public UObject {
                    GENERATED_BODY()
                    public:
                    
                    static int32 Alex;
                    
                    };
                    .CPP:
                    Code:
                    int32 UMyStatics::Alex = 0;

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

                    Code:
                    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:

                    Click image for larger version

Name:	UE-4.png
Views:	1
Size:	51.6 KB
ID:	1584741
                    Last edited by Invius; 02-14-2019, 12:28 PM.

                    Comment

                    Working...
                    X