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"

API constructor for UStruct pointers?

EDIT: I have realised I am not actually deriving from UStruct thanks to iniside's answer as I am only using the USTRUCT() macro, which renders this question pointless as I am simply making a C++ struct (and not an API one).

I̶ ̶c̶a̶n̶'̶t̶ ̶s̶e̶e̶ ̶a̶n̶y̶w̶h̶e̶r̶e̶ ̶i̶n̶ ̶t̶h̶e̶ ̶A̶P̶I̶ ̶w̶h̶e̶r̶e̶ ̶I̶ ̶c̶a̶n̶ ̶c̶o̶n̶s̶t̶r̶u̶c̶t̶ ̶a̶ ̶U̶S̶t̶r̶u̶c̶t̶ ̶i̶n̶ ̶a̶ ̶s̶i̶m̶i̶l̶a̶r̶ ̶m̶a̶n̶n̶e̶r̶ ̶t̶o̶ ̶c̶r̶e̶a̶t̶i̶n̶g̶ ̶a̶ ̶U̶C̶l̶a̶s̶s̶,̶ ̶i̶s̶ ̶t̶h̶e̶r̶e̶ ̶a̶n̶y̶ ̶w̶a̶y̶ ̶t̶o̶ ̶d̶o̶ ̶t̶h̶i̶s̶ ̶v̶i̶a̶ ̶t̶h̶e̶ ̶A̶P̶I̶?̶

U̶S̶t̶r̶u̶c̶t̶ ̶i̶s̶ ̶d̶e̶r̶i̶v̶e̶d̶ ̶f̶r̶o̶m̶ ̶U̶O̶b̶j̶e̶c̶t̶ ̶s̶o̶ ̶I̶ ̶f̶e̶e̶l̶ ̶l̶i̶k̶e̶ ̶t̶h̶i̶s̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶a̶l̶i̶t̶y̶ ̶s̶h̶o̶u̶l̶d̶ ̶b̶e̶ ̶t̶h̶e̶r̶e̶ ̶a̶n̶d̶ ̶I̶ ̶a̶m̶ ̶m̶i̶s̶s̶i̶n̶g̶ ̶i̶t̶,̶ ̶e̶s̶p̶e̶c̶i̶a̶l̶l̶y̶ ̶s̶e̶e̶n̶ ̶a̶s̶ ̶t̶h̶e̶r̶e̶ ̶a̶r̶e̶ ̶t̶h̶e̶s̶e̶ ̶m̶e̶t̶h̶o̶d̶s̶ ̶o̶f̶ ̶c̶r̶e̶a̶t̶i̶n̶g̶ ̶U̶C̶l̶a̶s̶s̶'̶s̶ ̶-̶ ̶h̶t̶t̶p̶s̶:̶/̶/̶d̶o̶c̶s̶.̶u̶n̶r̶e̶a̶l̶e̶n̶g̶i̶n̶e̶.̶c̶o̶m̶/̶l̶a̶t̶e̶s̶t̶/̶I̶N̶T̶/̶P̶r̶o̶g̶r̶a̶m̶m̶i̶n̶g̶/̶U̶n̶r̶e̶a̶l̶A̶r̶c̶h̶i̶t̶e̶c̶t̶u̶r̶e̶/̶O̶b̶j̶e̶c̶t̶s̶/̶C̶r̶e̶a̶t̶i̶o̶n̶/̶i̶n̶d̶e̶x̶.̶h̶t̶m̶l̶.̶

