My global function is not working

I am having problems with one script were I cant call my functions

(function(global){

    var Module = global = {};
    
pc.math.noise.generateNoiseMap = function(mapWidth, MapHeight, scale) {
    let noiseMap = [mapWidth, mapHeight];
    
    if(scale <= 0){
        scale = 0.0001;
    }
    
    for(y = 0; y < mapHeight; y++) {
        for(x = 0; x < mapWidth; y++){
            let sampleX = x / scale;
            let sampleY = y / scale; 
            
            let perlinValue = pc.math.perlin2(sampleX, sampleY);
            noiseMap [x,y] = perlinValue;
        }
    }
        return noiseMap;   
    };
})();

but when I type pc.math.noise.generateNoiseMap it doesn’t show up as a function. I have another script like this and it works just fine. pc.math.perlin2 is from this other script

1 Like

Are you getting an error similar to ‘Cannot read properties of undefined (reading generateNoiseMap)’?

1 Like

The script depends on pc.math.noise which is maybe not yet initialized, to make it independent from the script order, it could assign that object itself:

if (!pc.math.noise) {
  pc.math.noise = {}
}

Then the rest of the script looks like adopted from C# or something, this doesn’t work:

let noiseMap = [mapWidth, mapHeight];

Nor this:

noiseMap [x,y] = perlinValue;

If you want a normal 2D array, pick any idea from here e.g.: How can I create a two dimensional array in JavaScript? - Stack Overflow

2 Likes

yes that is true some of it is adopted from C++
sorry took so long to reply

no, I am not getting that error but similar. I am getting 'Cannot set properties of undefined(‘setting generateNoiseMap)’

Where have you defined/declared pc.math.noise? If you haven’t done:

pc.math.noise = {}

Somewhere, that is you issue. The code you have is setting a property on an undefined object.

Change it to:

(function(global){

    var Module = global = {};
    pc.math.noise = {};
    pc.math.noise.generateNoiseMap = function(mapWidth, MapHeight, scale) {
    let noiseMap = [mapWidth, mapHeight];
    
    if(scale <= 0){
        scale = 0.0001;
    }
    
    for(y = 0; y < mapHeight; y++) {
        for(x = 0; x < mapWidth; y++){
            let sampleX = x / scale;
            let sampleY = y / scale; 
            
            let perlinValue = pc.math.perlin2(sampleX, sampleY);
            noiseMap [x,y] = perlinValue;
        }
    }
        return noiseMap;   
    };
})();

that works thankyou