Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Someone help me in GM D:  (Read 3545 times)

0 Members and 1 Guest are viewing this topic.
Someone help me in GM D:
« on: June 27, 2009, 07:26:03 pm »
  • Fight the Power
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1245
I'm currently coding an engine all by myself in Game Maker 7.0 for GB style Zelda games. I'm trying to make it as clean as possible, and maybe one day I'll release it for public use if it's good enough.
Now, I think this has got to be the 6th time I start the whole file again because of my messy coding.

So I've got a few questions:

-How do you go about defining the variables you're going to use in your game? Where do you put them? In your player object?

-How do you decide whether you're gonna be using separate scripts or make a code in your object?

-How do STRINGS work?? I've really never got around to learning this. :x I feel this can be useful.

-What do you do when you feel your code is becoming a bit messy? I personally restart everything from scratch myself but... it's getting old pretty quickly and it's barely getting any better.

I'll think about other questions as I get stuck in this mess. :P

If someone's nice enough to answer these (specially the string one) I'll be verrryyy glad and you'll have my eternal love!!! <33 :D Thanks!
« Last Edit: June 27, 2009, 07:29:18 pm by Darunia »
Logged

Currently Listening to: Mos Def - Summertime

DJvenom

super-sage
Re: Someone help me in GM D:
« Reply #1 on: June 28, 2009, 12:22:36 am »
  • Colbydude was here.
  • *
  • Reputation: +7/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2898
For variables, it's all dependent on whether you want local (object specific) or global (shared) variables. Both are declared the same.

Say you're making an RPG... Every character in your party is going to have different health, yes? So lets say in our party we have 3 characters, Healy, Stabby, and Castro.

You can either do it though the character's creation event:
Code: [Select]
Max_HP=30; HP=30;or through another object:
Code: [Select]
Healy.Max_HP=30; Healy.HP=30;

For global variables (say you want the team's strength to progress in unison)
Then it's as simple as
Code: [Select]
global.Max_HP=30; global.HP=30;and have the characters read from those variables (i.e. Healy.HP=global.HP)



When I'm coding, I'm not really neat so I can't really help you 100% here, but when I'm deciding on whether to use scripts, or make everything object-based it all depends on how many times I'm going to be using the script, and how many objects are going to be acting in that manner. If I write a movement script that only effects the player, naturally I'll just write it in the object itself. But if I'm writing a shooting script that has variable projectiles, and velocities and will be used by many monsters, I'll use a script. :P ARGUMENTS FOR THE WIN!



Much like variables, strings are declared via the x=y formula. Game Maker 6-7 also have very extensive functions for string manipulation so making text engines in it is a breeze! As for the basics, say you want a character to spout off 2 random sayings (in popup menu)
in the NPC's create event:
Code: [Select]
saying[0]="Welcome to Plainsville.";
saying[1]="W... Welcome to Plainsville.";
and in whatever event you decide to trigger this
Code: [Select]
my_saying=saying[floor(random(2))];
show_message("Townie: "+string(my_saying));

And for added fun, you could mix variables into this, and add a bit of sarcasm to your NPC's

Code: [Select]
greeted=false;
saying0="Welcome to Plainsville.";
saying1="...Didn't I welcome you already?";

and

Code: [Select]
if greeted=false { show_message("Townie: "+string(saying0)); greeted=true;}
else { show_message("Townie: "+string(saying1)); }

So after being warmly greeted by the NPC, suddenly he coldly brushes you off as a tourist ;)

If you want a GMK of any of these in action, let me know! :]
« Last Edit: June 28, 2009, 12:27:24 am by DJvenom »
Logged
I do art
I ermmm... DID do art
I do art
Re: Someone help me in GM D:
« Reply #2 on: June 28, 2009, 04:15:13 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
Also, with strings, they have to be declared using "" around the string value, otherwise GM interprets you are trying to store a value as a real. I think I'm correct in saying this, but the only time GM allows you to set a variable to a name is when you have an object/sprite/sound etc with that name, otherwise it throws up an error. It lets you do this because it just retrieves the id of the resource and sets the variable to that.
Also, when dealing with strings and reals, string() and real() are very useful.
Quote from: GM manual
real(str) Turns str into a real number. str can contain a minus sign, a decimal dot and even an exponential part.
string(val) Turns the real value into a string using a standard format (no decimal places when it is an integer, and two decimal places otherwise).
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Someone help me in GM D:
« Reply #3 on: June 28, 2009, 07:11:05 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2245
Also, use constants. Too many times have I seen people do !@#$% like,
Code: [Select]
global.facing = "left"which is inefficient and more prone to errors.

