Discuss and collaborate on GML scripts

You are not logged in.

#1 Re: GML Code Help » Surfaces as a 2D lighting engine. [Solved] » 2010-04-30 13:27:30

Well, I came up with a good idea... it involves using draw_sprite_part_ext which is really hard to use when you are flipping the x scale of your sprites constantly...  So I couldn't get the bottom half of my sprite to line up perfectly with the top half.  But essentially what I was doing was creating a clone object that would draw the player's sprite at a depth below the grass, and then I would hide the bottom half of the player's sprite.  I got it to line up for one player, but a player of a different size didn't work (apparently the calculations weren't as universal as I had hoped).  I'm going to give up on this for now... it's not really worth the time I've already spent on it.

Thanks for your help again.

#2 Re: GML Code Help » Surfaces as a 2D lighting engine. [Solved] » 2010-04-30 11:39:03

I can detect whether the player is on a grass tile (they are on their own tile layer.  So if there is a tile at depth X at player.x player.y then do something) but simply drawing a grass sprite on top of them doesn't really work.  The reason being, as the player moves around on the grass, the grass would be moving with the player... and that is not exactly a convincing illusion.  I even tried an effect where when a player was on a grass tile, the tile's depth would be changed to be on top of the player.  The problem that caused is as the player is walking from one tile to the next, during the transition, up to half of the character wouldn't be covered in grass... not very pleasing on the eye. 

The ideal solution in my mind would be if I could some how take a portion of the tile layer around the player's feet and draw it as a sprite on top of the player.  But I can't find any way to isolate a specific tile depth to to capture as a sprite for redrawing...

Hmm... I did just have another idea... let me go try that...

#3 Re: GML Code Help » Surfaces as a 2D lighting engine. [Solved] » 2010-04-28 16:50:52

Actually... while I've got your attention, can I point you at another question I posted on the yoyogames forums?

I've used the method suggested by the one person who responded... and it looks awesome for when the player is standing in water... but it looks less awesome when standing in grass.  Any suggestions on how I could produce the same effect seen in those screenshots?

#4 Re: GML Code Help » Surfaces as a 2D lighting engine. [Solved] » 2010-04-27 11:36:16

Got it all in and working great.  I seem to always over complicate things.  Thanks, Xot.

#5 Re: GML Code Help » Surfaces as a 2D lighting engine. [Solved] » 2010-04-26 15:32:52

Hmm...  Wow.  Yeah, I guess that makes sense.  lol.  I'll have to give that a shot and report back.

#6 Re: GML Code Help » Surfaces as a 2D lighting engine. [Solved] » 2010-04-26 13:48:16

Okay, so I think this could actually work really well, but I need to be able to combine the light overlay with the "time of day" overlay without it drawing to the screen...  Problem is, I've ran out of "steps".

Begin Step: Draw the Black Rectangle for the light surface and the colored rectangle for the timeofday surface.
Step: In all of the light objects - draw the soft graduated lights.
End Step: In all of the light objects - draw the solid circles of absolute light.
?? Step: Combine the light surface with the time of day surface.
Draw Step: Draw the time of day surface.

Is there any way I can guarantee that something will happen after the end step but before drawing?  Trying to combine the two surfaces in the draw event right before drawing occurs looks very very very messed up.  Alternatively, is there anyway to do something in the draw even (like drawing a surface to another surface) without it being displayed on screen?

#7 Re: GML Code Help » Surfaces as a 2D lighting engine. [Solved] » 2010-04-26 11:34:32

I will give that a try and then post the results.  Thanks, Xot.

#8 GML Code Help » Surfaces as a 2D lighting engine. [Solved] » 2010-04-20 13:25:34

Replies: 11

This is more of a theory or solution question than pertaining to actual code (as my code works as well as I can get it to work right now, anyway).  Also, I am working in GM7 (my project did not like being ported to 8... so I stuck with 7)

I am fairly new to surfaces, but I really enjoy how they work so far.  Let me explain what I have going on.

I have a mask that creates a day night cycle in my 2D game.  The mask is constantly drawing a square to a surface the size of the view with a ever shifting color based on the time of day.  I then display the surface with the blend modes set to emulate "multiply" in photoshop.

Begin Step event in mask Object:

The mask does this in it's "begin step".  This allows other objects to draw to this same surface before the mask object draws it to the screen in it's draw event.  In this manner, I layer shapes on top of shapes within the same surface to emulate lighting.

So, I have a light object in my room that in it's step event draws a circle that is one color on the inside and then the outside matches the same color as the background of the surface. (see below)  Then, in the light's end step event, it draws a solid circle that matches the inside color.  This gives my light a specified area of solid color, surrounded by a gradient that blends to the surface background.  When all is said and done, it's a pretty convincing effect.

Step Event:

End Step Event:

Effect In Action:

However, a problem occurs when two lights intersect.

Problem one occurs when drawing the smooth circle in the step event.  When two circles drawn this way intersect, the one drawn last overwrites the previous one, breaking the illusion.

The second problem occurs when the lights are different colors.  Again, the one drawn last overwrites the previous one.

The above issue is exacerbated by the fact that the "blend area" around the edges of the light are constantly shifting randomly to create a "flicker" effect.  Again, by itself it is very convincing, but when intersecting with other lights, the illusion breakage is jarring.

So, here are my questions.
1) I believe I could work around both issues with level and game design, however to do so I would need to be able to control which lights get drawn first.  Is there any way to control which order objects get updated?
2) Obviously the best fix would to make all of these shapes and gradients I am drawing blend with one another... is there any possible way of doing that (without totally hosing my performance)?

