Repository navigation
Tutorials: Methods & Access Modifiers
A method is a function that is class-specific, the signature is identical to skript functions.
class example:
method helloWorld():
broadcast "hello world"
method getBalance(p: player) :: number:
return balance of {_p}Calling methods:
set {_example} to new instance of example
{_example}::helloWorld()
set {_balance} to {_example}::getBalance(player)By default methods and fields are public, meaning they can be accessed from anywhere as long as there is an instance available. You can prefix methods and fields with the private modifier to restrict their access to only within the same class.
class Example:
private field exampleField: boolean = false
private method exampleMethod():
broadcast "i can only be accessed internally!"This brings us onto the instance expression, this refers to the current class instance and can only be used in methods. It can be used to access fields and methods from the same instance.
class banEntry:
private field dateIssued: date
private field duration: timespan
method isActive() :: boolean:
if time since instance::dateIssued < instance::duration:
return true
return false
Access modifiers are most useful when writing apis. Private fields are nice middle ground between {_local} and {-memory} variables, where you can be sure that other code won't interfere with yours but you're still able to store things persistently.