fenomas / noa

Experimental voxel game engine.
MIT License
611 stars 87 forks source link

Is there a block update function? #81

Closed EliteAsian123 closed 5 years ago

EliteAsian123 commented 5 years ago

Is there a block update function? So every tick, each block calls a function or something similar. In this case I want to make my cactus grow every once in a while.

fenomas commented 5 years ago

Hi - the way I do this is to use custom block events to track when cactus blocks are added to or removed from the world, and then run a tick handler to occasionally update existing blocks.

E.g.:

noa.registry.registerBlock( cactusID, {
    // block settings, material, opacity, etc...
    onSet: addCactus,
    onLoad: addCactus,
    onUnset: removeCactus,
    onUnload: removeCactus,
})

function addCactus(x,y,z) {    // remember there's a cactus at (x,y,z)...
function removeCactus(x,y,z) { // forget there's a cactus at (x,y,z)...
noa.on('tick', function() {    // do something with all current cactus blocks...

Block event meanings are explained here, hope that helps.

EliteAsian123 commented 5 years ago

Thanks for the answer! Sorry for asking a lot of questions!

fenomas commented 5 years ago

Not at all, please assume every question is something I've forgotten to document! Thanks!