Secondly, I'd recommend using the same style as gm does for variables and functions, ie. lower_case_with_underscores.

Whenever doing a boolean comparision always use ==, instead of just =, otherwise it can get confusing at to whether you're assigning a variable or comparing it.
Always use curly brackets and to indent your code properly.

This is generally how I do it.
Code: [Select]
if (variable == value)
{
// do stuff here
}

Comment your code.
When using scripts, they can get pretty messy, particularly those with arguments, as all arguments come in the style of argument0/1/2/3/4 etc. I'd recommend at the beginning of the script that you create local variables for each argument so you can put it into context.

What do I do when my code becomes messy?
I break it down into more manageable pieces.  One key thing to remember is that game maker is event driven and also allows for inheritance, there have been too many times where I just see people have just a step event with one execute a piece of code in there, which can get messy pretty quickly.
Logged
Re: Someone help me in GM D:
« Reply #4 on: June 28, 2009, 09:27:17 am »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1633
Try to be VERY strict in your naming and ordering habits.

Example names:
script_Character_Walk
script_Character_Jump
...
sprite_Character_Walking_Left_Green_Tunic
sprite_Character_Walking_Left_Red_Tunic
...
obj_Enemy_Skeleton
obj_Enemy_Bat
parent_Enemy
parent_Enemy_Harmless
...

Or just reference by number if you feel the code will get too lengthy and/or messy. But then I strongly suggest you add comments behind EACH AND EVERY reference.
For example:
script_execute(3); // script_Character_Walk
or
script_execute(3); // Folder: Character > Movement > Walking

Name your folders in a similar fashion. Maybe also add numbers in front of the foldernames if you want to keep control over the order in which they get sorted. Commonly used items up top and others further down the list.


Well, those are my routines. I'm sure not everybody will agree with all it. XD
Logged
Re: Someone help me in GM D:
« Reply #5 on: June 28, 2009, 11:28:25 am »
  • Fight the Power
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1245
Why is it that you guys are always a ton more helpful than the GMC? D:

Anyways, that's exactly the kind of reply I was expecting. :)

The thing I have with global and local variables is that I'm never sure if I'm gonna be using the same code again with another object, so I always make them global... and it gets confusing after a bit.

I'll check out scripts now that I know how they work, roughly.

I ALWAYS comment my code. Infact, I comment it so much that I get lost in it. I'll show you guys one of the scripts from the current engine so you can see what I mean. When I comment I do it in a way I can easily understand what I mean, so other people would have a hard time understanding it sometimes.

I AM strict with my naming and ordering habits. :P I always use underscores and low case letters.

Windy, what do you mean by constants?
Right now I'm using a variable called global.facing... D: and it does the exact same thing you said. :P


Anyways, thanks a LOT for your replies guys. :] Oh I almost forgot, here's a sample of the way I code:

Code: [Select]
//When you press the UP Arrow Key, the variable global.holdu turns to 1.
if keyboard_check(vk_up) and global.donotgobacktoup=0 and global.sword=0{ 
global.holdu=1
}
//else, it turns back to 0
else if keyboard_check_released(vk_up) global.holdu=0 global.alreadyholdu=0 global.donotgobacktoup=0 global.push=0
//When you press the DOWN Arrow Key, the variable global.holdd turns to 1.
if keyboard_check(vk_down) and global.donotgobacktodown=0 and global.sword=0{
global.holdd=1
}
//else, it turns back to 0
else if keyboard_check_released(vk_down) global.holdd=0 global.alreadyholdd=0 global.donotgobacktodown=0 global.push=0

