Notification objects aren't excecuted when picked randomly?

I need help on this one. I’m making a mod, and I have made 10 objects, in this Object format:

var answer1 = {
    id : "mB_a1",
    date : "1/1/1",
    getNotification: function() {
        return new Notification({
            header: header,
            text: "I say that you shouldn't do such a thing. Not worth it. Think of something else.".localize(), //negative
            image: magicBall_FireChaos.modPath + '/images/notifications/ballNegative.png'
        });
    }
}

(Small spoiler)

This object is in an array. A function picks a random object of these 10, to excecute its notification, like this.

picker = function() {
        Math.floor(Math.random() * answers.length);
    }

The problem: Clicking the menu item pops up the UI, and clicking the button to pick a random notification doesn’t work. Any help? I can paste you the code if needed.

I would just declare a variable with a Math.random value and then take a switch statement. If its under 0.10 take the first number in the array, etc.

1 Like

I have tried that, like many solutions. Just the button doesn’t put the notification

Exactly what are you doing with the “picker” function? Currently it does nothing.

Redid it from scratch, your way. Same thing. It doesn’t do anything. I’m gonna update the status.

I asked you, what does the picker function do? It has no return value. If you call it, it wont do shait.

Here’s the whole code, spoiled the whole mod, but I’ll try to make it work;

var answers = [answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer0]
var number = Math.random()
//Picker
picker = function () {
switch (number) {
    case 'answer1':
        if (number <= 0.1) {
            answers[0]
        }
        break;
    case 'answer2':
        if (number <= 0.2) {
            answers[1]
        }
        break;
    case 'answer3':
        if (number <= 0.3) {
            answers[2]
        }
        break;
    case 'answer4':
        if (number <= 0.4) {
            answers[3]
        }
        break;
    case 'answer5':
        if (number <= 0.5) {
            answers[4]
        }
        break;
    case 'answer6':
        if (number <= 0.6) {
            answers[5]
        }
        break;
    case 'answer7':
        if (number <= 0.7) {
            answers[6]
        }
        break;
    case 'answer8':
        if (number <= 0.8) {
            answers[7]
        }
        break;
    case 'answer9':
        if (number <= 0.9) {
            answers[8]
        }
        break;
    case 'answer0':
        if (number <= 0.999999999999999) {
            answers[9]
        }
        break;
    }
}
    //Notifications
var header = "Magic 8 Ball".localize("heading")
var answer1 = {
    id : "mb_a1",
    getNotification: function() {
        return new Notification({
            header: header,
            text: "I say that you shouldn't do such a thing. Not worth it. Think of something else.".localize(), //negative
            image: magicBall_FireChaos.modPath + '/images/notifications/ballNegative.png'
        });
    }
}
var answer2 = {
    id : "mb_a2",
    getNotification: function() {
        return new Notification({
            header: header,
            text: "Hmmm, what if you rethink that idea more. It sounds good, but it needs more thinking.".localize(), //maybe
            image: magicBall_FireChaos.modPath + '/images/notifications/ballMaybe.png'
        });
    }
}
 var answer3 = {
    id : "mb_a3",
    getNotification: function() {
        return new Notification({
            header: header,
            text: "No way! A bad idea, don't do it. If you do, it will have a bad affection to your company.".localize(), //negative
            image: magicBall_FireChaos.modPath + '/images/notifications/ballNegative.png'
        });
    }
}
var answer4 = {
    id : "mb_a4",
    getNotification: function() {
        return new Notification({
            header: header,
            text: "The best idea you've ever had! I'm training you so well! Do it. I'll give you a cookie after".localize(), //positive
            image: magicBall_FireChaos.modPath + '/images/notifications/ballPositive.png'
        });
    }
}
var answer5 = {
    id : "mb_a5",
    getNotification: function() {
        return new Notification({
            header: header,
            text: "Do it. Seems an okay idea to me. What can go wrong! Start doing it right now.".localize(), //positive
            image: magicBall_FireChaos.modPath + '/images/notifications/ballPositive.png'
        });
    }
}
var answer6 = {
    id : "mb_a6",
    getNotification: function() {
        return new Notification({
            header: header,
            text: "Look, I'm not sure about this idea. It's complicated boss. I wonder what goes wrong...".localize(), //maybe
            image: magicBall_FireChaos.modPath + '/images/notifications/ballMaybe.png'
        });
    }
}
var answer7 = {
    id : "mb_a7",
    getNotification: function() {
        return new Notification({
            header: header,
            text: "Just no. That's just bad. Nope, nope, nope. Skip this idea now. Do something else instead.".localize(), // negative
            image: magicBall_FireChaos.modPath + '/images/notifications/ballNegative.png'
        });
    }
}
var answer8 = {
    id : "mb_a8",
    getNotification: function() {
        return new Notification({
            header: header,
            text: "Definitely. Do what you're thinking. I've taught you well. Just don't end up doing it wrong.".localize(), //positive
            image: magicBall_FireChaos.modPath + '/images/notifications/ballPositive.png'
        });
    }
}
var answer9 = {
    id : "mb_a9",
    getNotification: function() {
        return new Notification({
            header: header,
            text: "I'm confused. You better recall me again later. Your ideas can be sometimes confusing.".localize(), //maybe
            image: magicBall_FireChaos.modPath + '/images/notifications/ballMaybe.png'
        });
    }
}
var answer0 = {
    id : "mb_a0",
    getNotification: function() {
        return new Notification({
            header: header,
            text: "Yeah. Whatever. Choose yourself this time. I'm bored of you. I'd better go and get an upgrade this time.".localize(), //maybe
            image: magicBall_FireChaos.modPath + '/images/notifications/ballMaybe.png'
        });
    }
}
//Event adders:
GDT.addEvent(answer0)
GDT.addEvent(answer1)
GDT.addEvent(answer2)
GDT.addEvent(answer3)
GDT.addEvent(answer4)
GDT.addEvent(answer5)
GDT.addEvent(answer6)
GDT.addEvent(answer7)
GDT.addEvent(answer8)
GDT.addEvent(answer9)

