Discord
Login
Community
DARK THEME

Can't access random from JavaScript

In JavaScript, any attempt to access random functions fails. Which parts of microStudio's "standard library" are available from JavaScript? Everything in API but nothing in microScript programming? I couldn't find where this was documented.

PS I am loving microStudio. Thank you!

Pretty much you need to use pure javascript, so the Math.random() is what you would use instead. Here is a bit of code I wrote in Javascript within microStudio which may help to give you a general idea of the structure:

https://microstudio.dev/i/JimB007/hypno/

init = function(){
  w=0, h=0, r=0, g=0, b=0;
  rdir=10, gdir=20, bdir=30,
  clist=[];
  
  for (let i=0;i<332;i+=10){
    clist.push(getNextRGB());
  }
}

update = function(){
  w=430; h=290; 
  clist.shift();
  clist.push(getNextRGB());
}

draw = function(){
  clist.forEach((col) => {
    w-=12; h-=8;
    screen.fillRound(0,0,w,h,col);
  });
}

getNextRGB=function(){
  r+=rdir; g+=gdir; b+=bdir;
  if (r>255) {rdir=-rndRange(); r=255;}
  if (r<0) {rdir=rndRange(); r=0;}
  if (g>255) {gdir=-rndRange(); g=255;}
  if (g<0) {gdir=rndRange(); g=0;}
  if (b>255) {bdir=-rndRange(); b=255;}
  if (b<0) {bdir=rndRange(); b=0;}
  return "rgb("+r+","+g+","+b+")";
}

rndRange=function(){
  return Math.floor(Math.random(28)+4);
}

Thank you. This is what I ended up making: https://microstudio.dev/i/ygingold/katamaridemo/

All the functions in https://microstudio.dev/documentation/ under "API reference" seem to be available. Just not the ones under "microScript Programming". A note to this effect somewhere could help others. Perhaps a sentence like to that effect as a note next to the Settings > Advanced Options > Language Support pop-up menu.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community