//When you press the RIGHT Arrow Key, the variable global.holdr turns to 1.
if keyboard_check(vk_right) and global.donotgobacktoright=0 and global.sword=0{
global.holdr=1
}
//else, it turns back to 0
else if keyboard_check_released(vk_right) global.holdr=0 global.alreadyholdr=0 global.donotgobacktoright=0 global.push=0

//When you press the LEFT Arrow Key, the variable global.holdl turns to 1.
if keyboard_check(vk_left) and global.donotgobacktoleft=0 and global.sword=0{
global.holdl=1
}
//else, it turns back to 0
else if keyboard_check_released(vk_left) global.holdl=0 global.alreadyholdl=0 global.donotgobacktoleft=0 global.push=0

//When you press the SPACE Key, the variable global.sword turns to 1.
if keyboard_check(vk_space) and global.sword=0 and global.donotanimswordagain=0{
if global.push=0{
global.sword=1
script_execute(sword_script)
obj_link.alarm[0]=7
global.donotanimswordagain=1
//Create the actual sword
instance_create(obj_link.x-16,obj_link.y-16,obj_sword)
}}
//When you release the SPACE Key, the variable global.donotanimswordagain turns to 0.
else if keyboard_check_released(vk_space) global.donotanimswordagain=0

Code: [Select]
//Choose the right sprite with 90 degree movement

if keyboard_check(vk_right) or keyboard_check(vk_left) and global.holdu=1{
global.alreadyholdu=1
}
if keyboard_check(vk_right) or keyboard_check(vk_left) and global.holdd=1{
global.alreadyholdd=1
}
if keyboard_check(vk_up) or keyboard_check(vk_down) and global.holdr=1{
global.alreadyholdr=1
}
if keyboard_check(vk_up) or keyboard_check(vk_down) and global.holdl=1{
global.alreadyholdl=1
}

//Choose the right sprite with 180 degree movement

if global.facing="up" and keyboard_check(vk_down){
global.holdu=0
global.holdd=1
global.facing="down"
global.donotgobacktoup=1
}
if global.facing="down" and keyboard_check(vk_up){
global.holdd=0
global.holdu=1
global.facing="up"
global.donotgobacktodown=1
}
if global.facing="left" and keyboard_check(vk_right){
global.holdl=0
global.holdr=1
global.facing="right"
global.donotgobacktoleft=1
}
if global.facing="right" and keyboard_check(vk_left){
global.holdr=0
global.holdl=1
global.facing="left"
global.donotgobacktoright=1
}

Logged

Currently Listening to: Mos Def - Summertime
Re: Someone help me in GM D:
« Reply #6 on: June 28, 2009, 12:20:02 pm »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1633
Personally I'd rename those scripts of yours so that each starts with the indication that it's a script. You'll have a harder time figuring out what every is later on when it's more than just a handfull. Also consider how you're code will look if you haven't worked on it for two months or so.

Here's you're code with my interpretation of Windy's layout suggestion.

Code: [Select]
//Choose the right sprite with 90 degree movement

if (keyboard_check(vk_right) or (keyboard_check(vk_left) and global.holdu = 1))
{
      global.alreadyholdu = 1;
};

if (keyboard_check(vk_right) or (keyboard_check(vk_left) and global.holdd = 1))
{
      global.alreadyholdd = 1;
};

if (keyboard_check(vk_up) or (keyboard_check(vk_down) and global.holdr = 1))
{
      global.alreadyholdr = 1;
};

if (keyboard_check(vk_up) or (keyboard_check(vk_down) and global.holdl = 1))
{
      global.alreadyholdl = 1;
};

//Choose the right sprite with 180 degree movement
if (global.facing="up" and keyboard_check(vk_down))
{
    global.holdu = 0;
    global.holdd = 1;
    global.facing = "down";
    global.donotgobacktoup = 1;
};

if (global.facing="down" and keyboard_check(vk_up))
{
    global.holdd = 0;
    global.holdu = 1;
    global.facing = "up";
    global.donotgobacktodown = 1;
};

if (global.facing="left" and keyboard_check(vk_right))
{
   global.holdl = 0;
   global.holdr = 1;
   global.facing = "right";
   global.donotgobacktoleft = 1;
};

