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"

error C2227: left of '->SpawnActor' must point to class/struct/union/generic type UE4

I created maze generation lib, but when I try to compile, it returns error:

 CompilerResultsLog: Error: D:\Home\Volodya\Projects\Games\UE4\TestingSomething\Something\Source\Something\Private\MazeLib.cpp(17) : error C2352: 'UObject::GetWorld': illegal call of non-static member function
 CompilerResultsLog: Error: C:\Program Files\Epic Games\UE_4.18\Engine\Source\Runtime\CoreUObject\Public\UObject/Object.h(473) : note: see declaration of 'UObject::GetWorld'
 CompilerResultsLog: Error: D:\Home\Volodya\Projects\Games\UE4\TestingSomething\Something\Source\Something\Private\MazeLib.cpp(17) : error C2227: left of '->SpawnActor' must point to class/struct/union/generic type
 CompilerResultsLog: Error: D:\Home\Volodya\Projects\Games\UE4\TestingSomething\Something\Source\Something\Private\MazeLib.cpp(17) : error C2227: left of '->GetAuthoritativeClass' must point to class/struct/union/generic type
 CompilerResultsLog: ERROR: UBT ERROR: Failed to produce item: D:\Home\Volodya\Projects\Games\UE4\TestingSomething\Something\Binaries\Win64\UE4Editor-Something-5058.dll

My spawnMaze function in cpp file:

 void UMazeLib::spawnMaze(int size) {
     bool** maze = generateMaze(size);
     for (int h = 0; h < size; h++)
     {
         maze[h] = new bool[size];
         for (int w = 0; w < size; w++)
         {
             const FVector* position = new FVector(10 * h, 10 * w, 0);
             GetWorld()->SpawnActor(wallClass->GetAuthoritativeClass(), position);
         }
     }
 }
 

and my .h file:

 #pragma once
 
 #include "CoreMinimal.h"
 #include "Kismet/BlueprintFunctionLibrary.h"
 #include "Wall.h"
 #include "MazeLib.generated.h"
 
 /**
  * 
  */
 UCLASS()
 class SOMETHING_API UMazeLib : public UBlueprintFunctionLibrary
 {
     GENERATED_BODY()
     
     public:    
         UFUNCTION(BlueprintCallable, Category = "Something")
         static void spawnWall(FTransform str);
 
         UFUNCTION(BlueprintCallable, Category = "Maze")
         static void spawnMaze(int size);
 
         UPROPERTY(BlueprintReadWrite, EditAnywhere)
         TSubclassOf<class AWall> wallClass;
 
         UPROPERTY(BlueprintReadWrite, EditAnywhere)
         UWorld* world = GetWorld();
 
     private:
         static    bool** generateMaze(int size);
 };
Product Version: UE 4.18
Tags:
more ▼

asked Apr 25 '18 at 01:33 PM in C++ Programming

avatar image

Poseydon42
10 3 4 10

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

A static function has no "this" object so you can't call GetWorld(). You either need to make it non-static or pass in the actor pointer as a parameter.

more ▼

answered Apr 25 '18 at 01:48 PM

avatar image

Jin_VE
4.3k 31 77 40

avatar image Poseydon42 Apr 25 '18 at 03:05 PM

Yeah, but when I make function spawnMaze() non-static, it's hiding in level Blueprint.

avatar image Jin_VE Apr 25 '18 at 03:25 PM

That should be possible. What does your code look like? You just removed the static keyword from the function declaration?

avatar image Poseydon42 Apr 25 '18 at 03:40 PM

Yes, I only remove 'static' from declaration.

avatar image Jin_VE Apr 25 '18 at 03:51 PM

Oh it's a function library. For some reason I thought it was a level BP. I don't have much experience with function libraries, but if it has to be static then just pass the world as a function parameter instead of calling GetWorld() inside the function. When you call the function, you can pass in GetWorld().

avatar image Poseydon42 Apr 25 '18 at 03:58 PM

So, I have to add world object as function parameter. But why I can't use non-static methods threre. So my function must be like this: static void spawnMaze(int size, UWorld* world)?

avatar image Jin_VE Apr 25 '18 at 04:11 PM

You can't use non-static members from a static function. You need an object to work with. There's no way I can explain it better than the references available online. I found this one, for example

And yes, that function should work. It looks like you have to use static functions so passing in parameters is the only way to go.

avatar image Jin_VE Apr 26 '18 at 12:32 AM

Did it work out for you? If not, let me know. If so, please close the question (check-mark under the up/down-vote widget). By the way, if at any time one of your parameters is an AActor then you can call GetWorld() from that (and save an extra parameter).

(comments are locked)
10|2000 characters needed characters left
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