Add a item(Voxel) just using code

Is it possible to create/add a currently designed voxel to the world using just code?

2 Likes

You can modify VoxelMeshs and also spawn prefabs/templates, yes.

Have a look at World::Spawn()

3 Likes

Thanks for the help :+1:

2 Likes

For adding voxels directly we also provide:

voxelComponent:SetVoxel(Vector pos, VoxelAsset voxelAsset)

Change a single voxel in world space.

Plus there’s SetVoxelBox(), and SetVoxelSphere()

1 Like

Thanks for this, i’m slightly confused what to put as Template TemplateAsset. I have a Template(named Block) with a mesh inside (airVentStraight). However that doesn’t work.
I can use a properties name to make it work but I’m just interested to know what the actual parameters are

2 Likes

Sorry for the delay replying.

So in my code I have a property of type ‘voxelasset’

{name = "voxelType", type = "voxelasset"},

And then set voxelType to be a voxel of my choosing, and then call:

self:GetEntity():SetVoxelBox(position, Vector.New(75,75,75), self.properties.voxelType)

Which draws a 3x3x3 cube of that type. Does that help?

3 Likes

What you’ll want to do is add a property to your script with type “template”. Any entity that has that script attached to it will then show your that property in its inspector on the right side, probably somewhere down the bottom. You can use it to choose your template, it will even offer a handy dropdown with all the templates in your game…

Now you can use refer to this property when calling World:Spawn() like this:

GetWorld():Spawn(self.properties.theNameOfYourAwesomeProperty, someVector, someRotation)

I hope that answers your question.

3 Likes

Thanks guys for all the help. I managed to get this working all by myself in the end. I was shocked too lol

2 Likes