if (global.facing="right" and keyboard_check(vk_left))
{
  global.holdr = 0;
  global.holdl = 1;
  global.facing = "left";
  global.donotgobacktoright = 1;
};
Logged
Re: Someone help me in GM D:
« Reply #7 on: June 28, 2009, 12:57:24 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
yeah, I'd rename them as well, maybe prefix them with scr_ or something. The only time I don't bother with that is for common actions that return things and I want to use them over and over again, such as text engines. Then I just name them like a function, so I can call textbox(arguments) rather than scr_textbox(arguments).
And make sure you don't make scripts for every bit of code, as they're slower than just using code blocks. A script has to process arguments, if they're there or not. It's just how GM's coded.
Why is it that you guys are always a ton more helpful than the GMC? D:
Cause it's full of pros, noobs, and stuck up !@#$%. The pro's don't bother helping noobs, the noobs need help, and the stuck up !@#$% do their job.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Someone help me in GM D:
« Reply #8 on: June 28, 2009, 01:21:38 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2245
99% of the time you will never need globals.

Secondly, some of your comments are too literal.
Code: [Select]
//When you press the UP Arrow Key, the variable global.holdu turns to 1.This doesn't tell me anything new that I can't already see, what's the importance of global.holdu and what does turning it to 1 actually signify?, chances are you can't explain it then you're probably doing it wrong.

I don't particularly see how you can say with your naming and ordering habits, nearly all the variables in the scripts you've shown have no underscores.

Constants are similar to variables, only you can't change their value.  They are best defined using capital letters so you can easily differentiate them from variables, eg. FACING_LEFT. You change these in your global game settings.

Overall I'd say your coding is pretty crap.
Logged
Re: Someone help me in GM D:
« Reply #9 on: June 28, 2009, 01:41:47 pm »
  • Fight the Power
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1245
@Martijn
Quote
Here's you're code with my interpretation of Windy's layout suggestion.
Thanks for that! I'll try to keep my code looking exactly like that.

Quote
Personally I'd rename those scripts of yours so that each starts with the indication that it's a script.
Oh yes, totally right. I forgot about a spr_ in the sprites aswel, gotta fix that.

@Darklight
Quote
And make sure you don't make scripts for every bit of code, as they're slower than just using code blocks. A script has to process arguments, if they're there or not. It's just how GM's coded.
I only made it this way because it seemed to me that it was more organised, but if it makes it slower, I guess I'll just make an execute code in the Step event with fewer scripts then.

@Windy

Quote
Secondly, some of your comments are too literal.
This is true, now that I look at it, it doesn't help me at all. Looking at the code helps just as much as looking at the comment. I'll have to keep that in mind when I write this over again.

Quote
Constants are similar to variables, only you can't change their value.  They are best defined using capital letters so you can easily differentiate them from variables, eg. FACING_LEFT. You change these in your global game settings.
Hmm I'll have to check out how that works, thanks.

Quote
I don't particularly see how you can say with your naming and ordering habits, nearly all the variables in the scripts you've shown have no underscores.
Ugh. I must have forgot about that one, but what I meant is that I have a specific order for actions like in... when I'm talking about directions, I put up first, then down, then right and finally left. Always like that. Little things like that.

Quote
Overall I'd say your coding is pretty crap.
Well everybody's gotta start somewhere, no?  :-\

Thanks again.
Logged

Currently Listening to: Mos Def - Summertime
Re: Someone help me in GM D:
« Reply #10 on: June 28, 2009, 02:24:04 pm »
  • Fight the Power
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1245
I'm terribly sorry for double posting, but before I start coding this whole thing over again, I need someone to answer a few questions.

1)When exactly do you use the symbol << ; >> ?
2)When do you use () brackets or []? - I've seen this in other people's codes.
3)I kept in mind what you told me Windy, I'm using == instead of = now. But should I use = then? Edit: This makes the game crash, I'm forced to use a single =. :S
4)What's the difference between using && and 'and'?

And more importantly,

5)This is tormenting me. When do you use these:for, while

