How can I simplify my code when checking if a map has a key?

I have an if-else pattern, but I don’t know how to write the if statement in order to negate it. The code in the else block is what matters to the function.

    # The players to keep alive and avoid elimination. The int value is unused.
    var SurvivingPlayers : [player]int = map{}

    EliminateNonSurvivors():void=
        for(Player : GetPlayspace().GetPlayers(), Character := Player.GetFortCharacter[], Character.IsActive[]):
            if(UnusedValue := SurvivingPlayers[Player]):
                var nothing : int = 0 # Does nothing, just to keep the if-else pattern.
            else:
                Character.SetVulnerability(true)
                Character.Damage(99999.9)

How can I clean up my code? Thanks :slight_smile:

Is there a specific reason you are using a map over an array? With an array, you can use the (A : []t).Find[t] function which returns the index, or fails if the element t is not in the array. So you could do

var SurvivingPlayers : []player = array{}
for ( Player : GetPlayspace().GetPlayers()):
    # If a player is not in the array, the Find() function will fail, however 
    # we negate it using "not", meaning that the if will only execute if the 
    # Player is not in the SurvivingPlayers array
    if (not SurvivingPlayers.Find[Player]):
        # Damage code here

Thinking about it now, I was concerned about the quadratic time complexity of searching an array in a for loop, but since it’s only like max 100 players in the worst case. I think using an array would be okay because I am not calling this function often.

However, I can imagine encountering future similar situations where I need to iterate often and be concerned about performance. I was hoping there was a Set or Hash like data structure, but I think a Map is the closest to having a fast lookup.

That makes sense. In that case, why not make use of the value by converting it into a logic type, and including all players inside the map. Then you can set the logic value to true or false if you want to eliminate them or not.

Thanks, I think that solves it for this specific case.

You do the original request like so:

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

game_device := class(creative_device):

    Map: [int]int = map{}

    OnBegin<override>()<suspends>:void=

        if(not Map[0]):

            Print("No value")
1 Like

Thanks, this seems to be exactly what I was looking for.

How would this be quadratic? The time complexity for a simple for iteration would be linear, therefore O(N) in the worst case. The map is similar to a dictionary from other languages and I would strongly assume that a single lookup is of constant complexity O(1).

However the map’s key type is only comparable (aka. equatable) and not something like “hashable”. So the lookup might actually not be O(1) after all.

You are right, especially considering the way I worded the following:

I was concerned about the quadratic time complexity of searching an array in a for loop

Maybe I should have worded it like:

I was concerned about the quadratic time complexity of calling the Find function for every iteration in the for loop.

Assuming Find is O(N).

I never was good at English. Thanks for helping me improve the way I word things.

My assumption is that Find is O(N), and then being called in an O(M) for loop is O(NM) being quadratic. Maybe I’m having a stroke or something, but that’s how I assume it was quadratic.

Thanks. I welcome more corrections. :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.