//Menu item 
var menu = UI._showContextMenu;
var showMenu = function(type, menuItems, x, y) {
    menuItems.push({
        label: "Magic 8 Ball".localize("menu item"),
        action: function() {
            Sound.click();
            createWindow();
            GameManager.resume(true);
            var div = $("#magicBall");
            div.scrollTop();
            div.gdDialog({
                popout: !0,
                close: !0
            });
        }
    })
    menu(type, menuItems, x, y);
}
UI._showContextMenu = showMenu;

//Button Effect
UI.magicBall = function(company) {
    Sound.click();
    switch (company.id) {
        case "magicButton":
            picker();
            break;
        default:
            return;
    }

}
createWindow = function() {
    //Main Window - Uses HTML
    var div = $("body");
    div.append('<div id="magicBall" class="windowBorder tallWindow" style="overflow:auto;display:none;"> <div id="magicBallDivTitle" class="windowTitle smallerWindowTitle">Magic 8 Ball</div><p>Think a question, and click the "Shake Me" button. I hope you are being as descriptive as you can</div>');
    div = $("#magicBall");

    //Buttons
    div.append('<div id="magicButton" class="selectorButton orangeButton" centeredButtonWrapper onclick="UI.magicBall(this)" style="margin-left:50px;width: 250px">Shake Me</div>');
}

(It doesn’t matter if you spoil the mod, eveyone will see the code if they want anyway)

2 Likes

Well? Ideas?

Nah, you should probably just play around with it. In fact, using time to learn what you did wrong makes you learn a lot!

2 Likes

Just tell me I did a stupid error. Like always :P. Don’t tell me the error.

there is already a built-in .pickRandom() function. you can use it on any array :wink:

The good thing about this is that it uses the games RNG (random number generator) so it’s cheat-safe. By that I mean reloading a save will result in the same ‘random’ selection so you can’t just reload if you don’t like the result.

For future reference, if you do want to just select a random index of an array (that doesn’t use the games RNG though):

var arr = [];//your data here
var index = Math.min(arr.length - 1, Math.floor(Math.random() * arr.length));

Question, if I may, @PatrickKlug

var answers = [answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer0]  

My array of the objects. These are notifications

var answerpicker = answers.pickRandom()

Picks a random object (notification) in that array.

EDIT: Can this be different everytime? When you click a button or a menu item, you’ll get a new random object every time with this option.

No matter how much I try, it doesn’t pop-up a random notification from the array.

I give up.

In your code I think you are using wrong the switch. You are using as a case ‘answer1’,etc. But “number” should be a number, no?

And when you call the picker function you don’t send any parameter as number.

You should make clear what is the purpose of the “picker” function.

  1. Does it return a random index of a provided array? (The array would be the argument, the index would be returned).
  2. Does it return a random element from a provided array? (The element would be returned, not the index)
  3. Does it choose a random element from the array and execute its getNotification method? (The Notification would be returned)

Also take in consideration that in your example you are creating the array before declaring the variables.

You should stick to the way you were getting the random index of an array as it its simpler and works ok, or you can you the one @PatrickKlug suggested (I can’t think any reason of why is the Math.min needed).
If you keep your code short and clean it would be much easier to debug it.

1 Like

Thanks for the reply. Will try a few more methods.

arr.pickRandom() will just return a random item from the given array. If you call the method again it will give you another random item.

Picking the item shouldn’t be the problem but how do you try to show it?

You should push it via
GameManager.company.notifications.push(n);
(see https://github.com/greenheartgames/gdt-modAPI/wiki/Notification)

@Kimmberly you’re right. Math.min is redundant:var index = Math.floor(Math.random()*arr.length)); is the canonical version.