Heres an example:
I want a vector3 as an example.
My data type name is “Vector3”.
local dataType="Vector3"
dataType.new(1,5,5)
Hovewer, i cant do it.
Is there a possible way to do it?
Heres an example:
I want a vector3 as an example.
My data type name is “Vector3”.
local dataType="Vector3"
dataType.new(1,5,5)
Hovewer, i cant do it.
Is there a possible way to do it?
Not sure what you’re trying to achieve here, I am not sure if my information is correct but I think you can try it with table
-- Module is a table of dataTypes in your case
local Types = require(path.to.Module) -- Or you can just have {["Vector3"] = Vector3}
local DataType = "Vector3"
Types[DataType].new(1, 3, 4)
-- The module script:
return {
["Vector3"] = Vector3
}
There might be better way of doing it, But this is the only way i know of.
Still, Not sure what you’re trying to achieve with this.
Trying to access a datatype by string is like trying to get a variable by a string: not possible, as datatype “names” only exist in the code editor. One way to work around this would be to use what ItzMrRatsP said, and just link needed datatypes via a table.
You cannot use strings.
Just… do this instead:
local dataType = Vector3
dataType.new(1,5,5)
What you want is the factory pattern. Create various types of objects from their identifiers (for example strings). In Roblox, we have Instance, Instance.new(“some_class_name_as_string”).
In your case, it would return constructor objects that would build the desired object
-- ModuleScript
local Vector3D = {}
Vector3D.__index = Vector3D
function Vector3D.new(x: number?, y: number?, z: number?)
local self = setmetatable({}, Vector3D)
self.x = x or 0
self.y = y or 0
self.z = z or 0
return self
end
local Vector2D = {}
Vector2D.__index = Vector2D
function Vector2D.new(x: number?, y: number?)
local self = setmetatable({}, Vector2D)
self.x = x or 0
self.y = y or 0
return self
end
local classes = {
Vector3D = Vector3D,
Vector2D = Vector2D,
}
local function DataType(typeName: string)
local class = classes[typeName]
if not class then
error(`'{typeName}' undefined.`, 2)
end
return class
end
return DataType
-- some Script
local dataType = DataType("Vector3D")
local v1 = dataType.new(3, 4, 0)
local v2 = dataType.new(1, 1, 2)
print(v1,v2)
local dataType2 = DataType("Vector2D")
local pos = dataType2.new(10, 20)
print(pos)
I don’t understand. You want to get a type from a string containing its name? Or, do you want to make a new type?
If you want to make a new type, you can do something along the lines of this:
type MyVector = {
x: number,
y: number
}
function createVector(x: number, y: number): MyVector
return {
x = x,
y = y
}
end
Are you trying to create some sort of parser?
Something like that?
If you’re trying to access the Roblox built-in Vector3 library through the string alone, you can’t that way.
All of Roblox’s globals are contained within the environment of the main thread, which the metatable of every script’s environment redirects to (via __index).
Therefore, you can index that string with the thread’s environment table to do this dynamically.
--a word of warning, getfenv can deoptimise the thread environment.
--this is the most dynamic approach but not necessarily the most optimised.
local libName = "Vector3"
local senv = getfenv()
local v = senv[libName].new(1, 5, 5)
Thank you! This helped me.
However, i have a question.
How to convert string into data type
Like if i want string to be “0, 5, 5”. How to convert it to vector 3 as an example?
you can do some string matching to extract the numbers.
local results = {}
for num in string.gmatch(str, "%d") do
table.insert(results, num)
end
--results now contains your x, y and z values.
local x, y, z = table.unpack(results)
local senv = getfenv()
local vec = senv[libName].new(x, y, z)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.