Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I assign data types besides strings and integers as a key to a table?

Asked by 6 years ago
Edited 6 years ago

I'm working on making a 2D tile-based game, and I believe that storing Vector2's in a table to find the information of a tile would be quite useful. To explain better, this is what I'm trying to do:

local worldData = {
    tiles = {
        Vector2.new(0, 1) = "wall"
    }
}

I've tried both without and with the '.new', but to no success. The wiki explains that "both the key and value can be any Lua value (numbers, strings, Parts etc., and even other tables) except nil." Sadly the wiki does not provide any example of setting the key to a data type besides a string and integer.

As a side question, I've also noticed that you can't manually add in a Vector2, along with many other values, to objects in Roblox Studio's explorer. Any way to do that?

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

As you probably know, you can use strings as a key in dictionaries either with the notation {field = 4} or {["field"] = 4}. Every other data type will need to be wrapped in square brackets.

local worldData = {
    tiles = {
        [Vector2.new(0, 1)] = "wall"
    }
}
0
Cool. So the Vector is added as a key to the table. How do I read its value? I've attempted print(worldData.tiles[Vector2.new(0,1)]) as well as other things to no luck. Celt_ican 74 — 6y
0
You can't do that since the key is the identity of the vector itself and not the 0,1. One thing you could do is set the key to `["0, 1"]` and index `tiles` by doing `worldData.tiles[tostring(Vector2.new(0, 1)]` creeperhunter76 554 — 6y
Ad

Answer this question