06.UE4学习总结.Blueprints to C++.TMap常用方法

 

 

 

TMap<int32, FVector> Map;

    Map.Add(1, FVector::ZeroVector);
    Map.Add(1, FVector::ZeroVector);

    VecMap.Append(Map);


    const int32 NumElementsMap = VecMap.Num();
    const FVector& EnementMap = VecMap[1];

    TArray<int32> MapKeys;
    int32 NumKeys = VecMap.GetKeys(MapKeys);
    TArray<FVector> MapValues;
    VecMap.GenerateKeyArray(MapValues);

    const int32 Key = 1;
    if (VecMap.Contains(Key))
    {
        
    }


    const FVector* VecPtr = VecMap.Find(Key);
    if (VecPtr != nullptr)
    {
        int32 IntVal = VecPtr->X;
        const FVector& VecRef = *VecPtr;
        IntVal = VecRef.X;
    }

    VecMap.Remove(Key);
    VecMap.Empty();

    TArray<int32> KeysFromMap;
    const int32 Num = VecMap.GetKeys(KeysFromMap);

    for (int32 i = 0; i < Num; i++)
    {
        const int32 MapKey = KeysFromMap[i];
        const FVector& Vec = VecMap[MapKey];
        float Val = Vec.X;
    }
    for (const TPair<int32, FVector>& Kvp : VecMap)
    {
        
    }
    for (const auto& Kvp : VecMap)
    {
        const int32 MapKey = Kvp.Key;
        const FVector& Vec = Kvp.Value;
    }


    for (auto It = VecMap.CreateIterator(); It; ++It)
    {
        int32 MapKey = It.Key();
        FVector& Vec = It.Value();
    }
    for (auto It = VecMap.CreateConstIterator(); It; ++It)
    {
        const int32 MapKey = It.Key();
        const FVector& Vec = It.Value();
    }
    

 一个小案例练习

 

 

//小案例测试

    TArray<AActor*> Actors;
    UGameplayStatics::GetAllActorsOfClass(GetWorld(), AConeActor::StaticClass(), Actors);
    TMap<int32, AConeActor*> ConeActorMap;
    const int32 NumActors = Actors.Num();
    for (int i = 0; i < NumActors; i++)
    {
        AConeActor* ConeActor = Cast<AConeActor>(Actors[i]);
        if (ConeActor)
        {
            ConeActorMap.Add(i, ConeActor);
        }
    }
    AConeActor** ConeActorPtr = ConeActorMap.Find(Key);
    AConeActor* ConeActor = *ConeActorPtr;


    if (ConeActor)
    {
        ConeActor->TestFunction(2);
    }
    

 

posted @ 2022-02-14 23:18  底层逻辑  阅读(289)  评论(0)    收藏  举报