6)How exactly do you use strings? Do you just put in, for example:
Code: [Select]
execute_string("spr_")+execute_string("index") = spr_link_walk_up
Edit: Oh I actually found an answer to the 4th one. Is this true?:
Quote
You can use && only in arguments, like:
CODE
if(argument1 && argument2)
{}
while(argument1 && argument2)
{}

You can't use it inside the {}

Another question, take a look at this code:

Code: [Select]
//This variable tells me if the Up Arrow Key is being pressed,
if (keyboard_check(vk_up))
{

key_pressed_up = 1;

}
//or if it isn't being pressed.
else if (keyboard_check_released(vk_up))
{

key_pressed_up = 0;

}

//This is to check if the Down Arrow Key is being held down or not:
if (keyboard_check(vk_down))
{

key_pressed_down = 1;

}

The part where it says <<if (keyboard_check(vk_down))>> do I need to put an 'else' in there?
« Last Edit: June 28, 2009, 03:01:25 pm by Darunia »
Logged

Currently Listening to: Mos Def - Summertime
Re: Someone help me in GM D:
« Reply #11 on: June 28, 2009, 03:09:04 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2245
you use ; at the end of a statement.
Code: [Select]
variable = 1;

you use () whenever your executing a function (which you're probably already aware of), the other reason to use them is to assign priority to an expression same as in mathematics.

[] is used for arrays

if you're making a comparison you should use ==, otherwise if you assigning a variable you use =
eg. lets say "variable" is equal to 5.
Code: [Select]
variable = 1 && variable = 4; // would assign 4 to variable then it would end up being (1 && 4 as 4 is now the value of variable) which are both above 0 and thus, would return 1
variable = 1 && variable == 4; // would compare variable to 4, as variable is 5 would that would return false so it would end up being (1 && 0) and thus would return 0
edit: seems this is not the case in game maker, it still treats it as a comparison in this case, but in any real programming language, this is how it would work.

there is no difference between && and 'and', its really a matter of preference, if you intend to use another language in the future, say c/c++ you're better off using &&, otherwise if you like vb you should use 'and', either way as I said, its down to preference.
« Last Edit: June 28, 2009, 03:15:37 pm by Windy »
Logged
Re: Someone help me in GM D:
« Reply #12 on: June 28, 2009, 05:26:41 pm »
  • Fight the Power
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1245
Oh okay, thanks.

Okay, update. I rewrote most of the engine after rereading through the whole thread.
Now, I wanted to show you guys the code I have right now and maybe you could help me fix it?

It turned out to be very long...

Code: [Select]
        //  //  WHAT HAPPENS 'WHEN' YOU PRESS A KEY?
       
    //This variable tells me if the Up Arrow Key is being pressed,
if (keyboard_check(vk_up))
{

key_pressed_up = 1;

////BTW; this is here to PRIORITIZE movement:
    if key_pressed_down = 1
    {
   
    key_pressed_down = 0;
   
    };

};

    //or if it isn't being pressed.
else if (keyboard_check_released(vk_up))
{

key_pressed_up = 0
walking = 0
colliding = 0
sprite_index = spr_link_walk_up
image_speed = 0
image_index = 0;

};

    //This is to check if the Down Arrow Key is being pressed,
if (keyboard_check(vk_down))
{

key_pressed_down = 1;

////BTW; this is here to PRIORITIZE movement:
    if key_pressed_up = 1
    {
   
    key_pressed_up = 0;
   
    };

};

    //or if it isn't being pressed.
else if (keyboard_check_released(vk_down))
{

key_pressed_down = 0
walking = 0
colliding = 0
sprite_index = spr_link_walk_down
image_speed = 0
image_index = 0;

};

    //This is to check if the Right Arrow Key is being pressed,
if (keyboard_check(vk_right))
{

key_pressed_right = 1;

////BTW; this is here to PRIORITIZE movement:
    if key_pressed_left = 1
    {
   
    key_pressed_left = 0;
   
    };

};

    //or if it isn't being pressed.
else if (keyboard_check_released(vk_right))
{

key_pressed_right = 0
walking = 0
colliding = 0
sprite_index = spr_link_walk_right
image_speed = 0
image_index = 0;

};
    //This is to check if the Left Arrow Key is being pressed,
if (keyboard_check(vk_left))
{

key_pressed_left = 1;

////BTW; this is here to PRIORITIZE movement:
    if key_pressed_right = 1
    {
   
    key_pressed_right = 0;
   
    };

};

    //or if it isn't being pressed.
else if (keyboard_check_released(vk_left))
{

key_pressed_left = 0
walking = 0
colliding = 0
sprite_index = spr_link_walk_left
image_speed = 0
image_index = 0;

};


        //  //  WHAT HAPPENS 'AFTER' YOU PRESSED A CERTAIN KEY?

    //If you previously pressed the Up Arrow Key,
if (key_pressed_up)
{

facing = "up"

    if place_free(x,y-1)
    {
   
        //I'm giving priority to left and right sprites here:
        if !key_pressed_right and !key_pressed_left
        {
       
        y -= 3
        sprite_index = spr_link_walk_up
       
        }
       
        else if key_pressed_right
        {
        y -= 2
       
            if place_free(x+1,y)
            {
           
            x += 2
           
            }
        }
       
        else if key_pressed_left
        {
        y -= 2
       
            if place_free(x-1,y)
            {
           
            x -= 2
           
            }
           
       
        }
       
    image_speed = 0.25
    walking = 1;
   
    };

    else if !place_free(x,y-1)
    {
   
    sprite_index = spr_link_push_up
    image_speed = 0.25
    colliding = 1;
   
    };

};

    //If you previously pressed the Down Arrow Key,
if (key_pressed_down)
{

facing = "down"

    if place_free(x,y+1)
    {
   
            //I'm giving priority to left and right sprites here:
        if !key_pressed_right and !key_pressed_left
        {
       
        sprite_index = spr_link_walk_down
        y += 3
       
        }
       
        else if key_pressed_right
        {
        y += 2
       
            if place_free(x+1,y)
            {
           
            x += 2
           
            }
           
        }
       
        else if key_pressed_left
        {
        y += 2
       
            if place_free(x-1,y)
            {
           
            x -= 2
           
            }
       
        }
       
    image_speed = 0.25
    walking = 1;
   
    };

    else if !place_free(x,y+1)
    {
   
    sprite_index = spr_link_push_down
    image_speed = 0.25
    colliding = 1;
   
    };

};

    //If you previously pressed the Right Arrow Key,
if (key_pressed_right)
{

facing = "right"

    if place_free(x+1,y)
    {
   
            //I'm giving priority to up and down sprites here:
        if !key_pressed_up and !key_pressed_down
        {
       
        sprite_index = spr_link_walk_right
        x += 3
       
        }
       
    image_speed = 0.25
    walking = 1;
   
    };

    else if !place_free(x+1,y)
    {
   
    sprite_index = spr_link_push_right
    image_speed = 0.25
    colliding = 1;
   
    };

};

    //If you previously pressed the Left Arrow Key,
if (key_pressed_left)
{

facing = "left"

    if place_free(x-1,y)
    {
   
                //I'm giving priority to up and down sprites here:
        if !key_pressed_up and !key_pressed_down
        {
       
        sprite_index = spr_link_walk_left
        x -= 3;
       
        };
       
    image_speed = 0.25
    walking = 1;
   
    };

    else if !place_free(x-1,y)
    {
   
                //I'm giving priority to up and down sprites here:
        if !key_pressed_up and !key_pressed_down
        {
       
        sprite_index = spr_link_walk_left
       
        }
       
    sprite_index = spr_link_push_left
    image_speed = 0.25
    colliding = 1;
   
    };


*sigh* What do you think? I know I'm missing alot of things, like brackets and ;'s but this will become an habit I suppose. There is a bug with collisions, though. I uploaded the GMK to show you guys what the problem is.

http://www.box.net/shared/u9hmg5qjlf

It works fine when you collide against a wall and you're facing the left but for the other directions, it gets stuck. I really don't see what I'm missing here.
Logged

Currently Listening to: Mos Def - Summertime
Re: Someone help me in GM D:
« Reply #13 on: June 28, 2009, 07:46:35 pm »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1633
I strongly suggest you look up 'switch'. Or check out the example a little down below.

Movement is pretty basic for all games and it kinda bugs me when I'm playing something where it's off. So I'll give you the short version of how I did it. Not that that's the best way, but it works.

First I create one object to store the variables: pressed_first, pressed_second, pressed_third, pressed_fourth. This can be in your character object or some higher control object. Then I add a press button event per directional button in that object. If a direction is being pressed something like this kicks in:

Code: [Select]
if (pressed_first = 0)
{
   pressed_first = [insert direction];
}
elseif (pressed_second = 0)
{
   pressed_second = pressed_first;
   pressed_first = [insert direction];
}
elseif (pressed_third = 0)
{
   pressed_third = pressed_second;
   pressed_second = pressed_first;
   pressed_first = [insert direction];
}
else
{
   pressed_fourth = pressed_third;
   pressed_third = pressed_second;
   pressed_second = pressed_first;
   pressed_first = [insert direction];
}}};

