When a function is called I’d like to make my map, AssignedToPlayer[Player] basically set it to some version of nil, empty, whatever verse calls it.
var AssignedToPlayer :[player]vehicle_spawner_dirtbike_device = map{}
Was looking in the docs for 15 mins and couldn’t find it, probably will take two seconds for someone else to answer it and I’m sure thousands of people will Google for this in the future and land here lol
Okay ended up being harder than I thought. But here’s the info from the docs for anyone else that is curious.
# Removes an element from the given map and returns a new map without that element
RemoveKeyFromMap(ExampleMap:[string]int, ElementToRemove:string):[string]int=
var NewMap:[string]int = map{}
# Concatenate Keys from ExampleMap into NewMap, excluding ElementToRemove
for (Key -> Value : ExampleMap, Key <> ElementToRemove):
set NewMap = ConcatenateMaps(NewMap, map{Key => Value})
return NewMap
Indeed, if you want to use a Map and later remove elements to it, it’s yucky. Seems expensive. I’ve since done away with using Maps if I need to randomly add/remove things during runtime, easier to just use two parallel arrays (keys in one array, values in second array - indexes are parallel).
Though FYI, that’s not an empty value - it’s removing the key and value. If you want to “empty” a value, just set that key to your “empty” value. E.g. -1 if it’s an int. This is up to you though, what your code considers “empty”; int being a primitive type has no such thing as an “empty value”. So rather than doing an expensive clone-with-filter every time you want to remove something, consider instead setting the value to a “not set” value of your choice. That of course depends on how you’re using your Map - if you are often adding and removing and rarely iterating then this approach is good. But if you rarely remove and often iterate, then stick with what you’ve got (or use two parallel arrays). And again it depends how many “empty values” your Map might have at runtime, wasting ops checking if value is > -1 for example.
Ah, Collections optimization I may be premature here with all the suggestions, not a huge fuss - but you might find that such optimizations quickly becomes necessary in Verse, especially if you’re doing a tonne of stuff.
Yeah that’s smarter ha. Will probably avoid separating it into two arrays for now and going with the “empty” value. Didn’t really consider just -1 for optimization purposes but that’d definitely work for my case. Thank you!
As noted above, it is not necessary to remove the element from the map, you can make the value optional and set it to empty.
In your case it will look something like this:
var AssignedToPlayer :[player]?vehicle_spawner_dirtbike_device = map{}
# set as empty
if (set AssignedToPlayer[Player] := false) {}
I know this topic is a month old but this is still an issue.
Eventhough there are workarounds, the language should let you remove an element from a map, period.
EDIT: Came up with this more generic function, also it fails if the index is not found
You use it like this :
if(UpdatedMap := MyMap.WithRemovedKey(1)?):
set MyMap = UpdatedMap
Show Code
# Returns the same map with the provided index removed, returns option{false} otherwise
(Input:[t]u where t:subtype(comparable), u:type).WithRemovedKey(KeyToRemove:t)<transacts>:?[t]u =
var NewMap:[t]u = map{}
var FoundIndex:logic = false
for (Key -> Value : Input):
if(Key = KeyToRemove):
set FoundIndex = true
else:
set NewMap = ConcatenateMaps(NewMap, map{Key => Value})
if(FoundIndex?) then option{NewMap} else (__ :?[t]u = false)
You can also use it like this :
if(set MyMap = MyMap.WithRemovedKey(1)?):
option{set MyMap = MyMap.WithRemovedKey(1)?}
I’m not sure how those two work when index is not found (but they do).
I guess as long as your variable is not of type ?[t]u, the unwrapped option{false} will not be set and the setter will fail silently ?
NOTE: Didn’t find a way to make the function failable using failure bracketting style, if someone wants to show me, I’m in !