@this and receivers
CL++ is not C++. There is no Player*, no ->, and no this->x. Inside a method definition Class::Method, @ is the receiver sigil.
| Write | Luau |
|---|---|
@this | self |
@janitor | self.janitor |
@coins | self.coins |
hello.Greet(player) | hello:Greet(player) |
other.Register(@this) | other:Register(self) |
Declare the type in a .clh. Implement methods with Class::Method in a .clp / .clpp. void init() holds one service object and a local Janitor.
void CombatServer::BindPart(BasePart part) { @janitor.Add(part, "Destroy"); other.Register(@this);}
void Wallet::Add(int n) { @coins = coins + n; post("wallet " .: @coins);}function CombatServer:BindPart(part: BasePart) self.janitor:Add(part, "Destroy") other:Register(self)end
function Wallet:Add(n: number) self.coins = self.coins + n print("wallet " .. self.coins)endWhat init looks like
Section titled “What init looks like”init does not use @this. It constructs the service and captures it in the ~>Connect callback.
void init() { Players players = GetService<Players>(); LeaderstatsServer leaderstatsServer; Janitor janitor = new Janitor(); leaderstatsServer.janitor = janitor;
for (Player player in players.GetPlayers()) { leaderstatsServer.PlayerEntered(player); }
players.PlayerAdded~>Connect(func (Player playerEntered) { leaderstatsServer.PlayerEntered(playerEntered); });}See syntax for the operator table, and Janitor for @janitor plus the library.