-
Notifications
You must be signed in to change notification settings - Fork 84
GDT.addEvent()
Tyler edited this page Mar 20, 2018
·
1 revision
GDT.addEvent
allows you to add events (also referred to as notification's in code) to the game.
Events can be anything from simple popup messages that show at a certain date, to dynamic stories that trigger based on complex logic and have multiple choices and consequences.
You can add an event by calling GDT.addEvent
and passing in a event object.
property (bold means mandatory) | description |
---|---|
id | globally unique id for this event |
isRandomEvent | if set to true then this event will be part of the pool of events that can trigger from time to time (random events happen every 36 + 48 * random() weeks by default) |
date | string in the format of y/m/w referring to a Date- if specified this event will fire on or after this date |
ignoreGameLengthModifier | if set to true, the date specified will not be adjusted based on the game length selected by the player (see Date for details) |
trigger |
function(company) a method that can be used to check custom rules to decide if a event should trigger. this method will be queried repeatedly to see if the event should trigger. if the event is not a random event (isRandomEvent is false) this will be queried every second or so. |
maxTriggers | (int) specifies the maximum number a event can trigger. |
notification or getNotification | property or a method that gives access to the Notification object defining the actual event UI. |
complete |
function(decision) a callback method that will be called when the user decides on an action inside a Notification - Note: for this call to work the Notification needs to specify the sourceId with the same value as the id of the event. |
For an event to work properly the object should either have a date
or a trigger
method defined, otherwise the event will trigger immediately and possibly repeatedly.
Below is a full example of a complex event that tells a story, offers three choices and then tells consequences. It illustrates events and advanced uses of Notifications.
/*
example event:
when does it fire: random event, only in the first office when a game is in development
what happens: neighbours kid spies on game dev, resulting in several options.
*/
var eventId = "F413351E-2108-4967-A989-A7E98D4DEED5";//any string, but needs to be globally unique
var myRandomEvent = {
id: eventId,
isRandomEvent: true, //if you want to test this event, you can set this to false and it will trigger during dev. of the first game.
maxTriggers: 1,
trigger: function (company) {
//only in first office and only while a game is in development.
//most events that fire during game-dev work better if they don't fire right at the start or right at the end, that's why we use isGameProgressBetween
return company.currentLevel == 1 && company.isGameProgressBetween(0.2, 0.9);
},
//because we dynamically create the notification every time the event triggers, we use getNotification
getNotification: function (company) {
var game = company.currentGame;
var msg = "It seems that kids in the neighbourhood have started chatting about your upcoming game {0}. Rumour has it, that Billy, your neighbours kid, snuck into the garage and spied on some of the design papers.{n}How he managed to do this is a mystery. You could swear you were sitting in the garage the entire time!\nHow do you want to react?\n\nYou could talk to the parents to get him punished, ignore the incident or maybe invite some of the neighbours over to show them more of the game."
.localize().format(game.title);
//notice how we break up the story in two screens by using {n}. This creates a better flow than having one longer block of text.
//Also, since this is a story with multiple options and the buttons can only hold a small amount of text, we explain the options beforehand.
//the chatting among kids, creates a bit of hype.
//since the event isn't delayed we can do this directly on the company, otherwise we could call adjustHype() on the notification object to schedule the effect with the notification.
company.adjustHype(5 + 10 * company.getRandom());//increase hype between 5 and 15.
return new Notification({
sourceId: eventId,//this is important, otherwise nothing will happen when the player selects an option.
header: "Billy, the kid".localize(),//granted, this is a silly header.
text: msg,
options: ["Talk to parents", "Ignore incident", "Invite over"]//maximum of three choices
});
},
complete: function (decision) {
//decision is a number and will correspond to the index of the option that was chosen.
//0=talk to parents, 1=ignore incident, 2=invite over
//it's best if every decision has a different outcome
var company = GameManager.company;//we fetch the company object for use later.
if (decision === 0) {//talk to parents
//we create a new, simple notification to tell the outcome. no sourceId or options are necessary this time.
var n = new Notification({
header: "Billy, the kid".localize(),//keep the header consistent with the prior part of the story
text: "You talk to the parents about Billy's actions and they promise it won't happen again."
});
n.adjustHype(5 + 10 * company.getRandom());//increase hype between 5 and 15.
company.activeNotifications.addRange(n.split()); //since this notificaton should be shown immediately (not one second later) we insert it into activeNotifications. calling .split() ist just good practice in case we use {n} inside the notification.
return;
}
if (decision === 1) {//ignore incident
//nothing happens at first, but in a few weeks Billy again breaks in...
var n = new Notification({
header: "Vanished documents".localize(),
text: "You were working on some intricate design documents the other day but now you can't find them anymore. Small foot prints on the floor suggest that someone might have taken them.\nUnfortunately you have to recreate the documents (-500 cr.) - This might have been Billy's work",
weeksUntilFired: 1 + 2 * company.getRandom()
});
n.adjustCash(-500, "restoring documents");
company.notifications.push(n);//this notification isn't shown immediately so we add it to the normal company.notifications array.
return;
}
if (decision === 2) {//invite him over
var n = new Notification({
header: "Billy, the kid".localize(),//keep the header consistent with the prior part of the story
text: "You invite Billy, his parents and a couple of other interested neighbours over and show them the game in-progress. The kids are super-excited and for weeks you hear them talk about it afterwards."
});
n.adjustHype(15 + 25 * company.getRandom());//increase hype between 15 and 40
company.activeNotifications.addRange(n.split()); //since this notificaton should be shown immediately (not one second later) we insert it into activeNotifications. calling .split() ist just good practice in case we use {n} inside the notification.
return;
}
}
};
GDT.addEvent(myRandomEvent);