Windows Surfaces drawing Darkness below the floor.

E

eldinhelja

Guest
Hello,
I need help, I want to create darkness below the highest obj_Floor object.

Here is the game :


What I did there is I only Created surface added darkness to it, and drew it at constant location...
But what I want to achieve is dynamic darkness like in terraria :


It may be complex for me, but just give me some help to start.
I can't google this anywhere.

My obj_Floor is dynamic... I can destroy those objects so, my lightning needs to be dynamic.
If you can't understand what I want to achieve please reply, I will try to explain it in other way.


Any Help, any reply is appreciated,
Thanks
 
A

anomalous

Guest
I suspect they use a shader, its the basic flood fill I think, its a diamond pattern (like minecraft I think is the same, but 3d)
Its also probably time spliced such that it delays a fraction of a second before updating the darkness (allowing one to split up the load in updating the light in that time).
Terraria can probably also take advantage of more native coding, without all the other overhead a pre-made engine has, so don't be surprised if it's too intensive.
I don't think it's a beginner level task. They also use a very small 16 pixel size I think (covering the entire screen that's a lot of tiles)

If you want a poor man's version that you can test it out with something like this tutorial..there is a part #2 as well:

Basically fill the room with objects that draw darkness the same size as your tile size based on their proximity to light. Its easy to set up, and you can determine if you really like that look.
You can also double the number (If you use 64x64 tiles, you can do a 2x2 of 32x32 dark objects to get finer darkness). It's pretty easy to set up, I think you just use collision_line and point_distance to set the alpha or something. It's terrible on performance (brute force w/objects), this is just for you to see if it's really the direction you want to go.
 
E

eldinhelja

Guest
Hello @anomalous,
Thanks for reply
I have found this tutorial :
Its what I want to achieve. But in the video he got a little bit lost, and didn't explain many stuff. So I got an error

I created shader script (scr_shader) :
Code:
draw_sprite_ext(sprite_index,image_index, x,y, image_xscale, image_yscale, image_angle, noone, 1)

dist_parent = distance_to_object(instance_nearest(x,y,obj_SolidParent))

closest_object = dist_parent

temp_alpha=closest_object/128
alpha1=1

if temp_alpha < 1
alpha1=temp_alpha

draw_sprite_ext(sprite_index,image_index, x,y, image_xscale, image_yscale, image_angle, c_black, alpha1)
obj_SolidParent is parent of dirt, stone, grass. It has no sprite. So when I create DRAW EVENT inside obj_SolidParent, and in the DRAW_EVENT I add the following code :
Code:
scr_shader();
It gives me an error saying :

___________________________________________
############################################################################################
ERROR in
action number 1
of Draw Event
for object obj_SolidParent:

Trying to draw non-existing sprite.
at gml_Script_scr_shader (line 2) - draw_sprite_ext(sprite_index,image_index, x,y, image_xscale, image_yscale, image_angle, noone, 1)
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scr_shader (line 2)
called from - gml_Object_obj_SolidParent_DrawEvent_1 (line 1) - scr_shader()
 
K

Kris Hyre

Guest
Well, it looks to me like your script is trying to draw_sprite_ext the sprite of obj_SolidParent, which you mentioned doesn't have a sprite, and its returning the error "Trying to draw non-existing sprite."

PS I don't know what that fly did in the image in the original post, but it looks like its about to hate life.
 
E

eldinhelja

Guest
@Kris Hyre
Yes, but why is that an error. When I type sprite_index in obj_SolidParent , it should retrieve child's sprite_index right ?

Yeah, hahahha, flies are enemies.

I am making this game for my little cousin. She likes it a lot, she has fun and its entertaining for her xD. Making the game look like she wants, but I add details. So I practice + I am making the child happy :)
 
K

Kris Hyre

Guest
@eldinhelja I would imagine so, as its supposed to draw the sprite of the instance calling it, but from the error, it appears that that is not happening. I would try removing the script call from the parent and try it with an child object that has a sprite assigned and see if the code works. Otherwise, do you have an instance of the obj_SolidParent somewhere (not a child object) that is causing the error when you try to run?
 
E

eldinhelja

Guest
@Kris Hyre
I had obj_SolidParent in my room. You know... with sign "?". It shouldn't be there I guess. So I removed it... and children objects were all there, everything was fine. I haven't noticed that I put obj_SolidParent in my room.
Now shading :
So, now I removed instance of obj_SolidParent, and script is not causing the error again... But now you need to help me with script I guess ... Look at this :

Every object is now black
Here is the script code :
Code:
draw_sprite_ext(sprite_index,image_index, x,y, image_xscale, image_yscale, image_angle, noone, 1)

dist_parent = distance_to_object(instance_nearest(x,y,obj_SolidParent))

closest_object = dist_parent

temp_alpha=closest_object/128
alpha1=1

if temp_alpha < 1{
alpha1=temp_alpha
}


draw_sprite_ext(sprite_index,image_index, x,y, image_xscale, image_yscale, image_angle, c_black, alpha1)
I forgot to mention that... If I put the DRAW EVENT inside obj_SolidParent, and I dont put any code in it. Objects are invisible... If i put the scr_shaders() then every object is Black.

So I created new object without sprite and Added draw event to that object and I put code scr_shaders. And I made that obj_Break parent of obj_SolidParent.
But still problem is same ...

Just to test things out in Script :
Code:
draw_sprite_ext(sprite_index,image_index, x,y, image_xscale, image_yscale, image_angle, c_black, 0.5)
I changed "alpha1" to 0.5 on last argument. And objects are half black. So Script is functioning only that dist_parent, closest_object, temp_alpha could make a problem... I don't understand this code...

Thanks
 