Then I add a release button event for the diretion buttons with something like this:

Code: [Select]
if (pressed_first = [insert direction])
{
   pressed_first = pressed_second;
   pressed_second = pressed_third;
   pressed_third = pressed_fourth;
   pressed_fourth = 0;
}
elseif (pressed_second = [insert direction])
{
   pressed_second = pressed_third;
   pressed_third = pressed_fourth;
   pressed_fourth = 0;
}
elseif (pressed_third = [insert direction])
{
   pressed_third = pressed_fourth;
   pressed_fourth = 0;
}
else
{
   pressed_fourth = 0;
}}};

Now I can do all sorts of things. I'm guessing you want something along the line of the code below. That is for 4 directional movement. If you want 8 then just use a switch(pressed_second) within that one.

Code: [Select]
switch (pressed_first)
{
  case vk_left:
    // move left
    break;
  case vk_up:
    // move up
    break;
  case vk_right:
    // move right
    break;
  case vk_down:
    // move down
    break;
};

Hope it helps.
Logged
Re: Someone help me in GM D:
« Reply #14 on: June 28, 2009, 07:59:54 pm »
  • Fight the Power
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1245
Martijn, that's exactly the kind of logic that I was trying to apply! Thanks alot, now I'll just study your code and then start mine over again. :D

