Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Some Craft quality of life improvements

JohnJohn Admin Mod
edited January 8 in Codea Craft Posts: 787

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
end

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()
    scene.camera.z = 10
    scene.camera.eulerAngles = 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)
    end

    e.destroyed = function(entity)
        sound(SOUND_POWERUP)
    end

    craft.scene.main = scene
end

function touched(touch)
    if touch.state == BEGAN then 
        e:destroy()
    end
end

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

Comments

  • dave1707dave1707 Mod
    Posts: 10,049

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

  • Posts: 51
    The calling of the functions automatically is going to be huge!
  • Posts: 1,364

    Very nice!

  • Posts: 1,826
    Oh wow that’s great. Thanks so much.
  • JohnJohn Admin Mod
    Posts: 787

    @dave1707 Removed the extraneous "local"

  • Posts: 298

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

  • Posts: 298

    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 = scene.camera:add(OrbitViewer, vec3( 40, 20, -10), 80, 1, 900)
    
        -- 设置 voxel 地形参数:大小规模和坐标
        scene.voxels:resize(vec3(5,1,5))      
        scene.voxels.coordinates = vec3(0,0,0)    
    
        -- 创建自己的 block type
        scene.voxels.blocks:addAssetPack("Blocks")  
        grass = scene.voxels.blocks:new("myGrass")
        grass.setTexture(ALL, "Blocks:Dirt")
        grass.setColor(ALL, color(239, 222, 5))
    
        -- 使用 craft.voxels 默认提供的基础 block type:SOLID
        scene.voxels:fill("myGrass")
        -- 用默认的方块 SOLID 创建地面,球体,线条,单独的方块
        scene.voxels:box(0,0,0, 16*8,1,16*8)
    
        blinky()
        scene.voxels:fill("Blinky")
        scene.voxels:sphere(40,50,90,16)
    
        scene.voxels:fill("Blinky")
        scene.voxels:line(10,20,0,10,50,30)
        scene.voxels:block(vec3(30,20,35))
    
        -- 用新建的 block type 绘制
        scene.voxels:fill("myGrass")
        scene.voxels:block(vec3(40,20,35))
    
        -- How to write the block update here???
    
        craft.scene.main = scene
    end
    
    
    -- 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
            self:schedule(60)
        end
    
        function Blinky:blockUpdate(ticks)
            local randomColor = color(math.random(128,255), math.random(128,255), math.random(128,255))
            self:set(COLOR, randomColor)
            self:schedule(10)
        end  
    end
    
  • JohnJohn Admin Mod
    Posts: 787

    @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

  • Posts: 298

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

  • Posts: 1,826

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

  • dave1707dave1707 Mod
    Posts: 10,049

    @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.

  • edited June 3 Posts: 1,826

    @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.

  • dave1707dave1707 Mod
    Posts: 10,049

    @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.

  • Posts: 1,826

    @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.

  • dave1707dave1707 Mod
    Posts: 10,049

    @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.

  • Posts: 1,826

    @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.

  • Posts: 323

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

Sign In or Register to comment.