Day/Night and Clear Lights with Clock

Day/Night and Clear Lights with Clock
version: 2.0
maker built for: RMXP
by: Brewmeister, Gubid, and Star


Description:

Day/Night system that goes from light to dark depending on the time of day. Clear lights that open up the darkness on the map instead of layering over darkness to make it look foggy, making it easier to place events as light sources. You are also able to use your own graphic and with further updates I will have different graphics for different lights. An onscreen clock appears and shows you the current time of day as well. Lag free with default scripts, tested with some but could cause some compatibility issues. Updated versions will include efforts to make compatible with most scripts.

Features:

  • Gradual darkness and brightness change (won't even see it happen)
  • Setting the speed for how long a day lasts (Normal time is set at 24 earth minutes = 24 game hours
  • Switches to make screen dark for caves and screens darker for houses
  • Setting the clock visible or invisible at start-up and by switch
  • Setting the opacity of caves
  • Setting the extra opacity of houses
  • Starting hour and day
  • Current hour and day recorded in variables
  • Stopping the flow of time by starting and stopping the timer in event
  • Restarting the timer at any time will start the timer at the time you left it off at.
  • Lights are checked by putting light100 as a comment in an event
  • Ability to change between standard and military time
  • Onscreen images that show moon or sun

Screenshots:

Screen Show
Image


Demo:



Script:

Incase the demo is down.

Content Hidden Show
  1.  
  2. =begin
  3. ---------------------------------------------
  4. DAY_NIGHT_LIGHT_SYSTEM
  5. ---------------------------------------------
  6. by
  7. - Brewmeister
  8. - Gubid
  9. - Star
  10. ---------------------------------------------
  11. =end
  12.  
  13.  
  14. #-----------------------------------
  15. # Editing of the Script
  16. #-----------------------------------
  17.  
  18.  
  19. # How many real time minutes does it take for a whole gameday to go by?
  20. # Normal time would be 24 minutes so a minute in game goes by every second.
  21. #-----------------------------------
  22. GAME_DAY_IN_REAL_MINUTES = 24
  23.  
  24.  
  25. # Current_Game_Day is the variable of the current day
  26. #-----------------------------------
  27. CURRENT_GAME_DAY = 15
  28.  
  29. # Day you want Game_Day to start at.
  30. #-----------------------------------
  31. INTIALIZE_GAME_DAY = 1
  32.  
  33.  
  34. # If Flickercolon is true the : in the 00:00 flashes every second
  35. #-----------------------------------
  36. FLICKERCOLON = true
  37.  
  38. # Hour_variable is the variable of the current hour
  39. #-----------------------------------
  40. HOUR_VARIABLE = 9
  41.  
  42. # Inside_Switch is the switch set to make insides darker, not meant for caves
  43. #-----------------------------------
  44. INSIDE_SWITCH = 9
  45.  
  46. # Inside_Opacity is the opacity set to increase when entering insides
  47. #-----------------------------------
  48. INSIDE_OPACITY = 10
  49.  
  50. # Cave_Switch is the switch set meant for caves
  51. #-----------------------------------
  52. CAVE_SWITCH = 11
  53.  
  54. # Cave_Opacity is the total opacity you want when entering caves.  Generally
  55. # Should be set higher than the highest darkness.
  56. #-----------------------------------
  57. CAVE_OPACITY = 220
  58.  
  59. # if AM_AND_PM is true, everything is not in military time
  60. #-----------------------------------
  61. AM_AND_PM = true
  62.  
  63. # Starthour is the hour the game starts at
  64. #-----------------------------------
  65. STARTHOUR = 2
  66.  
  67. # New_hour is the variable that changes the current hour.  You must use this in
  68. # order to change the hour.  It will not tell you the current hour, only
  69. # Hour_variable can tell the current hour.
  70. #-----------------------------------
  71. NEW_HOUR = 10
  72.  
  73. # Set this switch to on in order to hide the clock
  74. #-----------------------------------
  75. INVISIBLE_TIMER_SWITCH = 10
  76.  
  77.  
  78.  
  79. #---------------------------------------------
  80. #Bitmap
  81. #---------------------------------------------
  82. class Bitmap
  83.   def sub(ox,oy,light)
  84.     ox -= (light.width / 2)
  85.     oy -= (light.height / 2)
  86.     for x in 0...light.width
  87.       for y in 0...light.height
  88.         lt_color = light.get_pixel(x,y)
  89.         ds_color = self.get_pixel(x + ox,y + oy)
  90.         ds_color.alpha -= lt_color.alpha
  91.         self.set_pixel(x + ox,y + oy,ds_color)
  92.       end
  93.     end
  94.   end
  95. end
  96.  
  97. #---------------------------------------------
  98. #end Bitmap
  99. #---------------------------------------------
  100.  
  101.  
  102. #---------------------------------------------
  103. #Game_System
  104. #---------------------------------------------
  105. class Game_System  
  106.   alias init_gme_sys initialize
  107.   def initialize
  108.     init_gme_sys
  109.     @timer_working = true
  110.     @timer_remember = 0
  111.     @timer = STARTHOUR * 2400
  112.   end
  113. def update
  114.     # reduce timer by 1
  115.     if @timer_working
  116.       if @timer > 57600
  117.         @timer = 0
  118.          $game_variables[CURRENT_GAME_DAY] += 1
  119.       else
  120.           if @timer_remember != 0 and @timer != @timer_remember
  121.           @timer = @timer_remember
  122.           @timer_remember = 0
  123.           else
  124.           @timer += (1 * (24/GAME_DAY_IN_REAL_MINUTES))
  125.           end
  126.       end
  127.     elsif @timer_remember != @timer and @timer_working == false
  128.       @timer_remember = @timer
  129.     end
  130.   end
  131. end
  132.  
  133. #---------------------------------------------
  134. #end Game_System
  135. #---------------------------------------------
  136.  
  137.  
  138.  
  139.  
  140. #---------------------------------------------
  141. #Sprite_Timer
  142. #---------------------------------------------
  143. class Sprite_Timer < Sprite
  144.   def initialize
  145.     super
  146.     self.bitmap = Bitmap.new(120, 20)
  147.     self.bitmap.font.name = "Arial"
  148.     self.bitmap.font.size = 15
  149.     self.x = 620 - self.bitmap.width
  150.     self.y = 0
  151.     self.z = 2000
  152.     @flicker = true
  153.     if $game_variables[CURRENT_GAME_DAY] < INTIALIZE_GAME_DAY
  154.       $game_variables[CURRENT_GAME_DAY] = INTIALIZE_GAME_DAY
  155.       end
  156.     update
  157.   end
  158.   def update
  159.     super
  160.     # Set timer to visible if working
  161.     if $game_switches[INVISIBLE_TIMER_SWITCH] == true
  162.     self.visible = false
  163.   else
  164.     self.visible = true
  165.     end
  166.         if $game_variables[NEW_HOUR] != 0 and
  167.            $game_variables[NEW_HOUR] != $game_variables[HOUR_VARIABLE]
  168.       $game_system.timer = $game_variables[NEW_HOUR] * 2400
  169.       if  $game_system.timer > 57600
  170.         $game_system.timer % 57600
  171.         $game_variables[CURRENT_GAME_DAY] += 1
  172.       end
  173.       $game_variables[NEW_HOUR] = 0
  174.       end
  175.     # If timer needs to be redrawn
  176.       if $game_system.timer / Graphics.frame_rate != @total_sec and $game_system.timer_working
  177.       # Clear window contents
  178.       self.bitmap.clear
  179.       # Calculate total number of seconds
  180.       @total_sec = $game_system.timer / Graphics.frame_rate
  181.       # Make a string for displaying the timer
  182.       case $game_variables[HOUR_VARIABLE]
  183.       when 0,1,2,3,4,5,18,19,20,21,22,23,24
  184.       moon_and_sun = RPG::Cache.picture("Moon")
  185.       cw = moon_and_sun.width
  186.       ch = moon_and_sun.height
  187.       rect = Rect.new(0, 0, cw, ch)
  188.       when 6,7,8,9,10,11,12,13,14,15,16,17
  189.       moon_and_sun = RPG::Cache.picture("Sun")
  190.       cw = moon_and_sun.width
  191.       ch = moon_and_sun.height
  192.       rect = Rect.new(0, 0, cw, ch)
  193.     end
  194.       if $game_variables[HOUR_VARIABLE] > 24
  195.       moon_and_sun = RPG::Cache.picture("Moon")
  196.       cw = moon_and_sun.width
  197.       ch = moon_and_sun.height
  198.       rect = Rect.new(0, 0, cw, ch)
  199.       end
  200.       if AM_AND_PM == true
  201.         if $game_variables[HOUR_VARIABLE] > 12
  202.           min = (@total_sec - 720) / 60
  203.           sec = @total_sec % 60
  204.         else
  205.           min = @total_sec / 60
  206.           sec = @total_sec % 60
  207.         end
  208.             if $game_variables[HOUR_VARIABLE] > 11 and $game_variables[HOUR_VARIABLE] != 24
  209.               setting = "PM"
  210.             else
  211.               setting = "AM"
  212.             end
  213.             if FLICKERCOLON == true and @flicker == false
  214.             text = sprintf("%02d %02d " + setting, min, sec)
  215.              @flicker = true
  216.           else
  217.             text = sprintf("%02d:%02d " + setting, min, sec)
  218.             if FLICKERCOLON == true
  219.               @flicker = false
  220.             end
  221.           end
  222.       else
  223.         min = @total_sec / 60
  224.         sec = @total_sec % 60
  225.         text = sprintf("%02d:%02d", min, sec)
  226.         end
  227.       if $game_variables[HOUR_VARIABLE] > 12 and AM_AND_PM == true
  228.       $game_variables[HOUR_VARIABLE] = min + 12
  229.       else
  230.       $game_variables[HOUR_VARIABLE] = min
  231.       end
  232.       $sec = sec
  233.       # Draw timer
  234.       rect2 = Rect.new(1, 1, 120, 20)
  235.       self.bitmap.font.color.set(0, 0, 0)
  236.       self.bitmap.draw_text(rect2, text, 1)
  237.       self.bitmap.font.color.set(255, 255, 255)
  238.       self.bitmap.draw_text(self.bitmap.rect, text, 1)
  239.       self.bitmap.blt(90, 0, moon_and_sun, rect)
  240.       end
  241.   end
  242. end
  243.  
  244. #---------------------------------------------
  245. #end Sprite_Timer
  246. #---------------------------------------------
  247.  
  248. #---------------------------------------------
  249. #Game_Event
  250. #---------------------------------------------
  251. class Game_Event < Game_Character
  252.   attr_reader :erased
  253.   attr_accessor :is_light
  254.   alias init_gm_evnt_lights initialize
  255.   def initialize(*args)
  256.     init_gm_evnt_lights(*args)
  257.     @is_light = false
  258.   end
  259.   alias refresh_gm_evnt_lights refresh
  260.   def refresh
  261.     tmp_page = @page
  262.     refresh_gm_evnt_lights
  263.     if tmp_page != @page
  264.       @is_light = false
  265.     end
  266.   end
  267. end
  268.  
  269. #---------------------------------------------
  270. #end Game_Event
  271. #---------------------------------------------
  272.  
  273. #---------------------------------------------
  274. #Sprite_Nighttime
  275. #---------------------------------------------
  276.  
  277. class Nighttime < Sprite
  278.   def initialize(x,y)
  279.     super()
  280.     @width = $game_map.width * 32
  281.     @height = $game_map.height * 32
  282.     @lights = {}
  283.     self.x = x
  284.     self.y = y
  285.     self.bitmap = Bitmap.new(@width, @height)
  286.     self.bitmap.clear
  287.     fillnight
  288.   end
  289.   def refresh
  290.     self.bitmap.clear
  291.     create_lights
  292.     fillnight
  293.   end
  294.  
  295.   def create_lights
  296.     for event in $game_map.events.values
  297.       unless event.erased or event.list == nil
  298.       for i in 0...event.list.size
  299.         if event.list[i].code == 108 #comment
  300.           if event.erased and event.list[i].parameters[0].include?("light")
  301.             @lights.delete(event)
  302.             next
  303.           elsif event.list[i].parameters[0].include?("light")
  304.             size = event.list[i].parameters[0].delete("light").to_i #convert text to interger
  305.             @lights[event] = Light.new(event, size) unless @lights.keys.include?(event)
  306.             event.is_light = true
  307.             next
  308.           end
  309.         end
  310.       end
  311.       #in the event page is changed and it is no longer a light.. remove it.
  312.       if @lights[event] != nil and event.is_light == false
  313.         @lights.delete(event)
  314.        end
  315.     end
  316.   end
  317. end
  318.   def fillnight
  319.     if $sec == nil
  320.       $sec = 0
  321.     end
  322.     if $game_switches[INSIDE_SWITCH] == true
  323.       extralpha = INSIDE_OPACITY
  324.     else
  325.       extralpha = 0
  326.       end
  327.      case $game_variables[HOUR_VARIABLE]
  328.     when 0, 1, 23
  329.     startop = 210 - extralpha
  330.     when 2
  331.     startop = 210 - ($sec / 2) + extralpha
  332.     when 3
  333.     startop = 180 - ($sec / 2) + extralpha
  334.     when 4
  335.     startop = 150 - ($sec / 2) + extralpha
  336.     when 5
  337.     startop = 120 - ($sec / 2) + extralpha
  338.     when 6
  339.     startop = 90 - ($sec / 2) + extralpha
  340.     when 7
  341.     startop = 60 - ($sec / 2) + extralpha
  342.     when 8
  343.     startop = 30 - ($sec / 2) + extralpha
  344.     when 9, 10, 11, 12, 13, 14, 15
  345.     startop = 0 + extralpha
  346.     when 16
  347.     startop = 0 + ($sec / 2) + extralpha
  348.     when 17
  349.     startop = 30 + ($sec / 2) + extralpha
  350.     when 18
  351.     startop = 60 + ($sec / 2) + extralpha
  352.     when 19
  353.     startop = 90 + ($sec / 2) + extralpha
  354.     when 20
  355.     startop = 120 + ($sec / 2) + extralpha
  356.     when 21
  357.     startop = 150 + ($sec / 2) + extralpha
  358.     when 22
  359.     startop = 180 + ($sec / 2) + extralpha
  360.   end
  361.   if $game_switches[CAVE_SWITCH] == true
  362.     startop = CAVE_OPACITY
  363.     end
  364.    
  365.     self.bitmap.fill_rect(0, 0, @width, @height, Color.new(0,0,0,255))
  366.     self.opacity = startop
  367.     # Now subtract out the lights
  368.     # Testing with a single light at tile 7,11
  369.     apply_lights
  370.   end
  371.   def apply_lights
  372.     applied_space = []
  373.     for light in @lights.values
  374.       image_sub = RPG::Cache.picture("LightSUB").clone
  375.       image_blt = RPG::Cache.picture("LightBLT").clone
  376.       base_rect = image_sub.rect
  377.       overlapLights = []
  378.       size = (light.size/100.0)
  379.       lwidth = base_rect.width*size
  380.       light_rect = Rect.new(light.x-(lwidth/2), light.y-(lwidth/2), lwidth, lwidth)
  381.       for applied_rect in applied_space
  382.         if light_rect.overlap?(applied_rect)
  383.           overlapLights << light_rect.area_in_overlap(applied_rect)
  384.         end
  385.       end
  386.       #now draw it
  387.       if overlapLights.size == 0 #no overlap detected.. then just BLT it into the image
  388.         self.bitmap.fill_rect(light_rect, Color.new(0,0,0,0))
  389.         self.bitmap.stretch_blt(light_rect, image_blt, base_rect)
  390.       else
  391.         self.bitmap.sub(light.x,light.y,image_sub)
  392.       end
  393.       applied_space << light_rect
  394.       image_sub.dispose
  395.       image_blt.dispose
  396.     end
  397.   end
  398.   def update
  399.     if $game_switches[INSIDE_SWITCH] == true
  400.       extralpha = INSIDE_OPACITY
  401.     else
  402.       extralpha = 0
  403.       end
  404.     case $game_variables[HOUR_VARIABLE]
  405.     when 0, 1, 23
  406.     self.opacity = 210 - extralpha
  407.     when 2
  408.     self.opacity = 210 - ($sec / 2) + extralpha
  409.     when 3
  410.     self.opacity = 180 - ($sec / 2) + extralpha
  411.     when 4
  412.     self.opacity = 150 - ($sec / 2) + extralpha
  413.     when 5
  414.     self.opacity = 120 - ($sec / 2) + extralpha
  415.     when 6
  416.     self.opacity = 90 - ($sec / 2) + extralpha
  417.     when 7
  418.     self.opacity = 60 - ($sec / 2) + extralpha
  419.     when 8
  420.     self.opacity = 30 - ($sec / 2) + extralpha
  421.     when 9, 10, 11, 12, 13, 14, 15
  422.     self.opacity = 0 + extralpha
  423.     when 16
  424.     self.opacity = 0 + ($sec / 2) + extralpha
  425.     when 17
  426.     self.opacity = 30 + ($sec / 2) + extralpha
  427.     when 18
  428.     self.opacity = 60 + ($sec / 2) + extralpha
  429.     when 19
  430.     self.opacity = 90 + ($sec / 2) + extralpha
  431.     when 20
  432.     self.opacity = 120 + ($sec / 2) + extralpha
  433.     when 21
  434.     self.opacity = 150 + ($sec / 2) + extralpha
  435.     when 22
  436.     self.opacity = 180 + ($sec / 2) + extralpha
  437.     end
  438.       if $game_switches[CAVE_SWITCH] == true
  439.     startop = CAVE_OPACITY
  440.     end
  441.     end
  442. end
  443. class Rect
  444.   def overlap?(rect)
  445.     corners = []
  446.     corners << [rect.x, rect.y] #UL
  447.     corners << [rect.x+rect.width, rect.y] #UR
  448.     corners << [rect.x+rect.width, rect.y+height] #LR
  449.     corners << [rect.x, rect.y+height] #LL
  450.     for corner in corners
  451.       if corner[0].between?(self.x, self.x+width) and corner[1].between?(self.y, self.y+height)
  452.         return true
  453.       end
  454.     end
  455.     return false
  456.   end
  457.   def area_in_overlap(rect)
  458.     return rect
  459.   end
  460. end
  461. class Light
  462.   def initialize(event, size)
  463.     @event = event
  464.     @size = size
  465.   end
  466.   def size
  467.     return @size
  468.   end
  469.   def x
  470.     return @event.real_x/4 + 16
  471.   end
  472.   def y
  473.     return @event.real_y/4 + 16
  474.   end
  475. end
  476.  
  477.  
  478. #---------------------------------------------
  479. #end Sprite_Nighttime
  480. #---------------------------------------------
  481.  
  482. #---------------------------------------------
  483. #Spriteset_Map
  484. #---------------------------------------------
  485. class Spriteset_Map
  486.   alias init_spr_map_lights initialize
  487.   def initialize
  488.     # Make nighttime sprite
  489.     @nighttime = Nighttime.new(0,0)
  490.     @nighttime.z = 2000
  491.     init_spr_map_lights
  492.     @nighttime.refresh
  493.     @map_idd = $game_map.map_id
  494.   end
  495.    alias dns_dispose dispose
  496.    def dispose
  497.      dns_dispose
  498.      @nighttime.dispose
  499.    end
  500.   alias upd_spr_map_lights update
  501.   def update
  502.     upd_spr_map_lights
  503.     # Update nighttime sprite
  504.     @nighttime.ox = $game_map.display_x / 4
  505.     @nighttime.oy = $game_map.display_y / 4
  506.     if @hour_day != $game_variables[HOUR_VARIABLE]
  507.       $game_map.refresh
  508.       @hour_day = $game_variables[HOUR_VARIABLE]
  509.       end
  510.     if @map_idd != $game_map.map_id
  511.       @nighttime.refresh
  512.       @map_idd = $game_map.map_id
  513.       end
  514.     @nighttime.update
  515.   end
  516. end
  517. #---------------------------------------------
  518. #end Spriteset_Map
  519. #---------------------------------------------
Expand to see the code.


Images:

Incase demo is down

Images Show
Image
Image
Image
Image
Image
Image


Instructions:

Place above main

in events with lights, make a comment that says "light" plus the size of the radius of the light. The normal size is 100 for bigger, use bigger numbers, smaller, smaller numbers. For example a normal light is "light100"

Terms and Conditions:

Free to use in any project, please give credit where it is due. Credit goes to Brewmeister, Gubid and Star
You guys finally got it done...it looks amazing! Congrats on completing it! I love it!
Excellent stuff! I especially like the light system, it's just so much cleaner subtracting darkness than overlaying light with brightness. Is there anyway to isolate that feature/stand-alone?

And a comment on scripting: I believe it's a good habit to enclose variables within a class/module of some sort, rather than just hanging loosely in the Interpreter class, no?
OHMYGODITLOOKSFREACKINGINSANE!
Now I can have some good lights in my game, not crappy one!
I told you in the working thread that I like this script a lot, and I'm glad you did make it a seperate thread now that labels it as a script instead of a request topic. Again, glad to see that something this cool came out of a request thread - not too common!

As for the script itself... there's a few things that make me twitch throughout the whole thing. Before that, let me ask you to put your next script in a single tag instead of many, for the sole reason that with neat scripting style, the individual scripts are distinguishable by scrolling through anyway, and looking through it it so much easier - nevermind copypasting it. Also, the [ rgss ] tag is generally cooler, but as it's a submission, that's not the most important thing.
I gotta say once more that I don't really fancy the bunch of constants. Not going into detail that much, but when a variable is only used once, it really shouldn't be a constant.
The scripting style varies a lot throughout the thing... especially as far as commenting goes. That surely is because you worked on it with three people, but yeah... shouldn't be too much hazzle to make it nice and shiney now that you got a final product.
 
 
@regi: I think what you're looking for is this method:
  1. class Bitmap
  2.   def sub(ox,oy,light)
  3.     ox -= (light.width / 2)
  4.     oy -= (light.height / 2)
  5.     for x in 0...light.width
  6.       for y in 0...light.height
  7.         lt_color = light.get_pixel(x,y)
  8.         ds_color = self.get_pixel(x + ox,y + oy)
  9.         ds_color.alpha -= lt_color.alpha
  10.         self.set_pixel(x + ox,y + oy,ds_color)
  11.       end
  12.     end
  13.   end
  14. end
Expand to see the code.
Just so you know, that a lot of that stuff went over my head. I'm just getting used to scripts now and a lot of it is still new to me. This thing came together because three people worked on it. If it were me alone, the lighting wouldn't be there of course. Everything else I handled mostly. That's why it looks like an amateur worked on it. Sadly, I sometimes rule "Things are working now" over "things are working now and it looks clean" because first of all, I don't know how to make it look that way.

I can put everything into one script effortlessly too

I don't understand what you mean by the bunch of constants? Or by variable only being used once.

Also regi, what do you mean by variables hanging loosely, you lost me there.

I'm sure there will be further updates to make it perfect. I still wanna make a weather script in there too
By "bunch of constants", I mean this:
  1. #-----------------------------------
  2. # Editing of the Script
  3. #-----------------------------------
  4.  
  5.  
  6. # How many real time minutes does it take for a whole gameday to go by?
  7. # Normal time would be 24 minutes so a minute in game goes by every second.
  8. #-----------------------------------
  9. GAME_DAY_IN_REAL_MINUTES = 24
  10.  
  11.  
  12. # Current_Game_Day is the variable of the current day
  13. #-----------------------------------
  14. CURRENT_GAME_DAY = 15
  15.  
  16. # Day you want Game_Day to start at.
  17. #-----------------------------------
  18. INTIALIZE_GAME_DAY = 1
  19.  
  20.  
  21. # If Flickercolon is true the : in the 00:00 flashes every second
  22. #-----------------------------------
  23. FLICKERCOLON = true
  24.  
  25. # Hour_variable is the variable of the current hour
  26. #-----------------------------------
  27. HOUR_VARIABLE = 9
  28.  
  29. # Inside_Switch is the switch set to make insides darker, not meant for caves
  30. #-----------------------------------
  31. INSIDE_SWITCH = 9
  32.  
  33. # Inside_Opacity is the opacity set to increase when entering insides
  34. #-----------------------------------
  35. INSIDE_OPACITY = 10
  36.  
  37. # Cave_Switch is the switch set meant for caves
  38. #-----------------------------------
  39. CAVE_SWITCH = 11
  40.  
  41. # Cave_Opacity is the total opacity you want when entering caves.  Generally
  42. # Should be set higher than the highest darkness.
  43. #-----------------------------------
  44. CAVE_OPACITY = 220
  45.  
  46. # if AM_AND_PM is true, everything is not in military time
  47. #-----------------------------------
  48. AM_AND_PM = true
  49.  
  50. # Starthour is the hour the game starts at
  51. #-----------------------------------
  52. STARTHOUR = 2
  53.  
  54. # New_hour is the variable that changes the current hour.  You must use this in
  55. # order to change the hour.  It will not tell you the current hour, only
  56. # Hour_variable can tell the current hour.
  57. #-----------------------------------
  58. NEW_HOUR = 10
  59.  
  60. # Set this switch to on in order to hide the clock
  61. #-----------------------------------
  62. INVISIBLE_TIMER_SWITCH = 10
Expand to see the code.
That is basically a piece of code that - judging it by performance only - is unlimited times inefficient; simply because it doesn't do anything that couldn't be done easier otherwise, and takes additional resources to do that. Now you may argue about practicability, and again, I need to say that I'm the only person in these forums with this opinion, but... I think having the user of the script search for the number they want to change directly in the script will not keep too many people from changing it, and if it does, it's their damn fault for wanting fork-it-over scripts. Worst effect it could have is people actually learn some scripting in the process, and since when is that a bad thing ^^
But yeah, I'm aware of the fact that as reasonable as that may sound, Trick and Seph and whoever all did the constant way, and I'm not quite under the glorified spotlight they had... still, I'm not getting tired of mentioning it :D
My comment was basically the same as BlueScope's. It's great that you're trying something new, but you can always improve! Generally you want to keep your variables in a class or module for organizational purposes. Merging everything in one script might be a good idea, too, since they're not too lengthy.
BlueScope wrote:
That is basically a piece of code that - judging it by performance only - is unlimited times inefficient; simply because it doesn't do anything that couldn't be done easier otherwise, and takes additional resources to do that. Now you may argue about practicability, and again, I need to say that I'm the only person in these forums with this opinion, but... I think having the user of the script search for the number they want to change directly in the script will not keep too many people from changing it, and if it does, it's their damn fault for wanting fork-it-over scripts. Worst effect it could have is people actually learn some scripting in the process, and since when is that a bad thing ^^
But yeah, I'm aware of the fact that as reasonable as that may sound, Trick and Seph and whoever all did the constant way, and I'm not quite under the glorified spotlight they had... still, I'm not getting tired of mentioning it :D

So you're saying that making it easier for people to change how the script works which effectively makes it more marketable and convenient for those not so well trained in intelligence but have a strong desire to make an RM game, is inefficient and forcing people to learn scripts that I don't even halfway understand yet is a good thing?

In my defense, I learned scripting and how to script, a little bit by trial and error and seeing what other people do in their scripts to do things and piece things together to make happen what I want to happen. I don't even know what all the terms mean, sadly. I want to learn more and hopefully I will. To prove my point, I have no idea what Alias does. I tried looking it up today with no justice. My guess is that it allows you to call a method when a similar method is called under the same class without overwriting the previous. If this is true, I can probably make an alias for sprite_timer and create it's own class without overwriting previous ones. (not that people should be using timer scripts with this, that's ridiculous.
It's not really that inefficient, only slightly more than encapsulating the variables in the individual classes. The constants (most of them) get used by multiple classes. Since the main class is going to be instantiated during most of the game, you wouldn't save a whole lot other than occasionally having those variables cleared from memory when the object goes out of scope, only to be re-declared as soon as another map is created.

The benefit of making it easier for a non-scripter, I think, far outweighs the tiny bit of inefficiency.
Don't get me wrong, some of the engineering software I work with is 5-10 million lines of code and efficiency is crucial.

I think it's a little arrogant to take the attitude that if you can understand & navigate the scripts that everyone else should have to.

Trick & Seph used Constants appropriately given the nature of the programs they wrote. They were intended for amateur gamemakers
to be able to install, configure & use quickly & simply so they could concentrate on their game design without having to learn programming.

Essentially, to make it easier. Same reason you asked for all of the scripts in one code tag. 😀

There are a lot of young (and not so young) people just starting out in the field of game-making who have grand visions & are overwhelmed
enough by the amount of work that goes into building a game. I, for one, don't want to be the one to discourage them. Some of them will
eventually learn to program while others never will. They might become experts at graphics, or screenplay, or conceptual design.


@Star, alias is just that, an "Alias" (another name for something)
Examples should be written

class Myclass
alias call_the_original_update update # rename the original update method defined in the default scripts, "call_the_original_update"

def update # redefine "update". Now any other object that calls this classes update method calls this one, not the one defined above
call_the_original_update #first, call the original update
@hp = 0 # basically, append this command to the end of the original update method
end
end


Pretty much the same as adding your new statement to the end of the original method, without having to edit the default scripts.

TTFN
Relax, guys... that's why I said it's my opinion. When I started using RPG Maker, I didn't know anything about scripting. I was like 10yo or something. And yet, for some reason, when I wanted to change something script-wise, I always found it. Might've taken me a second or two longer than if I would've just asked, but I found it. And I guess it did make me learn the whole thing after all, so that's where I come from.
And I'm aware that there's software where it's more vital to have an efficient code base. Thing is, why wouldn't you try to go for efficiency whereever you can? You're not driving all the way to work in first gear just because your car would survive it, do you?
Well, it's just my opinion. And the last thing I meant was trying to call the script bad because of that.
I'm relaxed. :scruff:

Yeh, I learned the same way. But I don't expect everyone else that might use this stuff to be programmers.

There are 2 different types of scripting, generally. 1) Scripting for scripters (SDK, MACL, Bitmap extensions, etc.) 2) Scripting for game developers that don't program. (Plug-n-play, "Super-Simple", etc..)

If you look at the few scripts I've published, you'll see I made some 'compromises' to make them as simple as possible for the "customer".
In fact, I always try to find a solution that allows the user to use the GUI to do what they need to do. (e.g. use 'terrain' to define an area instead of a hash in the script).

Quote:
why wouldn't you try to go for efficiency wherever you can?


If you're learning to be a professional programmer, you would. It's really good to get yourself in the habit of ALWAYS looking for more efficient ways of doing things. If you're teaching someone to program, absolutely!
On the contrary, if you're creating something for non-programmers to use, in this case API programs that need some configuration, why wouldn't you go the extra step & make it as 'user-friendly' as possible when the difference in performance is negligible?

Seph & I spent an afternoon a few yrs ago doing a battery of 'stress tests' on methods in the SDK, and some other 'graphics' related methods sparked by a few similar complaints as yours, to measure the differences in computation time, and in most cases the differences were so minute they were 'inconclusive'.

You said, "...a few things that make me twitch" and "...is unlimited times inefficient", but the only inefficiency you pointed out was the use of Constants.
If you care to point out something you believe is truly inefficient, we're all ears. The nature of the program; representing light more accurately by subtracting from the darkness -vs- adding 'white' which makes the result look hazy is inherently demanding on the processor. While the methods we chose to use are pretty fast, if you think there's a better way we certainly want to hear it. e.g. the subtraction methods. The only thing I can think of is to extend the bitmap methods using a compiled .dll instead of interpreted code. I'm sure there would be a measurable difference.

TTFN
I assume it's just a matter of different perspective... in my opinion, everyone working on a game should at least have a slight idea of what's going on. For the relatively small amount of people that know how to script and make games (and not just scripts; you know what I mean), there's a lot of people who's project threads basically consist of a number of scripts they took from somewhere. While that is a different issue to discuss, I feel people should at least have a slight glimpse of an idea. I'm not trying to force them, but seriously, making games without knowing code is not more than putting resources together. Now I'm not saying that's a bad thing, but personally, I don't think anyone gets great games out of a habit such as that. (just as people who only know scripting usually have comparetively worse graphics or storyline).
And just to clarify it, with "unlimited times inefficient" I meant that using a constant for a single-used value just means that there's a (technically) redundant call every time the script is executed.


As far as improving goes, I've already checked out the methods you posted in the working thread. Personally, I suck at math, so I probably couldn't point out anything even if there was something... so yeah, sorry to not be of any help on that end.

What I can suggest is that you might want to alter the method of determining caves, houses, ... maybe a thing in the map name, however that's not prone to failure.
I think using a variable instead would be better - instead of setting houses and caves, set the variable to the desired amount of opacity change, saving three to four constants in the process besides :p That'd allow for a dynamic, completely accessible control and might even enable stuff like caves that get darker the further you go into them (think on player touch events that change that opacity level). Might look pretty cool!
There seems to be a bug with the script not updating the light sources when you are on the map.

For example in the demo. If you went into the home and back outside during the day, and waited around until night, none of the light sources update to display. That is until you leave the map and come back. Same for inside the home if you sit and wait.

They also don't update if your out at night and wait for day again until you leave and come back.
We have been taught (gammaking university) that we should never put fixed numbers into our programs and should always put them out as changeable constraints. Though that is different, I guess, because in C++ it's compiled not interpreted, so our constraints can be done as preprocessor find and replace doodahs.

This is a great script though and hopefully will fix the trend of "lights over light" that people seem to like. (Light sources don't block vision, they enhance it!)
Taiine wrote:
There seems to be a bug with the script not updating the light sources when you are on the map.

For example in the demo. If you went into the home and back outside during the day, and waited around until night, none of the light sources update to display. That is until you leave the map and come back. Same for inside the home if you sit and wait.

They also don't update if your out at night and wait for day again until you leave and come back.

Thanks for finding this for me. The problem lies within not being able to determine what a light is and updating it properly, I figured out a solution but it causes a giant lag for a short second at an hour of the day which is annoying in general. Will have to figure out a better solution later. For now, I've updated the demo to have events where things remain comment:light100 the whole time, it's not like you can tell they are lights during the day if there's no darkness being added. And even if there was, it still wouldn't show. We'll just use that as a workaround for now, till I come up with a better solution.

@Wyatt What's a fixnum? and a Changeable constraint? I haven't gone to any university or anything I'm only good at math.
Show me what you mean and lets see if I can't grasp it.

BlueScope wrote:
Personally, I suck at math, so I probably couldn't point out anything even if there was something...

I'm afraid this script wouldn't be here today if it wasn't for math, god I love math, it's the multi-language of all cultures.

I updated the script and cleaned it up a bit using a few things. I was also thinking of separating the "settings" part of the script and making it to where you could remove it and the script would still function.

Meaning
if HOUR_VARIBLE == nil
HOUR_VARIBLE = 9
end

etc.
That works fine, really I see no issues yet as to why they can't just stay on.
This is likely to make it into my game, it's by fare one of the better day/night systems I've found, and being able to add lighting as well? extra bonus there.

Though with events not updating as time changes unless you change maps, it could be tricky if I wanted a npc only showing up say at night or such. mm..
Its not that events weren't updating. It was the script wasn't updating when the lights came on at that time. But when I put it in the script to fix it, it causes a one second jolt of lag which is not exceptable in my book. Events should still update normally even if our script wasn't there. Alternatively, if you want to be sure they do, under spriteset map class add this code to the update method

if @hour_day != $game_variables[HOUR_VARIABLE]
$game_map.refresh
@hour_day = $game_variables[HOUR_VARIABLE]
end

edit: I just realized something with my own script that Gubid added, you can change the size of the light by changing 100 to bigger number or smaller number.
So, I found a way to allow this to be much faster. It however is not as exact either, but I think with a couple of modifications it wouldnt matter. I do plan at some point to make it smarter by writing my own function in C to do what I need, but until then this should work.

Now we just need to update this script to:
1. Allow moving lights (since it wont be uber slow now)
2. Work with VX

Yep, its gonna be da bling!

Updated Demo with some extras for raster operations: http://www.mediafire.com/?u92cfybjbxlciwh
Sorry for the necropost but I am at wit's end..

First off, let me just say this is an amazing script. I am thoroughly impressed with everything it sets out to do! But I am having some difficulties with it. I can get the game to load the script fine and see the light sources I placed until I load a map with an event that requires a variable and/or switch to activate the event (which is like every map.. lol).

When I do that I get this error: "Script 'DNS' line 296: NoMethodError occurred. undefined method 'size' for nil:NilClass"

This totally cripples my game and makes it unplayable :(... That's no good because this is the best script I've seen lol.. It fits perfectly in my game. I've already checked to see if it was a script conflict and there was no problem there. So I wondering what I'm doing wrong and if there is any way to remedy this problem? I desperately want to use this script its too good to pass up.
I have the same error as uldamoc :\

This script looks great, but what would make it more useful for me is to replace the $game_variables with the Time class.
In my case, I have a constantly updating $time global variable.

I've attempted to edit this multiple times to change it to Time format, but the code is sort-of messy, and I just cant get it to work with the time class.
Could you make another copy that reads just from a single variable (for the time)?

By the way, you can suppress (but i don't think it fixes it) the error by adding under line 295:

next if event.list == nil
Wow thanks ZenVirZan! That actually did the trick.

The script no longer conflicts with inactive variables and switches.. but I'm curious if there is a way to set the darkness manually without messing with the time system. For example, going into a cave during the day but the screen will become dark (to emulate the darkness of a cave) while the time system still runs.

Doesn't make much sense to have your underground caves or dungeons light up during the day and only be dark at night lol. Maybe a house with windows.. but not caves, dungeons, cellars etc.

Could this possibly be done with a switch or variable? (Example: You walk into a cave and Switch [Darkness]=ON activates) ..and for that matter is there a way to replace the 3 variables that are being used by the script with a variable number of your choice? All of the script's variables are using variables that my game is already using and it would be horrible to have to replace all of them. (Though not impossible.. just a huge pain)

For now I'm going to just disable time and manually set the time of day to get the dark effect I need.

Anyway.. thanks again ZenVirZan for the help!
uldamoc wrote:
I'm curious if there is a way to set the darkness manually without messing with the time system. For example, going into a cave during the day but the screen will become dark (to emulate the darkness of a cave) while the time system still runs.

Doesn't make much sense to have your underground caves or dungeons light up during the day and only be dark at night lol. Maybe a house with windows.. but not caves, dungeons, cellars etc.

Could this possibly be done with a switch or variable? (Example: You walk into a cave and Switch [Darkness]=ON activates) ..and for that matter is there a way to replace the 3 variables that are being used by the script with a variable number of your choice? All of the script's variables are using variables that my game is already using and it would be horrible to have to replace all of them. (Though not impossible.. just a huge pain)

For now I'm going to just disable time and manually set the time of day to get the dark effect I need.


This is already in the script. check lines 42 - 57:
from the script wrote:
  1. # Inside_Switch is the switch set to make insides darker, not meant for caves
  2. #-----------------------------------
  3. INSIDE_SWITCH = 9
  4.  
  5. # Inside_Opacity is the opacity set to increase when entering insides
  6. #-----------------------------------
  7. INSIDE_OPACITY = 10
  8.  
  9. # Cave_Switch is the switch set meant for caves
  10. #-----------------------------------
  11. CAVE_SWITCH = 11
  12.  
  13. # Cave_Opacity is the total opacity you want when entering caves.  Generally
  14. # Should be set higher than the highest darkness.
  15. #-----------------------------------
  16. CAVE_OPACITY = 220
Expand to see the code.


Those numbers represent the switches used to trigger houses and caves as well as the opacity. you can change the number to the switch id that you are using.
I've got a problem -
All of the lights on the map have a comment light300
But only 1 of them is large. The rest are only the 100 size.
I added some prints into the script - the size variable is fine, its just drawing it wrong. Any ideas?

EDIT:
Appears to occur when 2 lights are near each other - i dont thing the

'self.bitmap.stretch_blt(light_rect, image_blt, base_rect)'

line is ever run (making it bigger) if you have to run

'self.bitmap.sub(light.x,light.y,image_sub)'

which does when the 'if overlapLights.size == 0' isnt == 0.

. . . if that makes sense?

EDIT2:
And even the default light100 lights are slightly different sizes to each other :\
this is great' let's see the spoiler......
ZenVirZan wrote:
I've got a problem -
All of the lights on the map have a comment light300
But only 1 of them is large. The rest are only the 100 size.
I added some prints into the script - the size variable is fine, its just drawing it wrong. Any ideas?

EDIT:
Appears to occur when 2 lights are near each other - i dont thing the

'self.bitmap.stretch_blt(light_rect, image_blt, base_rect)'

line is ever run (making it bigger) if you have to run

'self.bitmap.sub(light.x,light.y,image_sub)'

which does when the 'if overlapLights.size == 0' isnt == 0.

. . . if that makes sense?

EDIT2:
And even the default light100 lights are slightly different sizes to each other :\

I'm not so sure about the sizing part or overlapping part since most of that was brew and gubid or gubid and brew, however you look at it. But I have discovered a work around for your problem, kinda stupid in a sense cause you have to make more graphics for bigger lights next to each other. I just made the normal sub graphic three times it's size and named it LightSUBBIG.

  1.  def apply_lights
  2.     applied_space = []
  3.     for light in @lights.values
  4.       image_sub = RPG::Cache.picture("LightSUB").clone
  5.       image_blt = RPG::Cache.picture("LightBLT").clone
  6.       image_sub2 = RPG::Cache.picture("LightSUBBIG").clone
  7.       base_rect = image_sub.rect
  8.       overlapLights = []
  9.       size = (light.size/100.0)
  10.       lwidth = base_rect.width*size
  11.       light_rect = Rect.new(light.x-(lwidth/2), light.y-(lwidth/2), lwidth, lwidth)
  12.       for applied_rect in applied_space
  13.         if light_rect.overlap?(applied_rect)
  14.           overlapLights << light_rect.area_in_overlap(applied_rect)
  15.         end
  16.       end
  17.       #now draw it
  18.       if overlapLights.size == 0 #no overlap detected.. then just BLT it into the image
  19.         self.bitmap.fill_rect(light_rect, Color.new(0,0,0,0))
  20.         self.bitmap.stretch_blt(light_rect, image_blt, base_rect)
  21.       else
  22.         if size == 3
  23.           self.bitmap.sub(light.x,light.y,image_sub2)
  24.           else
  25.         self.bitmap.sub(light.x,light.y,image_sub)
  26.           end
  27.       end
  28.       applied_space << light_rect
  29.       image_sub.dispose
  30.       image_sub2.dispose
  31.       image_blt.dispose
  32.     end
  33.   end
Expand to see the code.


Then that would make any lights next to each other with a light300 sub to normal size. I'm not currently working with this script because it basically suits all my needs but I'll try to help anyway I can.

Yeah I noticed the inactive switch error a while back. My first work around was to make the first page of any event be blank if it needed a switch or variable condition. My next work around or fix was unless event.erased or event.list == nil after the line that says
for event in $game_map.events.values.

Quote:
I've attempted to edit this multiple times to change it to Time format, but the code is sort-of messy, and I just cant get it to work with the time class.
Could you make another copy that reads just from a single variable (for the time)?


I'm not sure what you mean by this. You're wanting a variable that just reads time? Basically rmxp reads every second as 40 frames in the scripts. I just use the timer under the system class. I've tried to make my own time class, but it never worked and it was just more convenient to use the timer because it's accessible in the events.
Is there any possibility that I can have say...a store open only certain hours (during the day) and have it closed during the other hours? (Night)

Also, I'm a very graphic heavy user (photoshop graphic artist), and I've noticed that the darkness seriously interferes with the graphics that show up.

e.g:
Content Hidden Show
Image
The windowskin and faceset (which are both just pictures) SHOULD be much brighter
You can adjust the Z height of the overlay map.

Just edit:
  1.  
  2. #---------------------------------------------
  3. #Spriteset_Map
  4. #---------------------------------------------
  5. class Spriteset_Map
  6.   alias init_spr_map_lights initialize
  7.   def initialize
  8.     # Make nighttime sprite
  9.     @nighttime = Nighttime.new(0,0)
  10.     @nighttime.z = 2000   #THIS LINE TO HAVE A LOWER NUMBER
  11.     init_spr_map_lights
  12.     @nighttime.refresh
  13.     @map_idd = $game_map.map_id
  14.   end
  15.  
Expand to see the code.


And yes, just attach your store to only show up during the dependant hours. You would use the time variable and just make a guess on where those hours would be.
Anything can be done to show a "light" on your character using the comment method? because when you try with show picture it won't work as the script covers the map with black tinting.

Also is there a way to fix the lights not going away when you try to do so but have to go in a new map or open/close inventory to have them go away?

Thanks
Bump

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=74494