Quote
Movement is pretty basic for all games and it kinda bugs me when I'm playing something where it's off. So I'll give you the short version of how I did it. Not that that's the best way, but it works.

This is why I'm spending so much time making this perfect before moving on.
On my first engine, code was messy, movement was a bit buggy but I had already made lots of other features, like screen scrolling, walking up stairs and such. But I started again and again and again, and will keep doing it til it is the way I want it to be. :)
Logged

Currently Listening to: Mos Def - Summertime
Re: Someone help me in GM D:
« Reply #15 on: June 29, 2009, 08:29:11 pm »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1633
Sounds like your code also got messy through irregular goals.

Quality > Quantity

Don't get me wrong. I started just like you and failed on my first attemp at making my own Zelda game. Then I took about 3,5 weeks just to get the movement perfected down to the last pixel. After that I added the item menu and map menu. Then stuff like pauze, text, room transitions and actual rooms. It's not very rewarding in the beginning. To not do flashy stuff like sword swinging, enemies etc. Heck, I've only got a tech demo working. After a while however it's totally worth it.

One last piece of advice:
Try to keep a log of every single variable you use and add 1 or 2 words to mention what it does. Gamemaker doesn't have functions to neatly list your variables. Nor can you search code other than scripts. To my knowledge anyway. I just have the advanced version so I might be missing out XD
When you get further ahead in your project than a list of variables per object is priceless.
Logged
Re: Someone help me in GM D:
« Reply #16 on: July 21, 2009, 10:05:01 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 183
Why so much scripts ????? My entire OoT2D has 4 scripts.... (2 for saving/loading)
Logged

Mamoruanime

@Mamoruanime
Re: Someone help me in GM D:
« Reply #17 on: July 21, 2009, 11:41:11 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
In terms of globals I almost always have a master object (in GM anyway) that I reference to so there's generally no need for globals to begin with. Typically the master object I have creates the character, handles the camera, etc.

@DJ- Healy isn't as cool as Stabby
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.331 seconds with 90 queries.