Adding a random seed from an external js

forum discussion on adding a seed random

Could you give an specific example on adding the js for the seed random JS script in a character in the game. Seems that the code add the JS to the camera entity. but is need it only in a character entity

Hi @lukethor and welcome,

What do you mean with a random seed?

The topic you link isn’t relevant to that, discusses loading models dynamically.

1 Like

Math.random() which doesn’t have the API to seed. I need to add an external js to be able to seed the random function

There is this external JS library that can do pseudo random (seeded) random generation:

Grab the seedrandom.min.js script from their repo and upload it to your project. From there study their README on how to get started using it in your scripts.

1 Like

Thank you

var Rotate = pc.createScript('rotate');

/** Attributes */

Rotate.attributes.add('animEntity', { type: 'entity' });
Rotate.attributes.add('power', { type: 'number', default: 450000 });
Rotate.attributes.add('bounceforce', { type: 'number', default: 800 });
Rotate.attributes.add('torquevalue', { type: 'number', default: 8 });

/** Lifecycle Methods */

/**
 * Initialize - setup internal state, resocxzczxurces, listeners.
 */

Rotate.prototype.initialize = function() {


      this.app.assets.loadFromUrl('//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js', 'script', function (err, asset) {
        this.entity.assets.add.script('script');
         this.entity.script.createScript('seedrandom');
    });

Getting the following error:

Cannot read properties of undefined (reading ‘assets’)

[rotate.js?id=120237371&branchId=abbcdf3c-57c4-4539-a5da-58cb4738ede3:20]: Uncaught TypeError: Cannot read properties of undefined (reading ‘assets’)

Here is what worked for me:

  1. Create app.js in the root asset
  2. added the following script to the UI manager entity
  3. Troubleshoot in the browser console to make sure the external JS was working
new Math.seedrandom()
ƒ e(){for(var n=u.g(6),t=d,r=0;n<g;)n=(n+r)*l,t*=l,r=u.g(1);for(;y<=n;)n/=2,t/=2,r>>>=1;return(n+r)/t}
var App = pc.createScript('app');

// initialize code called once per entity
App.prototype.initialize = function() {
  
  
  var script = document.createElement('script');
   script.onload = function() {
      alert("Script loaded and ready");
      
    };
    script.src = "//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js";
    document.getElementsByTagName('head')[0].appendChild(script);

};

// update code called every frame
App.prototype.update = function(dt) {

};

// swap method called for script hot-reloading
// inherit your script state here
// App.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/

Screen shot of reasonably unpredicted seed time:

If you save this JS file https://cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js

And upload it to the Assets in the Editor: https://cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js

Change it to be Loading type ‘After Engine’

That will be enough to add the library to your project without having to add it directly to the document with code.

1 Like

Works like a charm! Less invasive codewise solution.

Thanks