W̶h̶y̶ ̶a̶r̶e̶ ̶a̶l̶l̶ ̶t̶h̶e̶ ̶U̶O̶b̶j̶e̶c̶t̶ ̶c̶o̶n̶s̶t̶r̶u̶c̶t̶o̶r̶s̶ ̶l̶o̶c̶k̶e̶d̶ ̶d̶o̶w̶n̶ ̶t̶o̶ ̶U̶C̶l̶a̶s̶s̶ ̶w̶h̶i̶l̶e̶ ̶h̶a̶v̶i̶n̶g̶ ̶n̶o̶t̶h̶i̶n̶g̶ ̶f̶o̶r̶ ̶U̶S̶t̶r̶u̶c̶t̶?̶ ̶ ̶I̶ ̶d̶o̶n̶'̶t̶ ̶w̶a̶n̶t̶ ̶t̶o̶ ̶b̶e̶ ̶s̶t̶o̶r̶i̶n̶g̶ ̶s̶t̶r̶u̶c̶t̶s̶ ̶t̶h̶a̶t̶ ̶a̶r̶e̶n̶'̶t̶ ̶p̶o̶i̶n̶t̶e̶r̶s̶ ̶a̶n̶d̶ ̶w̶o̶u̶l̶d̶ ̶p̶r̶e̶f̶e̶r̶ ̶a̶n̶ ̶A̶P̶I̶ ̶m̶e̶t̶h̶o̶d̶ ̶o̶f̶ ̶c̶o̶n̶s̶t̶r̶u̶c̶t̶i̶o̶n̶ ̶r̶a̶t̶h̶e̶r̶ ̶t̶h̶a̶n̶ ̶i̶n̶c̶o̶n̶s̶i̶s̶t̶e̶n̶c̶y̶ ̶i̶n̶ ̶m̶y̶ ̶g̶a̶r̶b̶a̶g̶e̶ ̶c̶o̶l̶l̶e̶c̶t̶i̶o̶n̶ ̶a̶s̶ ̶i̶t̶ ̶g̶e̶t̶s̶ ̶m̶e̶s̶s̶y̶ ̶w̶i̶t̶h̶ ̶b̶i̶g̶ ̶p̶r̶o̶j̶e̶c̶t̶s̶.̶

Product Version: Not Selected
Tags:
more ▼

asked Jul 24 '14 at 04:04 AM in C++ Programming

avatar image

Furinyx
214 7 11 23

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

2 answers: sort voted first

There is no constructor. You just use new with preferably TSharedPtr.

 TSharedPtr<FMyStruct> MyStruct = MakeShareable(new FMyStruct())

However I do not recommend doing it in UObjects. You might quickly run into Circual reference issue and cause memory leak, which is might be hard or easy to track. Depends, when in it popup.

USTRUCT() is not derived from UObject. USTRUCT() is just macro, which create meta data for code reflection used by unreal. In general USTRUCT() in UE4 is used as http://en.wikipedia.org/wiki/Plain_Old_CLR_Object

Besides there is really no added benefit of storing pointer to USTRUCT() over just storing value. If you doing operations where you need raw C++ code and pointer, you should just use raw C++, without unreal reflection.

more ▼

answered Jul 25 '14 at 05:17 AM

avatar image

iniside
1.6k 104 97 181

avatar image Furinyx Jul 25 '14 at 05:57 AM

Thanks for your answer, I will use raw pointers for what I am wanting then.

I have already come across circular reference issues from trying that with UObjects so definitely a good non-recommendation.

I was referring to UStruct being derived from UObject, not the USTRUCT() macro - https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UObject/UStruct/index.html. Although I have realised I was not deriving from UStruct and was simply using the macro, ultimately rendering my question pointless :S. Thanks for pointing that out as I was not thinking properly.

For storing structs as pointers, see my response to Shadowriver.

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

Thru classes are similar to struct they are treated diffrently by UE4, they are treated like data type like which you use inside classes and its ok to store them inside there as long as you use USTRUCT() before struct declarations and UPROPERTY () before struct varbale, GC will handle the rest. Besides if you look on engine source you can find structs being used direcly without pointers and technicly they are not much diffrent in memory storange then ints or floats which are also in us, same goes with classes actully.

more ▼

answered Jul 25 '14 at 04:59 AM

avatar image

Shadowriver
35.7k 926 168 1104

avatar image Furinyx Jul 25 '14 at 05:40 AM

First part of your answer: The reason I am using a struct in this particular case is because I want data that is related to multiple classes, otherwise I would just have that data as part of a class so creating the struct inside each class is of no help and I already know how to do this.

The second part of your answer: Just because there are uses for non-pointers does not render pointers pointless. Storing a struct or class with a lot of data is bad programming and can be a huge difference in memory storage compared to an int or float. For example - one of my structs, which has over 20 properties, would make each reference to an instance of the struct over 80 bytes larger than using a pointer, if I was to create 10,000 instances with that struct, the memory footprint starts making a substantial difference. Now do that with every struct or class I use in my games and you have a massive problem.

Thanks for trying to offer alternative solutions, though so far it seems I will have to stick with using raw pointers for structs (if I need them to be pointers) and cleaning them up myself.

avatar image Shadowriver Jul 25 '14 at 10:43 AM

You dont need to clean them if you use UPROPERTY (), GC will handle that and i didnt said you that you should not use poiters, use storing when you need to hold data in object, use pointers for refrence to it

(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