Sounds, lights and effects
Sounds
A Sound inside a part is heard around that part; inside a Tool, around whoever holds it; anywhere else, everywhere.
local sound = new("Sound", world.Coin)
sound.soundId = "coin" -- a built-in sound, or "asset:123" for one you uploaded
sound.volume = 0.8
sound:play()| Member | |
|---|---|
soundId | "coin", "click", "swing", "hit", "win", "music", or "asset:123" |
volume | 0 to 1 |
looped | Plays round and round |
playing | Whether it is playing |
play(), stop() |
- A Script's sound plays for every player (a looped one also for players who join later). A ClientScript's plays for that player only.
- In KHIM Engine, a Sound's Play at start plays it for everyone when the game starts - background music with Looped.
- Upload your own
.wavsounds in the Assets panel (Models, pictures and sounds).
Lights
A Light inside a part makes the part give off light: a lamp, a torch, a neon sign that really lights the wall, a headlamp.
local lamp = new("Light", world.Lamp)
lamp.color = rgb(255, 200, 140)
lamp.brightness = 2 -- 0 to 10
lamp.range = 20 -- units, up to 60
lamp.angle = 360 -- all round; less is a cone (a spotlight)
lamp.direction = vec3(0, -1, 0) -- a cone's direction, in the part's own directions
lamp.innerAngle = 30 -- a cone's full-strength middleLights show best at night or indoors. Up to 16 lights are drawn at once at the top graphics levels (fewer on weaker devices) - the ones nearest the camera. Lights don't cast shadows.
The time of day: lighting
lighting.clockTime = 18.5 -- 6:30 pm
lighting:tween({clockTime = 20}, 60) -- dusk over a minute
lighting.brightness = 2
lighting.fogColor = rgb(200, 210, 230)
lighting.fogStart, lighting.fogEnd = 50, 400
lighting.globalShadows = true
print(lighting.timeOfDay) -- "18:30:00"| Member | |
|---|---|
clockTime | Hours, 0 to 24 (or timeOfDay as "hh:mm:ss") |
brightness | The sun's strength |
ambient, outdoorAmbient | Light that comes from everywhere, indoors and outdoors |
fogColor, fogStart, fogEnd | Fog, in units from the camera |
globalShadows | Shadows on or off |
exposureCompensation | Brighter or darker overall |
geographicLatitude | How high the sun goes |
getSunDirection() | Where the sun is |
The sky, sun, moon, stars and clouds follow clockTime by themselves. A Script's change reaches every player; a ClientScript's only its own player.
Particles
A ParticleEmitter (Particles in KHIM Engine's menus) throws off fire, smoke, sparks, magic, rain... Insert one on a part, then pick Start from in the Inspector for one of 17 ready-made effects - Fire, Smoke, Sparks, Sparkles, Magic aura, Embers, Shockwave, Explosion, Electric, Heal, Portal, Rune circle, Rain, Snow, Leaves, Dust, Bubbles - and change any number from there.
local fx = new("ParticleEmitter", world.Sword.Blade)
fx.texture = "Sparkle"
fx.rate = 30 -- a second
fx.lifetime = vec3(0.5, 1, 0) -- between 0.5 and 1 second
fx.speed = vec3(2, 4, 0)
fx.color = rgb(140, 90, 255)
fx.lightEmission = 1 -- glows where they cross
fx.lockedToPart = true -- they move with the part
fx:emit(20) -- a burst of 20 nowSome of what it can do: pictures and flipbooks, sizes, colours and transparency that change over each particle's life (NumberSequence, ColorSequence), shapes to throw from (box, sphere, cylinder, disc, cone), bursts on a schedule, drag, turbulence, collisions, trails, and sub-emitters. A ParticleForce on a part pulls, swirls or blows every particle near it. An Effect plays all the particles on its part and inside it as one: play(), stop(), clear().
Particles are drawn by each player's device: a ClientScript's emitter is that player's alone.
Pictures on parts: Decal and Texture
local poster = new("Decal", world.Wall)
poster.image = "star" -- or "asset:123"
poster.face = "Front" -- "Front", "Back", "Left", "Right", "Top", "Bottom"
local tiles = new("Texture", world.Floor)
tiles.image = "asset:456"
tiles.face = "Top"
tiles.tileSize = vec2(4, 4) -- repeats every 4 unitsHighlight
Outlines its part (and everything inside it) and tints it - through walls unless depthMode = "Occluded":
local glow = new("Highlight", world.Treasure)
glow.fillColor = rgb(255, 220, 0)
glow.fillTransparency = 0.6
glow.outlineColor = rgb(255, 255, 255)Beam and Trail
local laser = new("Beam", world.Turret) -- a band from one part to another
laser.part1 = world.Target
laser.color = rgb(255, 40, 40)
laser.width0, laser.width1 = 0.3, 0.3
local trail = new("Trail", world.Ball) -- the streak a moving part leaves
trail.lifetime = 0.5
trail.width0, trail.width1 = 1, 0Who sees changes to effects
The Decals, Textures, Highlights, Beams, Trails, Lights and particles a world is saved with reach every player. After that each player's device keeps its own copy: a server Script's change to one does not reach players (except a ParticleEmitter's settings and emit). To switch a light or change a decal for players, do it from a ClientScript - the server can tell clients to with net.sendAll:
-- Script
net.sendAll("lights", false)
-- ClientScript
net.on("lights", function(on)
for _, lamp in tags.get("StreetLamp") do lamp.Light.enabled = on end
end)