I’m trying to follow the Roblox Lua style but I have some questions. Java is my first language and basically everything in that language is camelCase (the methods, variables, etc.) whereas the constructors and class names are CamelCase. I notice that roblox uses methods like GetChildren(), so if I’m designing objects in Lua, should I be using CamelCase? It would look weird if I had for say redCar:moveCar() when Roblox uses uppercase syntax. Just wanting my code to look as professional and organized as possible. Please leave me some tips.
3 Likes
Class names, class members are meant to use PascalCase, variable names/parameter names using camelCase.
So think of it somethng like this.
public class RedCar {
public void MoveCar(int carSpeed) { // using PascalCase yes
// code that moves car
}
}
public class Main {
public static void main(String[] args) {
RedCar newCar = new RedCar(); // using camelCase for the variable name
newCar.MoveCar();
}
}
Rough equivalent of Luau:
local RedCar = { }
RedCar.__index = RedCar
function RedCar.new()
return setmetatable({ }, RedCar)
end
function RedCar:MoveCar(carSpeed: number)
-- code that moves car
end
local newCar = RedCar.new()
newCar:MoveCar(50) -- mph
So no you would do RedCar:MoveCar(carSpeed)
.
4 Likes
Coming from java myself, when creating objects or anything, I tend to use camel-case especially for functions. I believe it is just out of habit. Variable declaration and object declaration can really be done with camel-case or without. For example:
function GameHandler.new(playing : table, act : table)
local newgame = {}
newgame.Playing = playing or {}
newgame.active = act or {}
print(type(playing))
return setmetatable(newgame, GameHandler)
end
function GameHandler:checkPlayers(plrs)
--function
end
As you see the function I created use camel-case. I’m not as familiar with Lua but I’ve have no issue using camel-case.
Edit: Happy Birthday @sjr04
1 Like