You probably could use something like this
--in player added
Players[player.Name] = {--[[data]]}
--in player removing
Players[player.Name] = nil
You probably could use something like this
--in player added
Players[player.Name] = {--[[data]]}
--in player removing
Players[player.Name] = nil
@colbert2677 @Halalaluyafail3 @HaxHelper:
I’ve learnt so much today and my code actually works. This is my first good looking, readable, functioning OOP game (in development
).
When I decided to make an OOP event, I made it, and then also made it without using OOP. (no metatables, or hidden values) After not using the metatables, and all their shenanigans the performance for connecting events increased by about 30%
Not sure why you need to use OOP, care to explain?
I want to be able to have my main functions and data stored in ideal conditions so that I can easily access them and know what I’m doing, and it just seems to me that OOP handles that pretty well (for lua). I did initally start off making lots of functions in scripts, and then using Bindable Functions/Events.
I don’t know why your OOP events were so resource heavy. I hope mine isn’t xd.
I’m pretty sure the reason was that I used the metatables,
i.e
local tab = setmetatable({},{
__index = {5,2,3}
})
print(tab[1])
--vs
local tab = {5,2,3}
print(tab[1])
The first indexes the table, which then searches through the __index table, and then returns the value.
The second indexes the table, and then return the value.
Removing one step seemed to have a reasonable impact on performance.
You’re scaring me now xd
Surely the metamethod can’t have that much of an impact. Maybe it’s also because of the physical workspace objects you were tweaking and the rate of tweaking.
No need to be scared. You are correct to say that the metamethod doesn’t have much of an impact, but you need to be careful with what it is that you’re doing. Sometimes it can have a noticeable impact.
The performance difference in the given example is marginal. A 30% speed up is, sorry to say, a result of improving bad code. I put the sample code through a speed tester and looking through the table without the metatable is only ~2% faster than without (averaged between multiple tests).
local Functions = { }
Functions["Function1"] = function()
local tab = setmetatable({}, {__index = {5, 2, 3}})
print(tab[1])
end
Functions["Function2"] = function()
local tab = {5, 2, 3}
print(tab[1])
end
require(2110831719).new(1, "Table Lookup", Functions)
This isn’t enough to warrant concern or leave metatables.
Using them or avoiding them is completely dependent on what you want to do. In some cases (like if you’re creating pseudoclasses), it’s better to have a metatable with you. It’s not like you can make values magically exist.
local tab = {5, 2, 3}
and
local tab = setmetatable({}, {5, 2, 3})
are two different things. Depending on your scenario, very different things. It’s all about context. Context is quite important in the writing and application of code. In terms of what’s happening though, the table with a metatable does two lookups while the single-dimension table only does one.
I was spending the past few hours really confused because I had made dictionaries in the Players table, but doing #Players
returned 0, and I couldn’t do Players[Integer]
.
I found out by myself that apparently dictionaries in a table don’t have an index value, and instead, to access them, you do:
-- Table
local myTable = {
"This" = "is",
"a" = "dictionary"
}
print(myTable["This"])
Or
print(myTable.This)
To iterate over a table containing dictionaries
, using a for loop with #myTable
wont work. You’ll have to use the pairs
function to write a for-loop that goes through every key and value in a table.
local myTable = {
"This" = "is",
"a" = "dictionary"
}
for k, v in pairs(myTable) do
print(k, "=", v)
end
You are correct about tables with a defined key not being accessible by an integer. Every table is made up of a key and a value. If you define a table without a key, e.g:
local myTable = {
"Value1",
"Value2"
}
this is essentially the same as:
local myTable = {
[1] = "Value1",
[2] = "Value2"
}
This table can be indexed with an integer value because the key is an integer, e.g. myTable[1]
would return "Value1"
However if you define a table with a key, such as in the following example:
local myTable = {
v1 = "Value1",
v2 = "Value2"
}
Then you cannot use myTable[1]
because there is no key 1
in that table, instead you need to directly access the defined key myTable["v1"]
.
Also, in the example you gave in your reply:
This will not work and will return the following error:
[string "<eval>"]:2: '}' expected (to close '{' at line 1) near '='
To use a custom key you will have to use one of the following methods:
local myTable = {
v1 = "Value1",
v2 = "Value2"
}
or
local myTable = {
["v1"] = "Value1",
["v2"] = "Value2"
}
I would typically go for the first out of the two.
Thank you very much for the explanation of the key/value pairs.
Yeah I also forgot whilst typing this, that ROBLOX would throw an error because of the incorrect syntax. I used the correct method in my code, though.