Can't Clear or Remove key from Map Variable

For an Int|Actor Map with 3 elements the following prints:

  • 3
  • Removed
  • 2

Removing seems to work fine.

If you want to keep the Key but Clear its value, you can replace with null reference:

213754-empty.png

Untested but should work :slight_smile:

I have a map varible, witch contains Integer (as type A) and Actor Reference (as type B). The problem is that I need to clear this variable in some point of my code, but it simply does not work. Remove particular key is also doing nothing.

I also found that it works fine with map variable “Integer:String” or “Float:Vector”.

So the question is wheter it’s a bug or is it simply not posible to clear “Integer:Actor Reference” map variable. Thanks.

It should, but it does not (

Just tested it and it works exactly as advertised. As demonstrated above, removing and/or replacing the key with a null object reference produced the effect you described. More in-depth:

Could you provide an example screenshot of it not working.

Indeed, I can solve that problem with replacing all existing keys in map with null actor ref. Thats probably not the way it should be done, but it works.

Btw Remove or Clear still doesnt work. I found out that if you print lenght of map it will give you “0” and you might think that it actually did cleared map, but in fact it doesnt, you can tell that by printing the actor name at its key (screen attached). So if you try to get data from key, that suppose to give you no data (witch I’m trying to do), it actually will give you that “not-actually-cleared” data.

Hello,

I’d have to disagree with you. Have a look, create 5 entries with keys 0, 1, 2, 3, 4. Remove 2nd key, get its object name. The object is gone:

Can you show how you remove it, your screenshot does not cover that.

Map variable looks perfect for many needs. I tired to make inventory system on it and here goes problems.

As I found here Working with Maps | Unreal Engine Documentation

"Currently, Map values are immutable, which means that they cannot be modified after they have been created. "

As I understand, it is read only for Keys at last. I tried many tricky ways, like a removing whole key and update Values then add it again, but it partially works - I have random results, generally errors.

For now I think maps works only as pre defined variables and update it is on your risk. I hope I’m wrong and I would be happy to find somewhere proof that you can update maps - keys and values, I hope I just have limited skills for it.

After all those years…

You’re free to dynamically add / remove map keys as you please afaik. Adding an existing key overrides key’s value.

  • below, the map elements are added with numerical keys

  • RMB removes #3
  • to demo what this sequence does, I press 1,2,3,4 and then RMB, resulting in:

image

Any specific example that gives you a headache?

After all those years…

A blast from the past, indeed!

1 Like