
One interface for every kind of pickup
A game's coins, potions and keys each do something different when a player picks them up. A ShotScript interface names what they have in common, so one function can take any of them, and ShotScript Editor holds every class to it. We built a bag of pickups in a Roblox place to see how.
The promise
We worked in a place with a Shot folder in ReplicatedStorage, and gave it a Folder named Pickups. Its main script is Bag, a ModuleScript whose first line, system script, makes it the system's script, the one script that code outside the system can reach. An interface can be a script of its own, but ours sits at the top of Bag, beside the type its method takes. This is the top of Bag:
system script
use Coin
use Potion
use Key
«A player's coins, health and keys»
system type Stats = { Coins : number, Health : number, Keys : number }
«Anything a player can pick up: a name to show, and what it does»
interface Pickup = {
«Shown when it's picked up»
Name : string
«Changes the stats and says what happened»
Apply(stats : Stats) : string
}Pickup holds no code, only a field and the signature of a method, each with its doc. An interface belongs to its whole system, and so does a system type like Stats, so every script of the Bag system knows both by name, with no import.
Three pickups
A class names the interfaces it keeps after implements, and has every member they list. Coin sits beside Bag in the folder, and its first line, system script.Parent.Bag, makes it a script of Bag's system. This is all of Coin:
system script.Parent.Bag
«A coin worth some amount»
class Coin implements Pickup = {
public Name : string
public Worth : number
constructor new(worth : number)
local self : Coin = init()
self.Name = "coin"
self.Worth = worth
return self
end
public Apply(stats : Stats) : string
stats.Coins += self.Worth
return "+" .. self.Worth .. " coins"
end
}
return CoinIts last line, return Coin, is what the script hands out. Potion and Key are built the same way, each with its own field. This is all of Potion, whose Apply heals, never past 100:
system script.Parent.Bag
«A potion that heals some health, never past 100»
class Potion implements Pickup = {
public Name : string
public Heal : number
constructor new(heal : number)
local self : Potion = init()
self.Name = "potion"
self.Heal = heal
return self
end
public Apply(stats : Stats) : string
local before = stats.Health
stats.Health = math.min(stats.Health + self.Heal, 100)
return "+" .. stats.Health - before .. " health"
end
}
return PotionKey's Apply adds a key to the count and says which door it opens.
One function for all of them
The rest of Bag hands out the three classes as Bag.Coin, Bag.Potion and Bag.Key. Its Collect takes a list typed {Pickup}, knows nothing of coins or potions, and calls Apply on whatever it's given:
«Picks things up one after another»
module Bag = {
public Coin : CoinClass = Coin
public Potion : PotionClass = Potion
public Key : KeyClass = Key
function Collect(stats : Stats, items : {Pickup})
for _, item in items do
print(item.Name .. ": " .. item:Apply(stats))
end
end
}
return BagIts last line, return Bag, hands Bag out to code outside the system. In ServerScriptService, a Boot Script named Server uses only Bag, and hands it one of each and a second coin:
-- Picks up one of each
use game.ReplicatedStorage.Shot.Pickups.Bag
local stats = { Coins = 0, Health = 80, Keys = 0 }
Bag.Collect(stats, { Bag.Coin.new(5), Bag.Potion.new(30), Bag.Key.new("gate"), Bag.Coin.new(10) })
print("coins " .. stats.Coins .. ", health " .. stats.Health .. ", keys " .. stats.Keys)The editor marked nothing. We pressed Play, and it printed this:
coin: +5 coins
potion: +20 health
key: opens the gate
coin: +10 coins
coins 15, health 100, keys 1The potion gave 20 of its 30, since health stops at 100.
Holding a class to it
We deleted Key's Apply, and the editor marked it at both ends. In Server, line 6 got a mark under Bag.Key.new("gate"), where a Key goes into the list of pickups:
Table type 'Key' not compatible with type 'Pickup' because the former is missing field 'Apply'And Key got one on its class line, under the Pickup it promises to keep:
Key implements Pickup but has no Apply(stats : Stats) : stringWith Apply back, we made Potion's Apply internal instead. The mark went on Potion's class line:
Potion implements Pickup but its Apply is internal: what an interface promises is publicProblems listed one for Server's line 6 too, this time for the Potion in its list:
Table type 'Potion' not compatible with type 'Pickup' because the former is missing field 'Apply'A string in the list, "gem" after the key, is marked on line 6 of Server under the string, since a string is no Pickup: Expected this to be 'Pickup', but got 'string'.
Before Pickup promises something new
Every class that keeps Pickup has to have whatever Pickup adds, so a new member is the moment to find them all. We added one under Apply, at the top of Bag:
«A player's coins, health and keys»
system type Stats = { Coins : number, Health : number, Keys : number }
«Anything a player can pick up: a name to show, and what it does»
interface Pickup = {
«Shown when it's picked up»
Name : string
«Changes the stats and says what happened»
Apply(stats : Stats) : string
«How rare it is, from 1 to 5»
Rarity : number
}The editor marked every class that keeps Pickup, each on its class line, with what it still lacks:
Coin implements Pickup but has no Rarity : number
Key implements Pickup but has no Rarity : number
Potion implements Pickup but has no Rarity : numberLine 6 of Server got four marks as well, one under the Bag that starts each item in its list. There's nothing to search for: the marks are the list of classes to change.
Nothing at run time
An interface compiles to no code, and Bag's compiled Collect is the loop as written.
So the promise is checked while you write, and costs the game nothing when it runs.
