Some Craft quality of life improvements

Hey guys, based on feedback from @UberGoober I’ve experimented with a few additions to Craft to make things a little easier to work with. These improvements are mostly meant for Codea 4 but I’ve back ported some of them where it made sense.

The Main Scene

Use craft.scene.main to update and draw a scene automatically

-- This is all you need to do to draw and update your scene
function setup()
    scene = craft.scene()
    craft.scene.main = scene

Entity Callbacks

A set of new callback events in entities let you attach functions that will be called automatically:

  • entity.update
  • entity.fixedUpdate
  • entity.destroyed
function setup()
    -- Create a new craft scene
    scene = craft.scene() = 10 = vec3(0, 180, 0)
    scene.sun.eulerAngles = vec3(45, 45, 0)
    -- Create a new entity
    e = scene:entity()
    e.model = craft.model(asset.builtin.Primitives.Monkey)
    e.update = function(entity, dt)
        entity.y = math.sin(ElapsedTime)
    e.destroyed = function(entity)
    craft.scene.main = scene

function touched(touch)
    if touch.state == BEGAN then 

The plan was is to forward touch events automatically, but this is a little bit complicated and may come later down the line

Remove the “local” in the statement local e = scene:entity() or you’ll get an error when you touch the screen to destroy e.

The calling of the functions automatically is going to be huge!

Very nice!

Oh wow that’s great. Thanks so much.

@dave1707 Removed the extraneous “local”

I gave it a try, it’s a nice extension, and it looks like you’ll be able to use multiple scenarios with ease

I’m having a problem with how to apply the craft.scene.main to the block ?

Here is the test code:

viewer.mode = FULLSCREEN

function setup()
    scene = craft.scene()
    player =, vec3( 40, 20, -10), 80, 1, 900)
    -- ?? voxel ????:???????
    scene.voxels.coordinates = vec3(0,0,0)    
    -- ????? block type
    grass = scene.voxels.blocks:new("myGrass")
    grass.setTexture(ALL, "Blocks:Dirt")
    grass.setColor(ALL, color(239, 222, 5))
    -- ?? craft.voxels ??????? block type:SOLID
    -- ?????? SOLID ????????????????
    scene.voxels:box(0,0,0, 16*8,1,16*8)
    -- ???? block type ??
    -- How to write the block update here???
    craft.scene.main = scene

-- Create a blinky block type
function blinky()
    local Blinky = scene.voxels.blocks:new("Blinky")
    Blinky.setTexture(ALL, "Blocks:Stone")
    Blinky.geometry = TRANSLUCENT
    Blinky.renderPass = TRANSLUCENT
    Blinky.tinted = false
    Blinky.dynamic = true
    Blinky.scriped = true
    function Blinky:created()
        -- Schedule an update in one second
    function Blinky:blockUpdate(ticks)
        local randomColor = color(math.random(128,255), math.random(128,255), math.random(128,255))
        self:set(COLOR, randomColor)

@binaryblues It looks like DeltaTime was not being passed properly to scene:update() causing it to stall voxel updates. I’ve pushed a fix which will be in the next beta and app store versions

@John Thanks so much! A very good news! I’ll be looking forward to the new version.

Can this be pinned, or at least pinned in the Craft Category?

@UberGoober I’m sure when Codea 4 comes out, all of this will be included in the docs. You can copy this info and save it till then. There are too many useless/not updated things already pinned at the top of the forum.

@dave1707 The suggested lack of value of other pinned things doesn’t seem relevant to the decision of whether or not to add something new of high value.

Craft is hard for people to learn, and these enhancements can make it much easier to get started with, and there is literally no other way to learn about them.

@UberGoober There are other ways to learn about those enhancements so you don’t need to pin a couple of lines of code at the top of the forum. The majority of new users probably look at example code (written by you) to see how to do Craft code. So if all your example code has those enhancements in them then the new users will see them and know how to use them. That would be more effective then something pinned that someone probably won’t look at anyways. My opinion is examples are more beneficial when learning something new with dome documentation as a helper.

@dave1707 I myself can’t use these enhancements without looking at this reference—and I suggested some of them! And if I need the reference, newer users need it even more.

@UberGoober You should only need it once and that’s when you use it in one of your own examples. After that you refer to your own code and don’t have to hunt thru the forum looking for something you remember seeing. At least that’s what I do when I see something new or interesting.

@dave1707 regardless, a persistent reference for these major undocumented features would help me a lot, and I think it would help others a lot, too.

:smiley: lol if you keep arguing and bumping it, it will effectively stay at the top