My script dont place voxels or keep placeing them


#1

Ok, trying to start with some scripting, first back to the basics.

var position  = { x: 130, y: 170, z: 140 };
var count = 0;

function test()
{
    Voxels.setVoxel(position.x, position.y, position.z, 0.5, 255, 0, 0);
    Voxels.setVoxel(position.x, position.y-0.5, position.z, 0.5, 255, 255, 0);
    Voxels.setVoxel(position.x, position.y-1.0, position.z, 0.5, 0, 255, 0);
}

Script.update.connect(test);

Fine, script works i see flashing voxels, thats not good ! The script keeps running for some reason. pressing [x] to remove the script. but it keeps flashing the voxels. waiting a bit longer maby buffer. no change, relogging and the voxels show solid - one color. removeing voxels manual. and changing the code.

var position  = { x: 130, y: 170, z: 140 };
var count = 0;

function test()
{
    Voxels.setVoxel(position.x, position.y, position.z, 0.5, 255, 0, 0);
    Voxels.setVoxel(position.x, position.y-0.5, position.z, 0.5, 255, 255, 0);
    Voxels.setVoxel(position.x, position.y-1.0, position.z, 0.5, 0, 255, 0);
}

Script.update.connect(test);

Script.stop();

Fine, the script get executed and removed from running scripts.
But where are my voxels ? No voxels appear inworld. just like its not executeing the script.
What am doing wrong ? or did we hit some bug ?


#2

Put the Script.stop inside the function, it works I tested it.

var position  = { x: 130, y: 170, z: 140 };
var count = 0;

function test()
{
    Voxels.setVoxel(position.x, position.y, position.z, 0.5, 255, 0, 0);
    Voxels.setVoxel(position.x, position.y-0.5, position.z, 0.5, 255, 255, 0);
    Voxels.setVoxel(position.x, position.y-1.0, position.z, 0.5, 0, 255, 0);

Script.stop();
}

Script.update.connect(test);

#3
var position  = { x: 130, y: 170, z: 140 };
var count = 0;  // <-- what's this for, your not using it...?

// define the function
function test() 
{
    Voxels.setVoxel(position.x, position.y, position.z, 0.5, 255, 0, 0);
    Voxels.setVoxel(position.x, position.y-0.5, position.z, 0.5, 255, 255, 0);
    Voxels.setVoxel(position.x, position.y-1.0, position.z, 0.5, 0, 255, 0);
    Stop.script();
}

// call it once
test(); 

If you want it iterate more than once, you need a loop

var position  = { x: 130, y: 170, z: 140 };
var totalVoxels = 10;
var offset = 0;

// define the function
function test() 
{
	for (i=0; i<totalVoxels; i++)
	{
		Voxels.setVoxel(position.x + offset, position.y, position.z, 0.5, 255, 0, 0);
        	Voxels.setVoxel(position.x + offset, position.y-0.5, position.z, 0.5, 255, 255, 0);
   		Voxels.setVoxel(position.x + offset, position.y-1.0, position.z, 0.5, 0, 255, 0);
    	offset += 0.5;
		Script.stop();
	}
}

// call it once
test(); 

#4

Yiks, offcorse. how did i missed that one. in delphi its the same, lsl i use it to. going to try it.


#5

Well, I started playing around a bit with setting voxels by script. One thing I can say is that the interface renders voxels like a boss. I blew out a 100x100 floor in about 1 second, maybe a tad more. Of course it just a nested loop, but still, the voxels render with quickness, which is good…

// Voxel Floor Generator
// John Laury
// 8/29/14

    var position  = { x: 100, y: 100, z: 100 };
    var totalVoxelsX = 100;
    var totalVoxelsZ = 100;
    var offset = 0.5;

    function floorGenerator() 
    {
		for (i = 0; i < totalVoxelsZ - 1; i++)
		{
			for (j=0; j < totalVoxelsX - 1; j++)
    		{
	       		Voxels.setVoxel(position.x + (j * offset), position.y, position.z + (i * offset), 0.5, 255, 255, 0);
    		}
		}
		Script.stop();
    }

floorGenerator();

#6

Yeah the voxels do place well so long as there is no voxels there already, if you try to place voxels on top of existing voxels then it takes 10 times as long so always create an erase script and erase the voxels before trying again.
An erase script is a duplicate of the build script except every instance of Voxels.setVoxel must be replaced with Voxels.eraseVoxels, and simply run that to cleanly remove each placed voxel without damaging anything else.

@John_Laury I think you may have inverted the Script.stop command into “Stop.script”

@Richardus Please take a look at my GreenGables.js tutorial script at https://gist.github.com/Adrianl3d/cd61f1f4697a81f202c8

This script builds a simple 2 level house, all lines are commented to help people understand the script, also get the removeGreenGables.js for easy removal of the house. Here is the erase script https://gist.github.com/Adrianl3d/b8ddcdcb78c24c101ddd

Also here is a simple floor script that generates a checkered floor, although it is not commented, https://alphas.highfidelity.io/t/fun-and-useful-js-scripts/154/43


#7

@Adrian Thanks for pointing out my boo boo, I fixed it, and yeah, I know same script is already the removal script with the change of just one line. As a matter of fact, if you put it in a script ending function you can make it so that when you close the script it deletes the object on exit, or you could have separate build/un-build scripts.


#8

For small scripts I sometimes put build and remove functions in one script, and uncomment build() or remove() as required,

One trap I fell into and I expect lots of other people to also is building a floor on top of a voxel ground surface, if you set the position so its level with the existing floor, then you have to wait while it changes every single voxel, its a slow process.

Also if you start another voxel build script before all the voxels have finished being placed, it can cause massive lag in the next script.

Sometimes you want the floor to finish at the same level as the existing ground so the solution is to run the floor remove script before you run the build script, then it flies.