How to communicate and get access to other scripts in C++?

I started in Unreal not long ago, and in c++ even less, so i realy have problems when i trying communicate and get a reference to other classes. In blueprint its easy, because i have access to everything. I can use the “Get Actor Of Class” node and access anything, and its easier in general.

So, how i get access to another class, and is there a way to get access to a Blueprint in my c++ file?

Like here. I have two classes, MainCharacter and BaseItem. I want to check if the item is a child of BaseItem, but i need to have a reference to BaseItem to check it. I just put the path and it worked, but is there a way to get this reference without using absolutes paths?

void AMainCharacter::PickUpItem(const AActor* Item)
	if (UKismetMathLibrary::ClassIsChildOf(Item->GetClass(),TSubclassOf<ABaseItem>()))
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Black, "Pegou");

Simpler way to write it and with null check. :+1:

if (Item && Item->IsA(ABaseItem::StaticClass()))
	// do something
1 Like

Thanks, i changed some things in the code, but this way is really simpler. But i need to make the code know what is a BaseItem. I just added an include

#include "TestesC/Items/BaseItem.h"

And it works, but is there another way, that if i change the file location it will not broke my code?

You are correct about the explicit offset of the .h file and moving it breaks your code.

What you can do is … put following in your "Project.Build.cs"file. This code walks through all your project code directories and adds them to your include paths.

Add this C# function to the “Project : ModuleRules” class.

    * IncludeSubDirectoriesRecursive() 
    * - automatically adds all code folders to "PublicIncludePaths"
    * - so users will not have to explicitly offset all .h includes
	private void IncludeSubDirectoriesRecursive(string baseDirectory, string DirectoryPathToSearch)
        string[] allDirectories = Directory.GetDirectories(DirectoryPathToSearch);
        foreach (string nextDirectory in allDirectories)
            string relativePath = Path.GetRelativePath(baseDirectory, nextDirectory);
            Console.WriteLine("Adding include path: " + relativePath);
            IncludeSubDirectoriesRecursive(baseDirectory, nextDirectory);

Then in the same file in the “ProjectName : base” function add the following:

        // include all code directories in this project
        // so users do not have to explicitly offset all include files
        string baseCodeDirectory = Path.Combine(ModuleDirectory, "..\\");
		Console.WriteLine("Including all directories for " + ModuleDirectory.ToString());
		IncludeSubDirectoriesRecursive(baseCodeDirectory, baseCodeDirectory);

If it works correctly, in VisualStudio output you will see "“Adding include path: foldeName” for every code folder in your project. Now you can just Include “BaseItem.h” no matter where it is in your project code. No explicit offset needed.

1 Like

Thank you very much. It helps a lot. I still have some issues when trying to get things from blueprints, but it solves most of my problems. A last question, this can make the game heavy to run on any way or change nothing in the optimization?

No. This video covers blueprints vs. C++. Short answer, there’s pretty much no case where C++ isn’t faster than blueprints.

1 Like