How to make UObject members in UClass? (GameInstance)

My GameInstance is growing. So I thought it would be a good idea to separate some parts into different classes to have a cleaner code. However I am having some trouble doing this.

For example, I have made a new class so that it handles only the matter of friends.

This was on GameInstance:

This is my new class:

I chose the UObject type because delegates require this type to work.

So I declared a new member in GameIntance:
4

And instantiate it in the overridable function Init()
6

So I tried the code and the program crashed.
The commented code works perfectly. However, the function call of the friends member causes a crash. (both execute exactly the same code).

I know it must be some pointer problem.

I would even go so far as to say that the problem is related to this.

8

So I think I’m not following the correct steps to create a UObject member inside a UClass.

Maybe it is not possible?

So please forgive my ignorance because this is the first time I’ve tried to do this in Unreal C++.

Can someone help me a bit with this? What are the correct steps?

Thank you so much!!

I found the correct way to create a UObject member in a UClass.

And I also found the pointer that was causing the problem.

it was “OnlineSubsystem”. For some reason the instantiation in the constructor didn’t work.

2

I also tried assigning it from Game Instance but this didn’t work either.

Also checking if the pointer was NULL inside the function did not work either.
if (OnlineSubsystem == NULL)return;

It was all very confusing.

The only way to make it work was this:

I also deleted all the binaries and built everything from scratch.

Now everything works!!

The problem is that I’m not sure what the real problem was.

1-Incorrect instantiation of the friends object?
2-The constructor was not executed?
3-Can’t pass pointers to class member objects?
4-The binaries were corrupted because maybe it builds with patches?

If anyone can answer these questions I would really appreciate it.

They changed the behavior of the constructors… All variables take zero value even if you give them a non-zero value…

I also think that the compilation is done in pieces… And sometimes a piece is not updated. And unwanted things happen. So I think it’s a good idea to delete the binaries from time to time.

I don’t see anything that would protect the instance of UFriendsObject from being GC’d.

2 Likes

You’re right.
The garbage collector is driving me crazy.
UPROPERTY(), right?
Thank you so much!!

Yes, UPROPERTY will be the simplest solution

1 Like

This is wonderful. Now I can program like I normally do.
Thank you so much for your help!! :heart:

1 Like