Dynamic Texture MemCpy

I have a snippet like this:

(taken from A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums)

This should create a solid-color array that is sized 128x128 in a material (I’m replacing a material texture inside a blueprint via the CreateDynamicMaterialInstance node). The UE_LOG reports that the size of the colors array is 65535. When this texture is applied ingame, it looks like this:

104370-2.png

However, 65535 seems incorrect. 128x128=16384. FColor is a 32bit struct (right?), so it should take 524288 bytes of memory - unless I’m being a moron.

But if that’s the case, my generated texture should be showing on only 1/8th of the material. I’m confused by the FMemory::Memcpy. I feel like my colors array is being generated fine, but I’m getting garbage out.

Does anyone have any advice here? Many thanks in advance.

You are passing the array variable as the source for Memcpy. There are two errors there:

  • Memcpy expects an address of some data, ie. a pointer to that data.
  • But the real data of TArrays are somewhere inside them, as they are types with methods etc. You get the address (pointer) of the raw data using colors.GetData(), as described here.

Now, the size thing you got it a bit wrong (not a moron though XD), but even then it seems not right. sizeof gets the size in bytes, so FColor gives 4. That way, your UE_LOG should output 128x128x4 = 65.536. I don’t understand why it’s outputting 1 less.

Solved my own issue. I had a couple of problems in the code, below is the correct working code (fixed lines underlined with red):

And to show it’s working, here are in-editor and in-game shots to show what the material looks like with a default texture and with my dynamic texture:

104434-5.png

Oh, that’s right! I didn’t see you were already creating a pointer of the array using new. That way you were, indeed, passing a pointer to Memcpy. Just the wrong one XD

Glad to see you solved it.

It’s really work?Why my code can’t work?