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

Use method by its string name?

Asked by
maxbd 44
6 years ago
Edited 6 years ago

Hello, Community. I have a question that concerns something I tried to do in Roblox Lua and ran into an error.

Question: How could I go about calling a method by it's string name?

For example:

-- In a Local Script.

local mname = "GetService"

if game[mname]("Workspace") ~= nil then
    print("Workspace isn't nil.")
end

This outputs an error saying: "Expected ':' not '.' calling member function GetService."

I've tried a lot of other things I could think of to achieve this, but no success so far.

I know that it should be something like: if game:GetService("Workspace") ~= nil then print("Workspace isn't nil.") end

But how could I call the GetService method with it's string name? Thanks in advance.

0
ok but why DinozCreates 1070 — 6y
1
game("GetService", "Workspace") __namecall User#24403 69 — 6y
1
also game["GetService"] is equivalent to game.GetService, and game:GetService("Workspace") is sugar for game.GetService(game, "Workspace") User#24403 69 — 6y
0
[""] is the alternative method for index something with a StringName that interferes with the Scripts syntaxing, it is the same as . Ziffixture 6913 — 6y
View all comments (5 more)
0
GetService requires a Colon Ziffixture 6913 — 6y
0
No. It does not. As I just said, game:GetService("Workspace") is sugar for game.GetService(game, "Workspace"), if you have some experience in OOP you'd know that. User#24403 69 — 6y
0
The way OP is calling GetService is not with the ":" notation. User#24403 69 — 6y
0
Thanks for the help, yeah, actually I was trying to overwrite the __namecall metamethod for a sandbox environment where everything is wrapped using proxies. maxbd 44 — 6y
0
Well, not overwrite since the metatable is blank. maxbd 44 — 6y

1 answer

Log in to vote
1
Answered by
fredfishy 833 Moderation Voter
6 years ago
Edited 6 years ago

Basically just a writeup of what incapaxian said

The : operator is just "syntax sugar" for "." but with the first argument being the object you're calling from. "Syntax sugar" is a term for something which is syntactically equivalent to something else, but that is easier and cleaner to use. If you ever use Haskell, get ready for a lot of syntax sugar, because the language is basically built on it.

For example, these are equivalent:

game:GetService("Players")
game.GetService(game, "Players")

Furthermore, in Lua, the . operator is just sugar for an array access. The following are equivalent

table["banana"]
table.banana

Every different data structure in Lua is just a table wearing a different hat, so these rules apply to basically any data structure, including objects.

Therefore, this should do what you want:

function methodCall(owner, methodName, arguments)
  owner[methodName](owner, unpack(arguments))
end

methodCall(game, "GetService", {"Players"})
0
Not sugar for an array access, array indices are integer and table.1 would not work. Also, for the methodCall function you can just make the function take a tuple of values User#24403 69 — 6y
Ad

Answer this question