What am I missing? I am just trying to call a function

Hi there,
This has been driving me nuts all day, I don’t seem to be able to make the simplest function call work. I am obviously missing something (probably something obvious).

I have stripped this down to the simplest example for clarity.

All I am trying to do is call a function contained in a different class, that I reference by a pointer.

SampleActor simply declares the variable ( I believe I am forward declaring with the class keyword but correct me if I’m wrong)

SomeRandomComponent is just a component that I am trying to call the DoStuff function in.

I have tried every variation of this I can find, what am I missing Forum Geniuses??

I have

  1. Forward Declared the var
  2. Included SomeRandomComponent.h in SampleActor
  3. Read every forum post I could find (most say add the .h)
  4. Created an entirely new project to ensure it is not something silly
  5. Created new test classes to isolate the issue.

I can’t call a function. Please help.

you need to make it a friend class of the class you want to call something.

Syntax :

class <class_name>    
{    
    friend  <return_type>  <function_name>(argument/s);  
};

if you want to have an example of casting way : Example

2 Likes

Thanks for the quick response, that looks promising!

I am still getting the same "use of undefined type " error though.

Did I miss something in the definition?

Hi @vagabond00 !

From what I can see, there are a few problems with what you are doing. With what you already have, there is no connection between SomeRandomComponent and SampleActor because there is no instance of it anywhere. To solve this, I’d recommend first instancing the component, to do this edit the header of SampleActor and change:

class SomeRandomComponent* MyVar = NULL;

for this

UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
class USomeRandomComponent* MyVar;

Then on the cpp of ASampleActor, edit the constructor (ASampleActor::ASampleActor()) to this:

ASampleActor::ASampleActor()
{
    MyVar = CreateDefaultSubobject<USomeRandomComponent>(TEXT("SomeRandomComponent"));	
// if USomeRandomComponent inherits from USceneComponent then add this too, to add it to the Actor tree
// MyVar->SetupAttachment(RootComponent)
}

Then you are already set, you can now call MyVar->DoStuff() from any place on SampleActor code

2 Likes

You can’t use SpawnActor<> on a component, only for actors.

Actors are all the entities that are on your game world, an actor can have many components, they can be inherited from USceneComponent or UActorComponent, the difference is that USceneComponent components are part of the actor, but have a representation on the world and have their own transform, meanwhile UActorComponents are part of the actor, but are not physically on the world.

A component can only exist as part of an Actor, and can’t exist on its own.

The method SpawnActor<>, spawns an actor on the location that you give it, check this for the properties you can configure here

1 Like

Hi Barracius, That makes a lot of sense and I think I am getting closer. It is still not working with the changes however.

Also, when creating the Subobject you are using USomeRandomComponent but the var is using SomeRandomComponent. When do I need to include the U and when do I not?

Also neither of them are working but the errors are different.


As for the UPROPERTY stuff, I just want to confirm I understand, basically, that UPROPERTY is going to create an instance of the object? I am used to other languages where I would expect that to fail at runtime (if the pointer is null) but it would normally let me compile without an issue (I plan to add the reference I need at runtime anyways, it should be pointing to an existing and possibly changing reference in the scene).

Thank you for the detailed explanation though, that really helps.

That was a typo of mine, always use the Unreal name (with the U for objects, A for actors, E for enums and F for math), the only case where I would say, don’t use unreal name is if you configure DefaultEngine.ini

No, what UPROPERTY does is that it signals that variable to not be garbage collected, additionally it also has specifiers, specifiers are a powerful thing that has too many uses (going to link some info about it here, it’s pretty interesting)

image

What the documentation says about UPROPERTY

EDIT: Editted my previous answers to avoid confusing you with the typo. The one on the TEXT field, is how is the component going to be called on the actor tree

2 Likes

Thank you so much for your help. I have been using UPROPERTY already quite a bit, I just didn’t think it was the issue here.

My final problem was actually that “friend” specifier for the function but when I removed that it all started working correctly.

Thanks again and I’ll post the working version for anyone else who comes across this in the future.


2 Likes