Post tutorial RSS Coding a Daytime Engine [GML]

A simple daytime engine isnt that hard to code! Basicly, you just let a Variable called seconds count and at the point 59.99 you change it to 0 and set minutes to +1. The same with Hours, Days, etc... Even Day n Night change is in this example. Give it a try (commentary in German :P google translate it yourself) -- domis4

Posted by on - Basic Client Side Coding

first of all, you create a new objekt. Call it "Controller" and set its Depth to -2.
Create Event:

night=0
seconds=0
minutes=0
hours=17
days=0
global.night=1
global.nightalpha=0
addme=1

so everything is now defined, lets go to the Heart of the Time Engine:
Step Event:

//How many seconds a Step you count:
seconds+=10

if seconds >= 59.99     // if seconds at the point 59.99 //or above, it will set a minute +
{
 minutes+=1
 seconds=0
}

if minutes >=59.99      // if minutes at the point 59.99 //or above it will set a hour +
{
 hours+=1
 minutes=0
}

if hours >=23.99        // if hours at the point 23.99 or //above, it count one day + 
{                       
 days+=1
 hours=0
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Attention: its in 24hour format, so from 20 till 6 //o'clock its night
if hours =20 {global.night=1}
if hours =6  {global.night=0}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////that will set the nightalpha for the //darkness
if hours >= 20 && addme=1 {global.nightalpha+=0.01}
if global.nightalpha >= 0.7 {addme=0}

if hours >=4 &amp;&amp; hours <=6 &amp;&amp; addme=0 {global.nightalpha-=0.01}
if global.nightalpha <= 0 {addme=1}

so lets go to draw event:

draw_set_color(c_white)
draw_set_alpha(1)
draw_text(0,0,'seconds: ' +string(seconds))
draw_text(0,16,'minutes: ' +string(minutes))
draw_text(0,32,'hours: ' +string(hours))
draw_text(0,48,'days: ' +string(days))
draw_text(0,62,'nightalpha= ' +string(global.nightalpha))

draw_text(320,240,'Zeit: ' +string(hours) +string(':') +string(minutes) +string(':') +string(seconds))
draw_text(320,256,'Nacht: ' +string(global.night))

//that should you understand ;)

almost finish! now the last thing ist, create a new object and set its Depth to -1
draw event:

draw_set_color(c_black)
draw_set_alpha(global.nightalpha)
draw_rectangle(0,0,640,480,0)

now your day'n night Engine is finish :D pretty simple isnt it?

if you are lasy, then
download the file below. its completely commented so that you understand it. Its written in German, so google translate it yourself xP
(right click -> save target as ...)
right here!!

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.