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"

Moving a TArray of pointers from BeginPlay() to outside of the function (global) crashes the engine?

A snippet of the code in question is below. The vector, devices, is what is giving me grief. I need it to be outside of BeginPlay() because I need to use the pointers in the vector when Update() is called, but when I move it out of BeginPlay(), the engine crashes when I hit play :/ I am using a third party library called librealsense.

 TArray<rs::device *> devices; // this is the line moved from BeginPlay() that crashes the engine.
 
 ARealsenseData::ARealsenseData()
 {
     PrimaryActorTick.bCanEverTick = true;
 }
 
 void ARealsenseData::BeginPlay()
 {
     Super::BeginPlay();

     rs::context ctx;
     if (ctx.get_device_count() == 0) print("No device detected. Is it plugged in?");
 
     // Enumerate all devices
     //TArray<rs::device *> devices;
 
     for (int i = 0; i<ctx.get_device_count(); ++i)
     {
         devices.Add(ctx.get_device(i));
     }
 
     // Configure and start our devices
     for (auto dev : devices)
     {
         dev->enable_stream(rs::stream::depth, rs::preset::best_quality);
         dev->enable_stream(rs::stream::color, rs::preset::best_quality);
         dev->start();
     }    
 }
 
 void ARealsenseData::Tick( float DeltaTime )
 {
     Super::Tick( DeltaTime );
 
 /*    for (auto dev : devices)
     {
         dev->poll_for_frames();
     } */
 }






Product Version: UE 4.14
Tags:
more ▼

asked Jan 24 '17 at 10:36 PM in C++ Programming

avatar image

glang
8 1 2 4

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

1 answer: sort voted first

If it crashes on that declertion in global that means there problem during initiation of std::vector for some reason. Did you try moving it to class deceleration so it will be part of actor class? As long as you won't use UPROPERTY() on it UE4 should not mind it, UObject classes are still normal C++ classes.

Generly if you making some library support for UE4 best practice is to wrap it's API in to UE4 APIs and then use it elsewhere, instead of direly communicating with it

Also do you really need to use std::vector? By look of it you could use TArray too, again without UPROPERTY() this should not be a issue, TArray will simply store pointers

more ▼

answered Jan 24 '17 at 10:45 PM

avatar image

Shadowriver
34.5k 917 164 1094

avatar image glang Jan 24 '17 at 10:51 PM

Are you suggesting I add std::vector devices to the Actor base class? I have not tried that. Will give it a shot! Thanks.

avatar image Shadowriver Jan 24 '17 at 10:53 PM

i updated comment with some other possible solution ;] I will also convert it to anwser i guess

avatar image glang Jan 24 '17 at 10:52 PM

I have actually tried swapping the vector with Tarray and got the same crash when I moved the Tarray outside of beginplay() :/

avatar image Shadowriver Jan 24 '17 at 10:55 PM

Ahh ok, try placing in class

avatar image glang Jan 24 '17 at 11:00 PM

I'll give it a try later tonight and let you know how it goes. thanks.

avatar image Shadowriver Jan 24 '17 at 11:40 PM

Ah i also find other issue, you should not use exceptions in UE4 ( see "throw") they can mess up things as UE4 dont expect them being used. Either just make some behavioir in case of error or use check(bool condition here); which will stop the engine if condition is false, failed condition will be printed in log. But i think check() is little too radical in this case, you should inform user that be informed that device is not connected.

avatar image glang Jan 25 '17 at 12:52 AM

Thanks for the advice! I'll change that.

avatar image glang Jan 25 '17 at 05:40 AM

I placed the devices TArray into my class's .h file and that fixed the crashing! Thanks!

(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