so when i define the type of a variable, it goes totally alright:
local Player: Player = nil
but when i try to define the type of an element in a table, it doesnt go exactly how i thought it would:
PlayerProfile.Player: Player = nil
Missed symbol `(`.Luau Syntax Check.(miss-symbol)
this is my first time working with type so anyone know the correct way of doing this?
You can't arbitrary set type to random table member in Luau. You need to set types for all members on table creation or inside its creation scope.
On creation you can simply set type for each field directly:
type PlayerProfile = {Player: SomeType, OtherField: SomeOtherType}
Or you can express member types by first creating table with zero expression {} and assigning typed values to its members before leaving creation scope. But as soon as you leave that scope the table is "sealed" and no more changes are allowed.
local PlayerProfile = {}
PlayerProfile.Player = "string"
PlayerProfile.SomeField = 123 -- number, types are inferred from initialization values
Related
As I've said above, I'd like to ask if you could get an instance out of others with the same name by the attributes given to it.
If you already have the set of objects and you need to find one specifically, you could iterate over all of the objects and check if one has the attribute you're looking for :
-- find all the Instances in a Folder
local instances = game.workspace.PartsNamedFoo:GetChildren()
-- find the specific instance based on a specific attribute
local foundInstance = nil
for _, instance in ipairs(instances) do
if instance:GetAttribute("SOMETHING") == "TheOneYouAreLookingFor" then
foundInstance = instance
break
end
end
-- do a thing now that we have it
if foundInstance then
print("Found an instance with the right attribute :", foundInstance)
else
warn("Could not find an instance with the matching attribute")
end
But if you don't already have that set of objects, there does not appear to be a way to locate an object based on its attributes.
For reference :
the functions that interact with Attributes and
all of the other functions exposed on Instances,
Hello guys i am trying to add Divisions to a game but there is a problem with tables, do you guys know how to solve this?
local divisonName = result3[i].name
print(divisonName)
ESX.Divisions[result3[i].owner].divisonName = {}
it's the code it should get division name and create a table with that like this (we assume divisonName gonna return swat for example):
["police"] = {
["swat"] = {
},
},
but instead of putting division name as SWAT it will put divisionName variable
i already print that divisionName variable and console return me SWAT so everything is fine with logic and value of variable but i guess there it's syntax issue i am not sure!
Console Debug image
Note that in Lua the construct some_table.field is a syntactic sugar for some_table["field"]. Whatever is written after the dot will be treated as a string key.
In your case, to index by the value stored in the variable, you need to write ESX.Divisions[result3[i].owner][divisonName], and not as .divisionName.
__index is called when accessing as immutable :
local foo = bar["foo"];
__newindex is called when access as mutable an index that doesn't exist :
local bar = { }
bar["foo"] = 123 -- calls __newindex
bar["foo"] = 456 -- does NOT call __newindex
Is there a metamethod that can be called when accessing a key as mutable evey time, i.e not only if the key doesn't exist yet?
I would like to create a behavior so that when a users sets a key in a table, it calls a native method instead, regardless if the key already exists or not.
The standard way to do what you want is to use a proxy table, that is an empty table with suitable metamethods to access the actual table. Since the proxy is empty, the metamethods are called every time you get or set fields in it.
I am rather sure there are no such metamethods you ask for.
But you can try make a workaround to get what you want.
For example you can try to use the __call metamethod in this way:
local mt = {}
function mt.__call(tbl, key, val)
-- this is called every time you use bar(key, val)
tbl[key] = val
end
local bar = setmetatable({}, mt)
bar("foo", 123)
bar("foo", 456)
print(bar.foo)
Or you could use a function in some other way to achieve this.
Immutability doesn't exist in Lua, you just mean indexed accessing and assigning. Lua 5.3 states...
This event happens when table is not a table or when key is not
present in table.
...for both cases.
Your best option would be to store values in another table or subtable of yours.
I created a record type
type IpRanges = {ipStart: IPAddress; ipEnd: IPAddress; subnet: IPAddress; mask: IPAddress}
How can I create a collection type of IpRanges?
type CollRanges = ???
What kind of collection do you want to create?
A list of IPRanges? If so you can do it like this:
type CollRanges = List<IpRanges>
But since list is generic your CollRanges doesn't really need to be a new type, indeed the previous line will create a type alias which is a good thing since you will reuse all the functionality of the List type and your collection will be more cohesive (plugabble) with existing F# code. The same applies to any other generic collection, like Array, Seq, ResizeArray, ...
Is there an easy way to get a default value from an Erlang record definition? Suppose I have something like this:
-record(specialfield, {
raw = <<"default">> :: string()
}).
I would like to have some way to retrieve the default value of the raw field. Something like this would be very simple:
#specialfield.raw % => <<"default">>
This is not possible. I would need to instantiate a record in order to get the default value:
Afield = #specialfield{}
DefaultValue = Afeild#specialfield.raw
DefaultValue % => <<"default">>
Is there an easier way of doing this? I seems like there should be some way to retrieve the default value without having to create an instance of the record.
How about:
raw_default() -> <<"default">>.
-record(specialfield, { raw = raw_default() }).
And now you have a function with the default in it. This will be extremely fast since it is a function call to a constant value. If this is also too slow, enable inlining.
Constructing an empty record and accessing one field can be done on one line:
(#specialfield{})#specialfield.raw.
Take a look at erlang - records, search section "11.8".
There's not much special about records - they're just a tuple at runtime. So to get the field raw from the tuple of default values that is the internal representation of #specialfield{} you would use:
element(#specialfield.raw, #specialfield{}).
In this case, #specialfield.raw is the index of the value for raw in the #specialfield tuple. When you pass in specialfield that resolves to a tuple in the form {specialfield, <<"default">>}.