Download

C++ Variables not setting / giving rubbish.

Hi all,

I am tearing my hair our here as this really doesn’t make any sense to me. I have a Class Called AChunk which is inherited from AActor. This AChunk Class will use another of my Class’s called UUBlock Which inherits from UObject.
it used to be just a CPP Class but didn’t work so I’ve had it inherit UObject and that didn’t sort the issue either. The Issue that I am talking of is a method in UUBlock which I call from AChunk is not storing local / class variables.

If I step though my code the ‘t’ varible is reported as undeclared, and the results of basic maths is always 0… The function is show here :-



void UUBlock::Blockdata(AChunk* chunk, INT32 X, INT32 Y, INT32 Z, TArray<FMeshDataTriangle>& Triangles)
{

	int32 t = X;

	this->BlockCenter.X = (float)((float)X*chunk->BlockSize) + chunk->BlockSizeHalf;
	this->BlockCenter.Y = (float)((float)Y*chunk->BlockSize) + chunk->BlockSizeHalf;
	this->BlockCenter.X = (float)((float)Z*chunk->BlockSize) + chunk->BlockSizeHalf;

......
}


I have included the CPP’s & Header files for the class which isn’t delivering the goods. And the class which calls it.

I really am at my wits end and don’t understand why C++ isn’t doing as I am telling it. And I hope it’s just me been a noob with UE4.

Big Thanks to anyone one whom points me in the right direction.


UUBlock *blocks[CHUNKSIZE][CHUNKSIZE][CHUNKSIZE];

This code here is your problem. You’ve created an array of pointers. Doing that does not create any objects, it’s literally just an array of pointers, and until you do something more, they just point to random spots in memory. You have to actually create objects and set the pointers to point at those created objects. The problem with that is, if you create the objects manually, they’re either going to be leaked memory and not go away when you’re done with them, or they’re going get garbage collected immediately, depending on how you try and do it.

The Unreal Way™ of creating a multi-dimensional arrays is to put a [FONT=Courier New]TArray in a struct and then create a [FONT=Courier New]TArray of the struct.

I am confused why cannot I use a “normal” C++ Multi-Dimensional array ?

I am aware that this:-



UUBlock *blocks[CHUNKSIZE][CHUNKSIZE][CHUNKSIZE];


just gives me an array of pointers which is what I want as I will be instantiating these later with code like this:-



this->blocks[2][2][2] = NewObject<UUBlock>();


I tried to do this with the more traditional C++ ‘new’ keyword but unreal complains during compiling and informs you to use NewObject.

I am also very aware that this means that I am 100% responsible for dealing with the memory and cleaning up after myself.

[FONT=Courier New]NewObject<> creates objects that the engine is aware of, which is why you had to change to subclassing UObject. Engine objects are placed under garbage collection. Because you have no references to the object that the garbage collector is aware of (e.g. from using the [FONT=Courier New]UPROPERTY() macro), it thinks you’re done with them as soon as your function ends and the objects gets garbage collected shortly thereafter.

It almost helps to think of C++ in Unreal as a distinct dialect and not try to force it to work the way you might be accustomed to from traditional C++ programming.

As for why you can’t use good old c++ “new”, well… that’s above my paygrade, so to speak. You’d need somebody from Epic to answer that question, though my guess is that they want the engine to know about all memory usage so it can intelligently optimize resources.

Note: You can take control of the object’s using this



YourObjectInstance->SetFlags(RF_RootSet);


which will prevent garbage collection and make you responsible for deallocating it. I’m not saying it’s the right way, but it should prevent them from getting garbage collected.

While what Jeff says about garbage collection is very true, from what you say it sounds like there may be another issue. Are you sure it’s not just a case of the debugger reporting incorrect values? If you’re using the Development build configuration, optimisations are enabled which means the debugger isn’t reliable. Make sure you’re in DebugGame(_Editor) configuration if you’re stepping through the code.

Kamrann Good shout their. just done a debug with the build set to ‘DebugGame Editor’ and I am now seeing things working as I would expect them too.

I think Jeff’s comments about the garbage collector is also going to be causing me troubles.

But at least the debugging seems to be working better and showing me things that I would expect :slight_smile:

You can also just disable optimization in ‘Release mode’ and see all the watch variables, code step all work reliably.

Yep, you’ll definitely need to do as Jeff suggests if you stick with using UObjects.

I am curious I moved this class to inherit from UOBJECT to try and get it work correctly in the debug so I could see whats going on.

Now that your advise has helped resolve my troubles of debugging it, Is their any problem with me using Normal C++ Classes ? along side of the Unreal parts ?

No problem, it should be fine to do so. Be aware though that if you don’t use UObjects and UPROPERTIES, and simply embed a C++ class or pointer in your AChunk class, then that data will not be automatically saved and loaded with the level.