#Randomized permutations
1 messages · Page 1 of 1 (latest)
// random numbers [0,1,2,3]
const randomNumber = Math.floor(Math.random()*4);
// random boolean [false, true]
const randomBoolean = Math.random() < 0.5 ? true : false;
// random strings
const values = ["a", "b", "c"]
const randomString = values[Math.floor(Math.random()*values.length)];
// Then change the permutation of the block with the value obtained
const state = block.permutation.withState("example:state", randomNumber);
block.setPermutation(state);
You can use this in your custom component
How would I set up the custom component?
import { world } from '@minecraft/server';
// Register your component with an identifier
world.beforeEvents.worldInitialize.subscribe(initEvent => {
initEvent.blockComponentRegistry.registerCustomComponent('example:identifier', {
// Choose which event you will use
onPlace: e => {
// Get the block
const { block } = e;
// Get a random value between 0 and 3
const randomNumber = Math.floor(Math.random()*4);
// Set the block state based on this value
const state = block.permutation.withState("example:state", randomNumber);
block.setPermutation(state);
}
});
});
Then add the custom component to your block's json file using the identifier you chose.
"minecraft:custom_components": [
"example:identifier"
]
Sorry for late response but it isnt working
Can you send the code?
Script or block?
script
// Register your component with an identifier
world.beforeEvents.worldInitialize.subscribe(initEvent => {
initEvent.blockComponentRegistry.registerCustomComponent('myth:beer_random', {
// Choose which event you will use
onPlace: e => {
// Get the block
const { block } = e;
// Get a random value between 0 and 3
const randomNumber = Math.floor(Math.random() * 2);
// Set the block state based on this value
const state = block.permutation.withState("myth:beer_variant", randomNumber);
block.setPermutation(state);
}
});
});```
Can you send the block too?
The code seems correct, if there was an error setting the permutation it may be that it does not exist in the block or the value is not accepted.
You have not set the block state
And in your script you used Math.floor(Math.random()*2), this gives you a random number between 0 and 2 but without including 2, use Math.floor(Math.random()*3) instead
What does this mean?
Sorry the file had an error, try this one
It works, thanks!
@slate turret how this works can I use it on custom logs
maybe, this is for random states