Hey,
I want to make events in my game, which just appear for example at Halloween or Christmas.
So if you play the game at the 24th of December, a special event will be triggered.
Is there a possibility to use the date of the OS in the game?
Holiday events
● ARCHIVED · READ-ONLY
-
-
Most probably you should be fine by setting up a variable with the system's month and another one with system's day. From that point it's as simple as using some Conditional Branches to check if an event shall occur.
So you are looking for a js command to set current day and current month as values on two variables.
I know that the command to set a value into a variable should be something like this:
$gameVariables.setValue(var, value);
where
var is the id number of the variable
and
value its value to be set.
So we can create an object named dt that it will be a date object.
var dt = new Date();
This dt has attributes and methods to grab those attributes.
We will make two variables, m and d and grab the attributes Month and Day
by using those methods:
var m = dt.getMonth();
var d = dt.getDay();
Use a Script Event Command (third tab Advanced, on Event Commands) and paste this in it:
var dt = new Date();
var m = dt.getMonth();
var d = dt.getDay();
$gameVariables.setValue(1, m);
$gameVariables.setValue(2, d);
You are all set. Months are on Variable 1 and Days on Variabe 2.
If you want to set them to different numbers go on and change that 1 and that 2.
Now if you want you can use an Event Command Show Message to see if it works:
It must contain this:
\V[1] \V[2]
It will show the contents of the variable 1 along with the contents of variable 2.
You can now use Variables 1 and 2 to Conditional Branches to check for special events.
P.S. Don't forget to name the variables. Something like SysMonth, SysDay. -
I don't know why, but my game tells me the wrong date.
By using this, it tells 4th October and not today (8th November). -
You are right. I am not sure what is going on either, it might has to do with the object initialization.
var dt = new Date();
I will research it further.
For now you can do this:
var dt = new Date();
var m = dt.getMonth();
m = m+1;
var d = dt.getDay();
d = d+4;
$gameVariables.setValue(1, m);
$gameVariables.setValue(2, d);
That should fix it. Give me some time to research why it behaves like that.
I think I shall open a new thread on that.
It's either a bug or a... feature.
Nonetheless, try out the code above, it will fix your issue.
Just for the record here is a script that includes the year.
Oh! By the way, the year needs to add 1900 to show the proper number, as shown below:
var dt = new Date();
m = dt.getMonth();
d = dt.getDay();
y = dt.getYear();
m = m+1;
d = d+4;
y = y+1900;
$gameVariables.setValue(1, m);
$gameVariables.setValue(2, d);
$gameVariables.setValue(3, y); -
I don't know why, but my game tells me the wrong date.
By using this, it tells 4th October and not today (8th November).
The month given by the getMonth() function is correct, it's just zero-indexed (like most programming things). So the month given as 10 is actually November.
The month 4 is May, the month 7 is August, etc.
@Dreadshadow the getDay function gets the day of the week (i.e. Thursday), so that is not the correct approach to getting the date :)
You would be better off using Date.getDate()
For Javascript questions, always try the MDN site :)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate -
Oh! Makes sense now. I mean ok the zero indexed case was something I imagined, but the day (0 - 6) didn't passed my mind, thus the approach is wrong.
So it means that
d = dt.getDay();
should be
d = dt.getDate();
and it should work correct, right?
Edit:
P.S. Your links were great! Thanks for sharing this website. I should had imagined that Firefox team would have documentation (since JS started from Netscape)
So let me check if the following works...yes it does!
@EvilDennis replace the previous code with this one below:
var dt = new Date();
m = dt.getMonth();
d = dt.getDate();
y = dt.getFullYear();
m+=1;
$gameVariables.setValue(1, m);
$gameVariables.setValue(2, d);
$gameVariables.setValue(3, y);
Or you can even copy paste THAT code below, because it does the same job with less variables on the script:
var dt = new Date();
$gameVariables.setValue(1, dt.getMonth()+1);
$gameVariables.setValue(2, dt.getDate());
$gameVariables.setValue(3, dt.getFullYear());