x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Can you access an ustruct from another class

So I am trying to make an struct that I want to use in different classes, but I don't want to have the decleration of the struct in every class. How can I achieve an ustruct that is accessable from multiple classes and possibly even blueprints? This is what I tried in an empty class in which I removed everything except for the headers and #pragma once:

 /**
  * HELLSKRIEGCPP_API 
  BlueprintType
  */
 USTRUCT()
 struct FCoverStruct
 {
     GENERATED_USTRUCT_BODY()
 
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         bool InUse;
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         FVector Location;
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         FRotator Rotation;
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         AActor* OwningActor;
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         bool InLowCover;
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         bool IsNextToEdge;
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
 };

I have tried the blueprintype, GENERATED_USTRUCT_BODY(), GENERATED_BODY(), the GAME_API, the struct name with and without F. But whatever I try I can't access it from another header file to declare a variable.

Any help would be greatly apreciated.

-Erik

Product Version: UE 4.13
Tags:
more ▼

asked Nov 02 '16 at 02:30 PM in C++ Programming

avatar image

erik321123
413 17 15 31

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

All you have to do is include the header that holds the struct definition in whatever source you want to have access to the struct; just like if you want a class to have access to an external class you have to include the header file defining the external class.

You can create a header file that only holds your struct (or multiple structs, and/or enums) if you don't want to include the class as well, and then you don't need to create a .cpp file associated with it.

If you want it accessible in blueprints you need to use the BlueprintType specifier in the USTRUCT macro:

 #pragma once
 
 #include "MyStruct.generated.h"
 
 USTRUCT(BlueprintType)
 struct FMyCustomStruct
  {
      GENERATED_USTRUCT_BODY()
  
      //...
      //define the struct
      //...
 
 };

With the above struct, in whatever class you want to have access to it include:

 #include "MyStruct.h"

If you use it in the header (like if you define a class variable using the struct, or have a function that returns your struct or accepts the struct as an argument), include it in the header. If you just use the struct within a method but it's never exposed outside, you can just place it in the source file.

Same with enums, or any other datatype you may define.

more ▼

answered Nov 02 '16 at 02:48 PM

avatar image

GigasightMedia
1.7k 43 12 61

avatar image erik321123 Nov 02 '16 at 04:24 PM

It now is complaining about that there is no semi colon before the first variable. I don't see what I am doing wrong.

 USTRUCT(BlueprintType)
 struct FCoverStruct
 {
     GENERATED_USTRUCT_BODY()
 
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         bool InUse;
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         FVector Location;
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         FRotator Rotation;
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         AActor* OwningActor;
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         bool IsLowCover;
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         bool IsNextToEdge;
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
         float Distance;
 };
avatar image GigasightMedia Nov 02 '16 at 04:42 PM

What is complaining?

Bear in mind that a lot of the default warnings and errors that come from VS do not apply. If you have a successful compile, you are good.

Intellisense is particularly bad with FStructs; the first property in an FStruct often doesn't appear in intellisense at all. If you're just getting the squiggly lines and intellisense warnings, you can ignore them. The only thing that matters is if it compiles.

Use the output tab, do not use the Error tab in VS to evaluate compile results. UE4 uses so many macros VS often gives misleading error messages.

avatar image erik321123 Nov 02 '16 at 04:43 PM

found the last issue, I forgot to include the #include "CoverStruct.generated.h" in the CoverStruct.h

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question