Last edited by a moderator:
K

Kris Hyre

Guest
@Kris Hyre
I had obj_SolidParent in my room. You know... with sign "?". It shouldn't be there I guess. So I removed it... and children objects were all there, everything was fine. I haven't noticed that I put obj_SolidParent in my room.
Now shading :
So, now I removed instance of obj_SolidParent, and script is not causing the error again... But now you need to help me with script I guess ... Look at this :

Every object is now black
Here is the script code :
Code:
draw_sprite_ext(sprite_index,image_index, x,y, image_xscale, image_yscale, image_angle, noone, 1)

dist_parent = distance_to_object(instance_nearest(x,y,obj_SolidParent))

closest_object = dist_parent

temp_alpha=closest_object/128
alpha1=1

if temp_alpha < 1{
alpha1=temp_alpha
}


draw_sprite_ext(sprite_index,image_index, x,y, image_xscale, image_yscale, image_angle, c_black, alpha1)
I forgot to mention that... If I put the DRAW EVENT inside obj_SolidParent, and I dont put any code in it. Objects are invisible... If i put the scr_shaders() then every object is Black.

So I created new object without sprite and Added draw event to that object and I put code scr_shaders. And I made that obj_Break parent of obj_SolidParent.
But still problem is same ...

Just to test things out in Script :
Code:
draw_sprite_ext(sprite_index,image_index, x,y, image_xscale, image_yscale, image_angle, c_black, 0.5)
I changed "alpha1" to 0.5 on last argument. And objects are half black. So Script is functioning only that dist_parent, closest_object, temp_alpha could make a problem... I don't understand this code...

Thanks
Well, good to know that object inheritance was working as intended, and was just an instance of obj_SolidParent that was attempting to draw nothing.

As far as why nothing shows if you create a Draw Event and put no code in it, that is because if an object has a draw event, and does not draw_self() (or draw_sprite_ext() or similar) the object will be invisible. That's expected.

Why your code (which is not exactly shaders, but whatever) is making everything black I'm not sure about, I would actually expect it to not make anything black. You are getting the distance_to_object() of objects that are literally next to each other. Unless there was something funny going on with their mask or sprite, I would expect the distance between them to be 0 pixels, resulting in the temp_alpha being 0 as well (0/128, carry the nothing, would still be 0). But instead its still using your alpha1 = 1 value...not sure about that yet
 
Last edited by a moderator:
T

ThunkGames

Guest
Here is what I would do:
Create a separate object called obj_lighting or something. Make sure it's depth is so that it draws above the blocks. Keep a grid of all blocks, 1 if there is a block at that position and 0 if there isn't. If you wanted to get fancy and have semi-transparent blocks with values between 0 and 1. Make sure that every time a block is created or destroyed, this grid is updated to reflect that. Every time the grid is updated, refresh a surface that is the dimensions of the grid. To refresh the surface, clear it with draw_clear_alpha(c_white, 0) and use two for loops to cycle through every space in the grid. Make sure the drawing target is the surface. if the value at that tile is 0, do not draw anything. If it is greater than 0, set the alpha to (1 / .5) * clamp(ds_grid_get_disk_mean(grid, x, y, 3) - .5, 0, 1) and draw_point(x, y) where grid is the grid of values, x and y is the position in the loops, 3 is the radius of the check (the higher this is the slower things will be) and .5 means if half of the tiles around a block are not blocks there will be no darkness. Make sure the color is set to what color you want the darkness to be. Every step, enable pixel interpolation if it is not enabled (you can check in your global game settings) (for smooth lighting) and then draw the surface stretched to the room size. I would go more in depth but I'm on mobile right now. If you want more help I can lend you a hand but not until I'm back at my computer.

-David

Edit:
Looks like this:
 
Last edited by a moderator:

Yal

šŸ§ *penguin noises*
GMC Elder
point_distance() only takes coordinates into account and is faster, so if you could use that instead of distance_to_object() it might speed things up.

And @ThunkGames has a good point with the surface approach - updating image_blend will take longer and longer the more stuff is in your game world, but only drawing on a screen-sized region will take the same amount of processing no matter how small or big the world is.
 
E

eldinhelja

Guest
@ThunkGames I like where this is going, I would like you to help me more. So please when you find some free time expand the explanation, and put more details for me, because I haven't done this before.

Have I done steps you wrote ? :
Code:
//obj_Lighting Create Event

mygrid = mp_grid_create(0,0, room_width/32, room_height/32, 32,32)
mp_grid_add_instances(mygrid, obj_SolidParent, false) //obj_SolidParent contains all blocks
var mydsgrid;
mydsgrid = ds_grid_create(room_width/32, room_height/32)
mp_grid_to_ds_grid(mygrid, mydsgrid)

surf = surface_create(room_width, room_height)

// The following two lines clear the surface
// There is no guarantee that the surface doesn't contain garbage info post-creation

surface_set_target(surf)
draw_clear_alpha(c_white, 0)
draw_set_colour(c_green);
var xx;
var yy;