Thanks for your time in advance.

#9 Re: GML Code Help » Only one instance behaving properly. [SOLVED! THANKS FLATLANDER!!] » 2009-10-06 18:23:04

Good thing I sent it to you, the problem would never have been found by the posted code.  We were declaring "busy" in the script specifically for the object rather than individual instances.

Expandexecute_string("busy = "+exeObj+".busy");

Totally unnecessary.   I commented out that line of code and everything works swimmingly!  Thanks again, Flat.  You just cured a gigantic headache.

#11 Re: GML Code Help » Only one instance behaving properly. [SOLVED! THANKS FLATLANDER!!] » 2009-10-06 14:45:18

Yeah, attacking is definitely being set independently, I've set watches on the local variables for both instances and Attacking is always being toggled independently.  As for the extra parentheses, yeah, that's not going to help, otherwise the first instance wouldn't function properly either.

#12 Re: GML Code Help » Only one instance behaving properly. [SOLVED! THANKS FLATLANDER!!] » 2009-10-06 13:05:48

...Am I not giving enough information?  Or is this problem just too bizarre?  If it would help to get a hold of our .gm7 file (it's registered only, btw) I would be willing to do that... we are pretty desperate.  Our project is stuck until we can figure out what's going on.

#13 GML Code Help » Only one instance behaving properly. [SOLVED! THANKS FLATLANDER!!] » 2009-10-04 18:26:01

Replies: 6

So I have an enemy object.  The enemy object runs a script that controls its behavior.  The core idea is that it will do one of three things.

If it is far away from the target (the player) move towards it or hang out for a bit (this is randomized so that the enemy isn't just making a b-line for the player).
If it is in range, play the attack animation.

Now, the way I've set up animations for the monster is a timeline.  The timeline controls all animations and some variables. 

So, here's where things get weird.  I have two (or more, it doesn't matter) instances of the enemy object.  They will hop/move/idle, independently and correctly.  They can even target different players from one another.  However, only the first instance that was created will actually attack. 

Here's a bit of code that shows the enemy behavior.

Expandtimer -=1;
if ((timer < 0) && (busy == 0))
    //Decide on a target
    target = instance_nearest(x,y,players);

    //If in range of target, attack it.  If not, move towards it.
    if (distance_to_point(target.x, target.y) <10 && attacking == 0)
        timeline_position = 21; //position 21 is where the attack animation begins 
        attacking = 1; //this variable is set back to 0 at the end of the attack animation in the timeline.
        busy = 1;    
        timer = 90;
        speed = 0;
        height = 0;    
        if behavior = 0 //idle
            timeline_position = 3; //position 3 is where the idle animation begins.
            walkSpeed = 0
            speed = walkSpeed
            timer = 30
        else //Hop
            timeline_position = 5; //position 5 is where the hop animation begins.
            walkSpeed = 1;
            move_towards_point(target.x, target.y, walkSpeed)
            timer = 20; //number of frames for 1 jump

So, the weird thing is I've set a watch on both instances of the object.  And attacking will get set to 1, but the timeline never finishes for that instance (I never even really see it start) so attacking stays at 1.  Sure, I can go set attacking back to 0 somewhere else, but the big problem is why is that timeline not taking off for the second instance of the object when it is working perfectly for the first instance.  And obviously the timelines can work independently of the instances because both enemies can hop towards the different players at the same time but not be perfectly in sync.

My buddy and I have been banging our heads against this problem for days, and am finally breaking down and bothering you fine people.

Thanks in advance.

#14 GML Code Help » Variable Question [Solved] » 2009-09-10 16:43:18

Replies: 4

I've written a script that will take a csv file and parse it's pieces into an array.  It works great, that's not what I need help with.

I want to make the script more modular so I can use it to create many different arrays.  The problem is, when I call the script I want to be able to name the array variable.

For example, I would call the script like this:

Expandparse_text("file.csv",arrayName); //file name, array variable to store stuff in

...and then in the script it would know to store the parsed strings into arrayName[x,y] or whatever.

Is this possible?


So, did a bit more digging myself and discovered "execute_string".  A very nice function.  Helped me get this working.

#15 Re: GML Code Help » Color to Hex problems [SOLVED] » 2009-09-09 13:53:44

Ha.  Wow.  Okay, then.  I can't believe I wasted so much time trying to do it myself.

Thanks, xot.  Very helpful as always!

#16 Re: GML Code Help » Color to Hex problems [SOLVED] » 2009-09-09 13:36:30

Okay... I apologize, I probably am not being very clear.

Here's a graphical description of what I'm trying to accomplish.

So you give the script two colors, a total number of steps, and I suppose you would need to give the script the current step you are on.
The script looks at the start and end colors, and based on the position between the two colors on the timeline (that would be the current step) returns a color value... preferably in dec rather than hex.

Merge Color would consistently give me the color in the middle... but with no animation; no way to transition smoothly from red to purple to blue with all the hues in between.

Does that clear anything up?

#17 GML Code Help » Color to Hex problems [SOLVED] » 2009-09-08 18:12:50

Replies: 5

Okay... maybe I've just been staring at this for too long but I think I'm to the point where even simple answers are eluding me.  Anyway, I need help.

Here's what I'm trying to accomplish.  I want to create a script that will accept a start color, an end color, and a number of steps.  Then, when the script is used, the color will change smoothly at the desired rate.  So if I put in Red (255,0,0 || FF0000) and I want it to fade to blue (0,0,255 || 0000FF)  each RGB value will transition towards it's end goal at a constant rate.  (so you'll have a purple color as they both hit 128 on their way in the opposite direction).

