Skip to content

Scripting Examples

devilExE edited this page Jan 26, 2025 · 2 revisions

i want to start of this page by saying i use python syntax highlighting for this code snippets, just so that the code isn't pure white, and also because the comments are the same

About triggers

The $onbreak function will only be called from Objects marked as Glass, and non-Disable Trigger.
An important thing to note is that collision detection can get a bit funky, and the function may not be called all the time (especially if the target is really small and it was hit by a bullet)

Collision trigger

Preview

$milk = $:GameObject:Find("milk") # get a reference to the milk
$target = $:GameObject:Find("target") # reference of the target
$onbreak = fun($!coll, $!other, $!reason) # on glass break callback
    if $coll:gameObject != $target
        return 0 # not our object
    fi
    if $reason != 2
        return -1 # we only want to trigger when a bullet hits
    fi
    $p = $milk:transform:position
    $milk:transform:position = $:Vector3($p:x, 1.5, $p:z) # move the milk up (in the example it's below the floor, so we move it to y = 1.5)
    # note that instead of $p you could've also used $milk:transform:position, but the code would get really long
    # alternatively, you can:
    #$p = $milk:transform:position
    #$p:y = 1.5
    #$milk:transform:position = $p
    return 1 # insta break the target
nfu

Moving platforms

Preview

$tunnel = $:GameObject:Find("tunnel") # references to the game objects (note that the 'tunnel' object is a group)
$platform = $:GameObject:Find("platform")
$timer = 0 # timer that counts up
$update = fun($!dt number)
    $timer = $timer + $dt # increment the timer (dt = Time.deltaTime)

    # spinning tunnel
    $!rot = $tunnel:transform:localRotation:eulerAngles
    $rot:z = $rot:z + $dt * 30 # rotate the tunnel with respect to the time since last update
    $tunnel:transform:localRotation = $:FromEuler($rot)

    # moving platform
    $phase = $timer % 4 # loop animation every 4 seconds
    if $phase < 2 # first two seconds move the platform away
        $platform:transform:localPosition = $:Vector3(-15 - 15 * $ease($phase / 2), 0, 0)
    el # last two seconds move the platform close
        $platform:transform:localPosition = $:Vector3(-30 + 15 * $ease(($phase - 2) / 2), 0, 0)
    fi
nfu

$ease = fun($!t number) # ease in-out function [0,1] -> [0,1]
    return $t * $t * (3 - 2 * $t)
nfu

Key pickup system

Preview

$keyCollected = 0
$key = $:GameObject:Find("keyPickup")
$door = $:GameObject:Find("door")
$update = fun($!dt number)
    if $keyCollected # if key was collected, don't try to spin it (will throw nasty errors for null object reference)
        return
    fi
    if $key == nil # if somehow the key got destroyed
        return
    fi
    $!rot = $key:transform:localRotation:eulerAngles
    $rot:y = $rot:y + $dt * 90
    $key:transform:localRotation = $:FromEuler($rot)
nfu

$onbreak = fun($!coll object, $!other object, $!reason number)
    if $coll:gameObject != $key
        return 0 # don't care
    fi
    if $reason != 1
        return -1 # not the player
    fi
    $keyCollected = 1
    $:GameObject:Destroy($door) # remove the door blocking the way to the milk
    return 1 # delete the key
nfu

Sliding door

Preview

$door = $:GameObject:Find("door")
$isMoving = 0
$isOpen = -1
$onbreak = fun($!coll object, $!other object, $!reason number)
    if $coll:gameObject:name != "switch" # only check the name
        return 0
    fi
    if $reason == 2 # only trigger sliding door if it was hit by a bullet
        if !$isMoving # if the door is not moving, start moving it
            $isMoving = 1
        el # else, flip the direction it's moving in
            $isOpen = -$isOpen
        fi
    fi
    return -1 # don't destroy the glass
nfu

$update = fun($!dt number)
    if !$isMoving
        return
    fi
    $p = $door:transform:position
    $p:x = $p:x + $dt * 2 * $isOpen # $isOpen dictates the direction that the door needs to go in
    if $p:x < -8.51 || $p:x > 0.51 # if we reached the end
        if $p:x < -8.5
            $p:x = -8.5
        fi
        if $p:x > 0.5
            $p:x = 0.5
        fi
        $isMoving = 0 # mark the door as not moving
        $isOpen = -$isOpen # flip the direction
    fi
    $door:transform:position = $p
nfu

Clone this wiki locally