for(xx=16; xx<=room_width; xx+=32){

    for(yy=16; yy<=room_height; yy+=32) {
      
    //Check if there is object at position, if there is, set value to 1 else value = 0
           if(instance_place(xx,yy, obj_SolidParent)) {
                mydsgrid[# xx, yy] = 1
                draw_set_alpha(clamp(ds_grid_get_disk_mean(mygrid, xx, yy, 3) - .5, 0, 1))
                draw_point(xx,yy)
           } else {
                mydsgrid[# xx, yy] = 0
           }
  
    }

}

draw_set_color(c_white)
// Reset to application surface
surface_reset_target();
Code:
//Draw End

draw_surface(surf, 0,0);
Have I done anything right ? xD

@Yal Thanks for information for making things faster, but I am switching to @ThunkGames plan.
I like what he has done in his game... Its perfect, just like I want :)
 
T

ThunkGames

Guest
Here is what I would do:

Create event-
Code:
//Some variables
global.ShadowGrid = ds_grid_create(room_width / 32, room_height / 32);
global.ShadowSurface = surface_create(room_width / 32, room_height / 32);
global.ShadowSurfaceRefresh = true;

//Set up the grid
for (var j = 0; j < room_height / 32; j++) {
for (var i = 0; i < room_width / 32; i++) {
if position_meeting(i * 32, j * 32, obj_SolidParent) {
ds_grid_set(global.ShadowGrid, i, j, 1);
} else {
ds_grid_set(global.ShadowGrid, i, j, 0);
}
Draw Event-
Code:
//Surfaces can cease to exist sometimes
if !surface_exists(global.ShadowSurface) {
global.ShadowSurface = surface_create(room_width / 32, room_height / 32);
global.ShadowSurfaceRefresh = true;
}

//If the surface needs to be refreshed
if global.ShadowSurfaceRefresh {
surface_set_target(global.ShadowSurface);
draw_clear_alpha(c_white, 0);
draw_set_color(c_black);
for (var j = 0; j < room_height / 2; j++) {
for (var i = 0; i < room_width / 2; i++) {
if ds_grid_get(global.ShadowGrid, i, j) > 0 {
draw_set_alpha(clamp((ds_grid_get_disk_sum(global.ShadowGrid, i, j, 3)) / 8, 0, 1)); //You can tweak the 8 and 3 number until you get the look you're going for
draw_point(i, j);
}
}
draw_set_alpha(1);
surface_reset_target();
}

//If you want smooth lighting and pixel interpolation is off do texture_set_interpolation(true);
//If pixel interpolation is on and you want blocky lighting do texture_set_interpolation(false);
draw_surface_stretched(global.ShadowSurface, 0, 0, room_width, room_height);
//Make sure you set interpolatio nback to how it was if you did anything with it
Whenever a block is created-
Code:
ds_grid_set(global.ShadowGrid, BlockX / 32, BlockY / 32, 1);
global.ShadowSurfaceRefresh = true;
Whenever a block is destroyed-
Code:
ds_grid_set(global.ShadowGrid, BlockX / 32, BlockY / 32, 0);
global.ShadowSurfaceRefresh = true;
Sorry for the late reply, had to get back home. Tell me if you need me to explain anything.
-David

Edit:
I've been assuming all of your obj_SolidParents have been squares. If not... sorry for wasting your time :( Also the screenshot I included is with pixel interpolation on. I manually disabled it however I do enable it when I draw the lighting surface to give the lighting a smoother appearance.

Edit 2: (6:21 ET)
Fixed something.
 
Last edited by a moderator:
E

eldinhelja

Guest
Hello @ThunkGames,
Don't be sorry for late reply, I appreciate your replies whenever you post them.

I just copied and paste the code, just to see if it works (then later I would read documentation if I don't understand something) and I started the game and in the compile form the messages start to appear :
Grid 0, index out of bounds writing [45,5] - size is [45,26]
Grid 0, index out of bounds writing [45,6] - size is [45,26]
Grid 0, index out of bounds writing [45,7] - size is [45,26]
Grid 0, index out of bounds writing [45,8] - size is [45,26]
Grid 0, index out of bounds writing [45,9] - size is [45,26]
Grid 0, index out of bounds writing [45,10] - size is [45,26]
Grid 0, index out of bounds writing [45,11] - size is [45,26]
Grid 0, index out of bounds writing [45,12] - size is [45,26]
Grid 0, index out of bounds writing [45,13] - size is [45,26]
Grid 0, index out of bounds writing [45,14] - size is [45,26]
Grid 0, index out of bounds writing [45,15] - size is [45,26]
Grid 0, index out of bounds writing [45,16] - size is [45,26]
Grid 0, index out of bounds writing [45,17] - size is [45,26]
Grid 0, index out of bounds writing [45,18] - size is [45,26]
Grid 0, index out of bounds writing [45,19] - size is [45,26]
Grid 0, index out of bounds writing [45,20] - size is [45,26]
Grid 0, index out of bounds writing [45,21] - size is [45,26]
Grid 0, index out of bounds writing [45,22] - size is [45,26]
Grid 0, index out of bounds writing [45,23] - size is [45,26]
.
.
.
It won't stop.

The error is in Draw event. In the code you posted there is one missing bracket in draw event, I put it
Code:
.
.
.
draw_set_alpha(1);
surface_reset_target();
}
} <---- Here
Please check one more time, the draw event.

Thanks for your Time, I really appreciate it !!!

Edit:
All of my objects which have the obj_SolidParent as their parent are squares.

Code:
for (var j = 0; j < room_height / 2; j+=1) // Should this be 2 or 32 ?
for (var i = 0; i < room_width / 2; i+=1)
 
Last edited by a moderator:
T

ThunkGames

Guest
Hello @ThunkGames,
Don't be sorry for late reply, I appreciate your replies whenever you post them.

I just copied and paste the code, just to see if it works (then later I would read documentation if I don't understand something) and I started the game and in the compile form the messages start to appear :
Grid 0, index out of bounds writing [45,5] - size is [45,26]
Grid 0, index out of bounds writing [45,6] - size is [45,26]
Grid 0, index out of bounds writing [45,7] - size is [45,26]
Grid 0, index out of bounds writing [45,8] - size is [45,26]
Grid 0, index out of bounds writing [45,9] - size is [45,26]
Grid 0, index out of bounds writing [45,10] - size is [45,26]
Grid 0, index out of bounds writing [45,11] - size is [45,26]
Grid 0, index out of bounds writing [45,12] - size is [45,26]
Grid 0, index out of bounds writing [45,13] - size is [45,26]
Grid 0, index out of bounds writing [45,14] - size is [45,26]
Grid 0, index out of bounds writing [45,15] - size is [45,26]
Grid 0, index out of bounds writing [45,16] - size is [45,26]
Grid 0, index out of bounds writing [45,17] - size is [45,26]
Grid 0, index out of bounds writing [45,18] - size is [45,26]
Grid 0, index out of bounds writing [45,19] - size is [45,26]
Grid 0, index out of bounds writing [45,20] - size is [45,26]
Grid 0, index out of bounds writing [45,21] - size is [45,26]
Grid 0, index out of bounds writing [45,22] - size is [45,26]
Grid 0, index out of bounds writing [45,23] - size is [45,26]
.
.
.
It won't stop.

The error is in Draw event. In the code you posted there is one missing bracket in draw event, I put it
Code:
.
.
.
draw_set_alpha(1);
surface_reset_target();
}
} <---- Here
Please check one more time, the draw event.

Thanks for your Time, I really appreciate it !!!

Edit:
All of my objects which have the obj_SolidParent as their parent are squares.

Code:
for (var j = 0; j < room_height / 2; j+=1) // Should this be 2 or 32 ?
for (var i = 0; i < room_width / 2; i+=1)
Replace this:
Code:
for (var j = 0; j < room_height / 2; j++) {
for (var i = 0; i < room_width / 2; i++) {
with this:
Code:
for (var j = 0; j < room_height / 32; j++) {
for (var i = 0; i < room_width / 32; i++) {
Just forgot the 3 my bad.
 
E

eldinhelja

Guest
Replace this:
Code:
for (var j = 0; j < room_height / 2; j++) {
for (var i = 0; i < room_width / 2; i++) {
with this:
Code:
for (var j = 0; j < room_height / 32; j++) {
for (var i = 0; i < room_width / 32; i++) {
Just forgot the 3 my bad.
I replaced it, but still same thing.Something else is causing error as well
 
T

ThunkGames

Guest
Does the lighting work? Can you paste the complete error log please.
 
E

eldinhelja

Guest
Compile started: 01.03.17
"F:\Steammapps\steamapps\common\gamemaker_studio\GMAssetCompiler.exe" /c /m=win /config="Default" /tgt=64 /obob=True /obpp=False /obru=True /obes=False /i=2 /cvm /tp=2048 /mv=1 /iv=0 /rv=0 /bv=1757 /gn="AmilaGame" /td="C:\Users\EKREDIN-PC\AppData\Local" /cd="C:\Users\EKREDIN-PC\Documents\GameMaker\Cache" /sh=True /dbgp="6502" /hip="192.168.1.100" /hprt="51268" /o="C:\Users\EKREDIN-PC\AppData\Local\gm_ttt_86634\gm_ttt_36905" "H:\GameMaker Steam\PROJECTS\AMGameAutoGen.gmx\AmilaGame.project.gmx"

Reading project file....finished.
Compile Constants...finished.
Remove DnD...finished.
Compile Scripts...finished.
Compile Objects...finished.
Compile Timelines...finished.
Compile Triggers...finished.
Compile Rooms...finished.
Compile Extensions...finished.
Final Compile...finished.
Saving IFF file... C:\Users\EKREDIN-PC\AppData\Local\gm_ttt_86634\gm_ttt_36905\AmilaGame.win
Writing Chunk... GEN8
Writing Chunk... OPTN
Writing Chunk... EXTN
Writing Chunk... SOND
Writing Chunk... AGRP
Writing Chunk... SPRT
Writing Chunk... BGND
Writing Chunk... PATH
Writing Chunk... SCPT
Writing Chunk... SHDR
Writing Chunk... FONT
Writing Chunk... TMLN
Writing Chunk... OBJT
Writing Chunk... ROOM
Writing Chunk... DAFL
Writing Chunk... TPAGE
Writing Chunk... CODE
Writing Chunk... VARI
Writing Chunk... FUNC
Writing Chunk... STRG
Writing Chunk... TXTR
0 Compressing texture... writing texture texture_0.png...
Writing Chunk... AUDO
Writing Chunk... SCPT
Writing Chunk... DBGI
Writing Chunk... INST
Writing Chunk... LOCL
Writing Chunk... STRG
Stats : GMA : Elapsed=1603,0911
Stats : GMA : sp=25,au=0,bk=1,pt=0,sc=3,sh=0,fo=2,tl=0,ob=34,ro=2,da=0,ex=0,ma=100,fm=0x487C0260
Asset Compile finished: 01.03.21
-----------------------------------------------------------
executing C:\Users\EKREDIN-PC\AppData\Local\gm_ttt_86634\gm_ttt_36905\AmilaGame.win
-----------------------------------------------------------
"F:\Steammapps\steamapps\common\gamemaker_studio\Runner.exe" -game "C:\Users\EKREDIN-PC\AppData\Local\gm_ttt_86634\gm_ttt_36905\AmilaGame.win"


***************************************
* YoYo Games Runner v1.0(999)[r32908] *
***************************************
RunnerLoadGame: C:\Users\EKREDIN-PC\AppData\Local\gm_ttt_86634\gm_ttt_36905\AmilaGame.win
#########################################################################
####!!!!$$$$$$ pwd - C:\Users\EKREDIN-PC\AppData\Local\gm_ttt_86634\gm_ttt_36905\
#########################################################################
RunnerLoadGame() - C:\Users\EKREDIN-PC\AppData\Local\gm_ttt_86634\gm_ttt_36905\AmilaGame.win
Checking if INIFile C:\Users\EKREDIN-PC\AppData\Local\gm_ttt_86634\gm_ttt_36905/options.ini Exists
C:\Users\EKREDIN-PC\AppData\Local\gm_ttt_86634\gm_ttt_36905/options.ini file contents: [Windows] CreateTexturesOnDemand=0 AlternateSyncMethod=0 VertexBufferMethod=1 SleepMargin=1
Process Chunk: SCPT 652
Process Chunk: DBGI 9708
Process Chunk: INST 4
Process Chunk: LOCL 1928
Process Chunk: STRG 34456
Reading File C:\Users\EKREDIN-PC\AppData\Local\gm_ttt_86634\gm_ttt_36905\AmilaGame.win
Loaded File C:\Users\EKREDIN-PC\AppData\Local\gm_ttt_86634\gm_ttt_36905\AmilaGame.win(123012)
IFF wad found
Get Resolution
Get Header Information
InitGMLFunctions
HighScore..SavePrePend = C:\Users\EKREDIN-PC\AppData\Local\AmilaGame\
Game..Math..Graphic..Action..File..Resource..Interaction..3D..Particle..Misc..DS..Sound..Physics..Gamepad..GAMEPAD: 4 axis values (last) at 0x3cb4260
GAMEPAD: 4 axis values (last) at 0x3cb45a8
GAMEPAD: 4 axis values (last) at 0x3cb46c0
GAMEPAD: 4 axis values (last) at 0x3cb4698
Buffers..Networking..Shaders..InitPushFunctions...YoYo..Fini
Code_Load()
VARI_Load()
got 11 global variables
got 39 instance variables
got 12 local variables
ID_STRG
Create Window
AdjustWindowRectEx
RegisterClassEx
CreateWindowEx
Init Graphics
GR_D3D_Init()
Enable DwmEnableMMCSS
Create D3D9Ex interface
Enumerate Adapters
Selected Resolution: 1280x860
Prepare to create device
Creating D3D device - ARGB=32bit, D24S8, SwapEffect=Copy, Hardware vertex processing
Creating screen render target
Get Device Caps
Graphics initialisation end...
Background_InitTextures()
Sprite_InitTextures()
Font_InitTextures()
IO Init
Process Messages
Splash!
Start Frame
Part Create Textures
Debug Init Remote Interface
VM Init
Create Score Form
Create Load Form
Create Error Form
Do The Work
LoadGameData()
initialise everything!
Process Chunk: GEN8 140
Process Chunk: OPTN 64
Process Chunk: EXTN 4
Process Chunk: SOND 4
Audio_Load()
Process Chunk: AGRP 4
AudioGroup_Load()
Process Chunk: SPRT 5776
Process Chunk: BGND 28
Process Chunk: PATH 4
Process Chunk: SCPT 40
Process Chunk: SHDR 4
Process Chunk: FONT 4452
Process Chunk: TMLN 4
Process Chunk: OBJT 11876
Process Chunk: ROOM 5936
Process Chunk: DAFL 0
Process Chunk: TPAG 1280
Process Chunk: CODE 17288
Process Chunk: VARI 4012
Process Chunk: FUNC 2736
Process Chunk: STRG 8900
Process Chunk: TXTR 60272
Process Chunk: AUDO 4
Audio_WAVs()
PrepareGame()
Extension_Prepare()
Code_Constant_Prepare()
Script_Prepare()
TimeLine_Prepare()
Object_Prepare()
Preparing 34 objects:
Objects 0: obj_MTrail
Objects 1: obj_Metak
Objects 2: obj_Mouse
Objects 3: obj_Dot
Objects 4: obj_Muha
Objects 5: obj_Amila_new
Objects 6: obj_Beforee
Objects 7: obj_Spawner
Objects 8: obj_SpawnerDup
Objects 9: obj_Amila
Objects 10: obj_Dirt
Objects 11: water_new
Objects 12: obj_sun
Objects 13: obj_Wood
Objects 14: obj_Stone
Objects 15: obj_WALL
Objects 16: grass_new
Objects 17: Cloud
Objects 18: obj_Ball
Objects 19: obj_wtrNotSolid
Objects 20: obj_SolidParent
Objects 21: obj_FallParent
Objects 22: obj_Lighting
Objects 23: obj_Hcontrol
Objects 24: obj_rrGame
Objects 25: obj_Parent
Objects 26: obj_Draw
Objects 27: obj_Desk_Player
Objects 28: obj_DarkRoom
Objects 29: obj_EnemyController
Objects 30: obj_path_planner
Objects 31: object28
Objects 32: object29
Objects 33: obj_Turret
Room_Prepare()
Sound_Prepare()
InitGraphics()
Finished PrepareGame()
Run_Start
StartGame()
Grid 0, index out of bounds writing [0,26] - size is [45,26]
Grid 0, index out of bounds writing [1,26] - size is [45,26]
Grid 0, index out of bounds writing [2,26] - size is [45,26]
Grid 0, index out of bounds writing [3,26] - size is [45,26]
Grid 0, index out of bounds writing [4,26] - size is [45,26]
Grid 0, index out of bounds writing [5,26] - size is [45,26]
Grid 0, index out of bounds writing [6,26] - size is [45,26]
Grid 0, index out of bounds writing [7,26] - size is [45,26]
Grid 0, index out of bounds writing [8,26] - size is [45,26]
Grid 0, index out of bounds writing [9,26] - size is [45,26]
Grid 0, index out of bounds writing [10,26] - size is [45,26]
Grid 0, index out of bounds writing [11,26] - size is [45,26]
Grid 0, index out of bounds writing [12,26] - size is [45,26]
Grid 0, index out of bounds writing [13,26] - size is [45,26]
Grid 0, index out of bounds writing [14,26] - size is [45,26]
Grid 0, index out of bounds writing [15,26] - size is [45,26]
Grid 0, index out of bounds writing [16,26] - size is [45,26]
Grid 0, index out of bounds writing [17,26] - size is [45,26]
Grid 0, index out of bounds writing [18,26] - size is [45,26]
Grid 0, index out of bounds writing [19,26] - size is [45,26]
Grid 0, index out of bounds writing [20,26] - size is [45,26]
Grid 0, index out of bounds writing [21,26] - size is [45,26]
Grid 0, index out of bounds writing [22,26] - size is [45,26]
Grid 0, index out of bounds writing [23,26] - size is [45,26]
Grid 0, index out of bounds writing [24,26] - size is [45,26]
Grid 0, index out of bounds writing [25,26] - size is [45,26]
Grid 0, index out of bounds writing [26,26] - size is [45,26]
Grid 0, index out of bounds writing [0,27] - size is [45,26]
Grid 0, index out of bounds writing [1,27] - size is [45,26]
Grid 0, index out of bounds writing [2,27] - size is [45,26]
Grid 0, index out of bounds writing [3,27] - size is [45,26]
Grid 0, index out of bounds writing [4,27] - size is [45,26]
Grid 0, index out of bounds writing [5,27] - size is [45,26]
Grid 0, index out of bounds writing [6,27] - size is [45,26]
Grid 0, index out of bounds writing [7,27] - size is [45,26]
Grid 0, index out of bounds writing [8,27] - size is [45,26]
Grid 0, index out of bounds writing [9,27] - size is [45,26]
Grid 0, index out of bounds writing [10,27] - size is [45,26]
Grid 0, index out of bounds writing [11,27] - size is [45,26]
Grid 0, index out of bounds writing [12,27] - size is [45,26]
Grid 0, index out of bounds writing [13,27] - size is [45,26]
Grid 0, index out of bounds writing [14,27] - size is [45,26]
Grid 0, index out of bounds writing [15,27] - size is [45,26]
Grid 0, index out of bounds writing [16,27] - size is [45,26]
Grid 0, index out of bounds writing [17,27] - size is [45,26]
Grid 0, index out of bounds writing [18,27] - size is [45,26]
Grid 0, index out of bounds writing [19,27] - size is [45,26]
Grid 0, index out of bounds writing [20,27] - size is [45,26]
Grid 0, index out of bounds writing [21,27] - size is [45,26]
Grid 0, index out of bounds writing [22,27] - size is [45,26]
Grid 0, index out of bounds writing [23,27] - size is [45,26]
Grid 0, index out of bounds writing [24,27] - size is [45,26]
Grid 0, index out of bounds writing [25,27] - size is [45,26]
Grid 0, index out of bounds writing [26,27] - size is [45,26]
Grid 0, index out of bounds writing [0,28] - size is [45,26]
Grid 0, index out of bounds writing [1,28] - size is [45,26]
Grid 0, index out of bounds writing [2,28] - size is [45,26]
Grid 0, index out of bounds writing [3,28] - size is [45,26]
Grid 0, index out of bounds writing [4,28] - size is [45,26]
Grid 0, index out of bounds writing [5,28] - size is [45,26]
Grid 0, index out of bounds writing [6,28] - size is [45,26]
Grid 0, index out of bounds writing [7,28] - size is [45,26]
Grid 0, index out of bounds writing [8,28] - size is [45,26]
Grid 0, index out of bounds writing [9,28] - size is [45,26]
Grid 0, index out of bounds writing [10,28] - size is [45,26]
Grid 0, index out of bounds writing [11,28] - size is [45,26]
Grid 0, index out of bounds writing [12,28] - size is [45,26]
Grid 0, index out of bounds writing [13,28] - size is [45,26]
Grid 0, index out of bounds writing [14,28] - size is [45,26]
Grid 0, index out of bounds writing [15,28] - size is [45,26]
Grid 0, index out of bounds writing [16,28] - size is [45,26]
Grid 0, index out of bounds writing [17,28] - size is [45,26]
Grid 0, index out of bounds writing [18,28] - size is [45,26]
Grid 0, index out of bounds writing [19,28] - size is [45,26]
Grid 0, index out of bounds writing [20,28] - size is [45,26]
Grid 0, index out of bounds writing [21,28] - size is [45,26]
Grid 0, index out of bounds writing [22,28] - size is [45,26]
Grid 0, index out of bounds writing [23,28] - size is [45,26]
Grid 0, index out of bounds writing [24,28] - size is [45,26]
Grid 0, index out of bounds writing [25,28] - size is [45,26]
Grid 0, index out of bounds writing [26,28] - size is [45,26]
Grid 0, index out of bounds writing [0,29] - size is [45,26]
Grid 0, index out of bounds writing [1,29] - size is [45,26]


IT is not error, its just in a compile form, saying that it is out of bounds.
Trying to access array index that doesn't exist...
 
T

ThunkGames

Guest
I think I have an idea. After this:
Code:
//If the surface needs to be refreshed
if global.ShadowSurfaceRefresh {
Put this:
Code:
global.ShadowSurfaceRefresh = false
^That was nother mistake by me.

Now for your grid error. The error says it is for writing into the grid. This would mean the error is occuring in the create event. If the error is not coming from the create event, perhaps the nonexistence of space around the grid is causing ds_grid_get_disk_sum to try and access values outside of the grid. I'd try this:

Code:
global.ShadowGrid = ds_grid_create((room_width / 32) + 8, (room_height / 32) + 8);
and

Code:
if position_meeting(i * 32, j * 32, obj_SolidParent) {
ds_grid_set(global.ShadowGrid, i + 4, j + 4, 1);
} else {
ds_grid_set(global.ShadowGrid, i + 4, j + 4, 0);
}
and

Code:
draw_set_alpha(clamp((ds_grid_get_disk_sum(global.ShadowGrid, i + 4, j + 4, 3)) / 8, 0, 1));
 
E

eldinhelja

Guest
Did everything, the message "out of index" is not appearing anymore, but there are no shadows as well.
They weren't there when out of index message was showing up too.
 
T

ThunkGames

Guest
Is the depth of the object drawing the surface negative? Otherwise the blocks could be drawing themselves over the surface.
 
E

eldinhelja

Guest
The depth of obj_Ligting is = -1000
The depth of obj_Solidparent is = 0
 
T

ThunkGames

Guest
This is very strange. What happens when you replace this:
Code:
draw_set_alpha(clamp((ds_grid_get_disk_sum(global.ShadowGrid, i, j, 3)) / 8, 0, 1));
with this
Code:
var alpha = clamp((ds_grid_get_disk_sum(global.ShadowGrid, i, j, 3)) / 8, 0, 1);
show_debug_message(string(alpha));
draw_set_alpha(alpha);
 
E

eldinhelja

Guest
This is very strange. What happens when you replace this:
Code:
draw_set_alpha(clamp((ds_grid_get_disk_sum(global.ShadowGrid, i, j, 3)) / 8, 0, 1));
with this
Code:
var alpha = clamp((ds_grid_get_disk_sum(global.ShadowGrid, i, j, 3)) / 8, 0, 1);
show_debug_message(string(alpha));
draw_set_alpha(alpha);
It says : 1
Edit :
I changed this :
Code:
clamp((ds_grid_get_disk_sum(global.ShadowGrid, i, j, 3)) / 8, 0, 1);
to this :
Code:
clamp((ds_grid_get_disk_sum(global.ShadowGrid, i+4, j+4, 3)) / 8, 0, 1);
 
E

eldinhelja

Guest
Multiple times, it is going on and on, until I stop the game
 
T

ThunkGames

Guest
Could you post the complete code in all events for you lighting object? I don't know why this isn't working sorry this is taking a while.
 
E

eldinhelja

Guest
Create Event
Code:
//Some variables
global.ShadowGrid = ds_grid_create((room_width / 32) + 8, (room_height / 32) + 8);
global.ShadowSurface = surface_create(room_width / 32, room_height / 32);
global.ShadowSurfaceRefresh = true;

//Set up the grid
for (var j = 0; j < room_height / 32; j++) {
    for (var i = 0; i < room_width / 32; i++) {
         if position_meeting(i * 32, j * 32, obj_SolidParent) {
            ds_grid_set(global.ShadowGrid, i + 4, j + 4, 1);
         } else {
            ds_grid_set(global.ShadowGrid, i + 4, j + 4, 0);
         }
    }
}
Draw Event
Code:
//Surfaces can cease to exist sometimes
if !surface_exists(global.ShadowSurface) {
global.ShadowSurface = surface_create(room_width / 32, room_height / 32);
global.ShadowSurfaceRefresh = true;
}

//If the surface needs to be refreshed
if global.ShadowSurfaceRefresh {
global.ShadowSurfaceRefresh = false
surface_set_target(global.ShadowSurface);
draw_clear_alpha(c_white, 0);
draw_set_color(c_black);
for (var j = 0; j < room_height / 32; j+=1) {
for (var i = 0; i < room_width / 32; i+=1) {
if ds_grid_get(global.ShadowGrid, i, j) > 0 {
//draw_set_alpha(clamp((ds_grid_get_disk_sum(global.ShadowGrid, i + 4, j + 4, 3)) / 8, 0, 1)); //This code is pretty much straight out of the game from my screenshot
var alpha = clamp((ds_grid_get_disk_sum(global.ShadowGrid, i+4, j+4, 3)) / 8, 0, 1);
show_debug_message(string(alpha));
draw_set_alpha(alpha);
draw_point(i, j);
}
}

draw_set_alpha(1);
surface_reset_target();
}
}

//If you want smooth lighting and pixel interpolation is off do texture_set_interpolation(true);
//If pixel interpolation is on and you want blocky lighting do texture_set_interpolation(false);
draw_surface_stretched(global.ShadowSurface, 0, 0, room_width, room_height);
//Make sure you set interpolatio nback to how it was if you did anything with it
Don't be sorry, I should be sorry for wasting your time :)
 
T

ThunkGames

Guest
This:
Code:
}
}

draw_set_alpha(1);
surface_reset_target();
}
}
Should be this:
Code:
}
}
}

draw_set_alpha(1);
surface_reset_target();
}
I wish I could indent.
 
E

eldinhelja

Guest
There is some PROGRESS !!! but it seems it doesnt have proper location of surface.

 
T

ThunkGames

Guest
Code:
if ds_grid_get(global.ShadowGrid, i, j) > 0 {
should be

Code:
if ds_grid_get(global.ShadowGrid, i + 4, j + 4) > 0 {
 
T

ThunkGames

Guest
1st things first: those ugly white outlines. That could be a product of clearing the surface with white, even through there is an alpha of 0. Try setting the clear color to c_black instead of c_white.

As for the surface being off, see if changing room_width in the final line of draw code to room_width - 16. I don't know why this is happening but surfaces in GM can be weird.
 
E

eldinhelja

Guest
Nice, nice, Thanks very much


So if I want to play with darkness I need to play with this code : clamp((ds_grid_get_disk_sum(global.ShadowGrid, i+4, j+4, 3)) / 8, 0, 1);
room_width-32 did the thing.

How to get darkness to be a little lower, like first object to be completely ligten and it slowly becomes blacker and blacker.
room_height+32 would maybe be it...

@ThunkGames Thank you very much for your time and patience. You helped me really well. This solved !
 
Last edited by a moderator:
T

ThunkGames

Guest
One way to "lower" darkness would be to make it so blocks with a certain amount of blank spaces around them would not be dark at all, thus reducing the darkness in the first few layers as these are exposed to more open tiles. You could do this by:
Code:
clamp(((ds_grid_get_disk_sum(global.ShadowGrid, i+4, j+4, 3)) - 4) / 4, 0, 1);
Or something like that. The - 4 means that if a block checks and has 4 blocks or less within its disk area, it will not draw any darkness (I'm dividing by 4 to compensate for lower max values). Play around with these as you see fit.

Another option is to draw the surface lower. At y of something other than 0 in draw_surface_stretched.

Glad to help but have to be off. Good luck!

-David

Edit:
If you post your complete working code as well as screenshots in the original post you made, it could really help future users :)
 
E

eldinhelja

Guest
Hello @ThunkGames,
I tried to play with clamp(...) but whatever I change, everything seems to be same.
And because of room_wdith-32, which I had to type when drawing surface stretched cause this to happen (Side-scroller game) at the end of room:

If I leave it room_width and not subtract anything then further, and further I go in my room the darkness is moved to right so it is in air, not below the block, as you can see :

obj_LIghting Create event :
Code:
//Some variables
global.ShadowGrid = ds_grid_create((room_width / 32) + 8, (room_height / 32) + 8);
global.ShadowSurface = surface_create(room_width / 32, room_height / 32);
global.ShadowSurfaceRefresh = true;

//Set up the grid
for (var j = 0; j < room_height / 32; j++) {
    for (var i = 0; i < room_width / 32; i++) {
         if position_meeting(i * 32, j * 32, obj_SolidParent) {
            ds_grid_set(global.ShadowGrid, i + 4, j + 4, 1);
         } else {
            ds_grid_set(global.ShadowGrid, i + 4, j + 4, 0);
         }
    }
}
obj_Lighting Draw Event
Code:
//Surfaces can cease to exist sometimes
if !surface_exists(global.ShadowSurface) {
    global.ShadowSurface = surface_create(room_width / 32, room_height / 32);
    global.ShadowSurfaceRefresh = true;
}

//If the surface needs to be refreshed
if global.ShadowSurfaceRefresh {
    global.ShadowSurfaceRefresh = false
    surface_set_target(global.ShadowSurface);
    draw_clear_alpha(c_black, 0);
    draw_set_color(c_black);
        for (var j = 0; j < room_height / 32; j+=1) {
            for (var i = 0; i < room_width / 32; i+=1) {
                if ds_grid_get(global.ShadowGrid, i + 4, j + 4) > 0 {
                    //draw_set_alpha(clamp((ds_grid_get_disk_sum(global.ShadowGrid, i + 4, j + 4, 3)) / 8, 0, 1)); //This code is pretty much straight out of the game from my screenshot
                    //var alpha = clamp((ds_grid_get_disk_sum(global.ShadowGrid, i+4, j+4, 3))/8, 0, 1);
                    var alpha = clamp(((ds_grid_get_disk_sum(global.ShadowGrid, i+4, j+4, 3)) - 4) / 4, 0, 0.9);
                    //show_debug_message(string(alpha));
                    draw_set_alpha(alpha);
                    draw_point(i, j);
                }
        }
    }
    draw_set_alpha(1);
    surface_reset_target();

}

//If you want smooth lighting and pixel interpolation is off do texture_set_interpolation(true);
//If pixel interpolation is on and you want blocky lighting do texture_set_interpolation(false);
draw_surface_stretched(global.ShadowSurface, 0, 0, room_width, room_height);

//Make sure you set interpolatio nback to how it was if you did anything with it
 
T

ThunkGames

Guest
The surface stretching:
The - 32 was kind of a hack. Based on the documentation and my experience the drawn width of the stretched surface should be room_width. You might have to inquire with someone with more experience.

Different looking surface:
Try using my original alpha code except dividing by a greater number like 16. You could also replace if ds_grid_get(global.ShadowGrid, i + 4, j + 4) > 0 with if ds_grid_get_min(global.ShadowGrid, i + 3, j + 3, i + 5, j + 5) > 0. This would mean that the requirement to draw a shadow tile would not be "there is a block tile here", and instead be "there are no air tiles in the 3x3 region around the tile"
 
E

eldinhelja

Guest
This
Code:
 if ds_grid_get_min(global.ShadowGrid, i + 3, j + 3, i + 5, j + 5) > 0 {

var alpha = clamp((ds_grid_get_disk_sum(global.ShadowGrid, i + 4, j + 4, 3)) / 16, 0, 1)

draw_surface_stretched(global.ShadowSurface, 0, 0, room_width, room_height);
Is causing this :

The problem is in :
Code:
 if ds_grid_get_min(global.ShadowGrid, i + 3, j + 3, i + 5, j + 5) > 0 {
 
T

ThunkGames

Guest
If you want to get rid of the visible area on the left, you could, on creating the grid, clear it with 0 and then set the region below a certain point with 1. The effect is occurring because of the padding in the grid. If you go into the help docs and search DS Grids you can figure out how to clear and set regions.

Edit:
Other than the no-shadows on the left it looks pretty good!

Also did you try changing the divider from 8 to something higher? That could make it look better but if it's too high you will never have complete darkness.
 
Last edited by a moderator:
Top