Anyway, so here's my main problem right now.  I'm using color_to_hex() to convert my start and end colors into hex values.  The problem is these values are not being padded properly.  So while Red is properly interpreted as FF0000, blue is being interpreted as FF (which is in turn interpreted as FF0000 by Game Maker);  So I need a way to properly pad each of the values... I think.  (I'm a little burned out.  Been working on this for hours.)

Anyway, let me throw some code up here...  I warn you though... it may make some of you more experienced guys absolutely nauseous.   It's really messy right now as I'm at the point where I'm just throwing stuff around to get things to work.  (It's currently set up as an object rather than a script as I experimented with core functionality.)

In the Create Event I have this:

ExpandstartColor  = c_blue;
endColor    = c_red;
newColor    = -1;
numSteps    = 128;
currentStep = 0;

Then, in the Draw Event I have this:

Expandif (newColor != endColor)
    //Debug Text Output

    //Convert the start and end colors into Hex Strings
    startColor = color_to_hex(startColor);
    endColor   = color_to_hex(endColor);

    //Ripping each of the colors for the StartColor into seperate dec values.
    startColorRed  = real(hex_to_dec(string_copy(startColor,1,2)));
    startColorGreen = real(hex_to_dec(string_copy(startColor,3,2)));
    startColorBlue   = real(hex_to_dec(string_copy(startColor,5,2)));
    //Ripping each of the colors for the End Color into seperate dec values.
    endColorRed  = real(hex_to_dec(string_copy(endColor,1,2)));
    endColorGreen = real(hex_to_dec(string_copy(endColor,3,2)));
    endColorBlue   = real(hex_to_dec(string_copy(endColor,5,2)));

    //Find the difference in the Blue Values
    blueDiff = endColorBlue - startColorBlue;
    //Find the difference in the Green Values
    greenDiff = endColorGreen - startColorGreen;
    //Find the difference in the Red Values
    redDiff = endColorRed - startColorRed;    

    //Modify each value by the difference divided by the number of steps the transition should take.    
    newColorBlue  = startColorBlue + (currentStep*(blueDiff/numSteps));
    newColorGreen = startColorGreen + (currentStep*(greenDiff/numSteps));
    newColorRed   = startColorRed + (currentStep*(redDiff/numSteps));    
    //Taking the new color values and converting them back to hex.
    newColorBlue  = dec_to_hex(newColorBlue);
    newColorGreen = dec_to_hex(newColorGreen);
    newColorRed   = dec_to_hex(newColorRed);

    //Building the string to represent the new color;
    newColor = "RRGGBB";
    newColor = string_replace(newColor,"BB",newColorBlue)
    newColor = string_replace(newColor,"GG",newColorGreen)
    newColor = string_replace(newColor,"RR",newColorRed)

    newColor = hex_to_color(newColor);

    //Set the start and end colors back from hex for the next loop.
    startColor = hex_to_color(startColor);
    endColor   = hex_to_color(endColor);

    if (currentStep = numSteps)
        newColor = endColor;



