Download

...how do I include a header file? (InstancedStaticMesh.h)

I just made a c++ class which extends UInstancedStaticMeshComponent. I need to add an include for “InstancedStaticMesh.h”. When I do this, the build fails with “No such file or directory”. The include is the only change I’ve made to my .h at all:


// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/InstancedStaticMeshComponent.h"
#include "Engine/InstancedStaticMesh.h" // also fails with just "InstancedStaticMesh.h"
#include "ParametricInstancedSMComponent.generated.h"

/**
 *
 */
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class RPG_API UParametricInstancedSMComponent : public UInstancedStaticMeshComponent
{
    GENERATED_BODY()




};

I’ve read that I need to regenerate project files on the .uproject, which makes no sense and didn’t work. I’ve also read that my includes all need to come before the .generated.h include, which I’ve done. I’ve read that I may need to add a “module” dependency to build.cs, but I have no idea which module would have InstancedStaticMesh.h or how to find out. What exactly do I need to do here?

I come from an environment (Eclipse/Java) where this is a complete non-issue. If I so much as try to use a class I haven’t imported, my IDE would suggest possible ones I might mean, and let me add the exact import statement I need in one click. To have an IDE be this needlessly obstructionist is not acceptable.

[EDIT] Including the full path of the file starting from /source/ fixed the issue:


#include "Runtime/Engine/Private/InstancedStaticMesh.h"/

Is this the proper way to include engine .h files? (I read somewhere that using the full path was a bad practice).

You don’t need to regenerate project files everytime you include a header. If that was the case nobody would ever get anything done!

For some engine-level includes you do often have to include some additional pathing to the file. There are ways around it but I can’t remember them right now… IIRC it involves adding engine paths to the build.cs file but it’s probably more trouble that it’s worth. In this case, try the following (I’m using 4.19)



Engine\Private\InstancedStaticMesh.h
Runtime\Engine\Private\InstancedStaticMesh.h


The first one should probably work.

Visual Studio won’t tell you what you should include and to my knowledge it never has. UE4’s source code is far too vast for Intellisense to handle properly. I highly recommend getting Visual Assist X, pretty much anybody doing serious programming in UE4 has it and it’ll help you find files you need faster, not to mention it’s leaps and bounds faster than anything in VS.

To determine what API / Module a class is a part of - you can check it’s declaration. For example, InstancedStaticMeshComponent is decared like this:



class ENGINE_API UInstancedStaticMeshComponent : public UStaticMeshComponent


Which means it’s part of the ‘Engine’ module, the same way your class is part of your ‘RPG’ module. Engine Module is usually included in game modules by default.