Download

TMap Add method not keeping keys

I do not understand how TMaps are supposed to work… But the TMap won’t keep its keys.
Using this thing gets me really confused, I familiar with C# so maybe I shouldn’t be relating it to the Dictionary but they seem very similar.

I declared my TMap like this in my .h like so
image

in my .cpp file I have a method that uses the TMap; this is the entire method without the signature.
image

Essentially this method makes it so we don’t have overlapping Audio playing on the same “layer”. I am using the TMap to keep track of each layer, represented by int32, and the audio currently playing on that layer.

I had a log at the top and bottom of this method that gave me the TMap.Num() so I can see if they were being added because the audio was still overlapping each other.

However I kept getting 0 at the top and 1 on the bottom. Everytime I called this method, but I shouldn’t because nothing actually removes the audio, it stays there until another audio replaces it. So I expect when calling this method again using the same layer it should have something there but it doesn’t.

Why is it that when I add an element in this method it doesn’t persist outside of the scope?

Can you post the entire function? Preferably also the code that calls it.

That is the entire function the only thing missing was the signature.

but here is the full method with the signature

Blueprints call this method. However the problem definitely lies here in this method and not in anyone calling it because I was doing the same thing with a single AudioComponent reference instead of a TMap and everything worked fine. So it definitely is the TMap.

So you log the number of map elements at the beginning of this function call and it says 0 every time? Different audio components, different layers or same layer, doesn’t matter? And you log at the end of the function call and it says 1 every time?

Logically speaking, something has to happen between function calls that either removes the element or empties the map. Do you call TMap::Resetor TMap::Empty on the components map at any time? What exactly happens between function calls? Is there a scenario where it doesn’t log 0 and one, for example if you call the function two times in a row directly after each other?

At this point, it’s probably necessary to see the entire audio manager class. The function itself looks right to me.

That is the sad part is that is the entire class.

.h

.cpp

There are only calls to that one function. No one has access to the TMap except the AudioManager. So no one is calling the . . . .

OMG, I think I know the issue and I am so dumb.

I am very disappointed in myself. The AudioManager is sort of a singleton where the GameInstance handles the instantiation and destruction of it… I created the object but I didn’t assign it to the instance… In order to call that function you have to go through the GameInstance, essentiaully. So everytime I called that function I was creating a new Audio Manager.

I should have rubber duckied this before posting. sorry for wasting your time! thank you!

1 Like

Well, you figured it out, so it wasn’t really a waste of time after all.

1 Like