//    draw_set_color(hex_to_color(newColor));
//    draw_text(10,175,hex_to_color(newColor));    

        //Even More Debug Text
    draw_text(200,25,"Dec Blue:  "+string(endColorBlue));
    draw_text(200,50,"Dec Green: "+string(endColorGreen));
    draw_text(200,75,"Dec Red:   "+string(endColorRed));
        //More Debug Text
    draw_text(10,25,"Dec Blue:  "+string(startColorBlue));
    draw_text(10,50,"Dec Green: "+string(startColorGreen));
    draw_text(10,75,"Dec Red:   "+string(startColorRed));
        //Even More Debug Text
    draw_text(100,100,"Hex Blue:  "+string(newColorBlue));
    draw_text(100,125,"Hex Green: "+string(newColorGreen));
    draw_text(100,150,"Hex Red:   "+string(newColorRed));

Again, I'm terribly sorry about the mess...

Anyway, I know there's probably a better way to do this but I have been searching feverishly for a script made by someone more experienced and have yet to find one. 

So, if you look at this and think "My God... this is such a mess, I don't even know where to begin helping him..." rather than click away, you could post a suggestion for how I could start over using a different (hopefully more simple) method.  I'm very open to starting over so I can learn more and get something cleaner.

...or, if you happen to know of a script that does just what I'm trying to do out there somewhere on the internets, pointing me in the right direction via a link would also be appreciated.

Thanks for your time.

#18 Re: GML Code Help » Slide and Fade [Resolved! THANKS!] » 2009-08-04 12:01:24

I've never used paths before... and I'm having a bit of trouble figuring this out...  What seems to be happening is your code is creating a two point path, but when it reaches what I thought would be the end of the path, it turns around and heads back to the beginning.  path_position is interpreted as only being .5 at what should be the end of the path.  How do I create a linear path where point 1 is the start and point 2 is the end... not the middle?


Nevermind.  I added a path_set_closed(__path,0); and that fixed it.  Thanks again for your help!  The script is now working exactly how I want it to, and I think I learned a little bit.  biggrin

#19 Re: Community » Introductions » 2009-08-03 19:02:21

Hi, everyone! 

I just recently downloaded and registered GM7 and this is basically my first foray into developing my own games.  Up until January of this year I did work in the game industry as a Lead Game Designer for a shovelware factory called "Sensory Sweep".  So my skills are mostly game design, graphic design, etc... not really programming.  But I'm having a blast with Game Maker so far, and hopefully I can see at least a portion of my vision come to fruition with the help of Game Maker and you chaps smile.

#20 Re: GML Code Help » Slide and Fade [Resolved! THANKS!] » 2009-08-03 11:29:13

Ah, I haven't gotten around to playing with path's yet (still really new to GameMaker), but that make's sense.  I'll play with that.  Thanks for the help.

Board footer

Powered by FluxBB