Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Need some GML programming help...  (Read 2141 times)

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Need some GML programming help...
« on: August 27, 2009, 02:28:42 am »

Sorry to make another topic about the game I'm working on, but I need some help with the programming in order to meet my self imposed deadline.

What I need to do is get the zombies moving on the 32x32 grid, without getting out of the grid and without running through stuff... They also need to home in (for now) on the nearest human object.

I also need to find a way of getting the zombie turn to end, but I'm unsure of how to set up the following count (if zombiemove/zombiecount = 1, then turn =0).
More specifically, I need to find out how to count the zombie instances.

Just figured out how to do this.

Once I get that done, I'll go ahead and release the first version of the game.

(The reason I locked the sprite thread is because I have all the sprites I need for now, if I need more I'll unlock it and bump it, or maybe even make a new thread.)
« Last Edit: August 27, 2009, 10:23:04 am by chaoticag »
Logged

Derakon

  • Bay Watcher
    • View Profile
Re: Need some GML programming help...
« Reply #1 on: August 27, 2009, 11:36:43 am »

Let's start with the homing, because "keeping objects on the grid" and "keeping objects from running through stuff" is logic you'll need in multiple places.

Let's say the zombies are on the grid at location (x, y). And the nearest human object is at location (a, b). You can create a vector from (x, y) to (a, b) by subtracting (x, y) from (a, b). This vector is the amount you'd need to add to the zombies' location for them to be on top of the human.

For example, say the zombies are at (5, 8) and the human is at (12, 24). We subtract (5, 8) from (12, 24), getting (7, 16). And indeed, if you were to add (7, 16) to the zombies' location you'd be on top of the human.

Now of course, the zombies can't move that fast. Presumably they can only move one square at a time, and let's assume for now that they can't move diagonally. So take a look at that movement vector (7, 16). If they could only move in one direction, which direction would it be to get them closer to the human? They'd want to move downwards (assuming as always that Y distance increases as you go down, and X distance increases as you go right).

Of course, vectors can have negative components. If the human was to the left of the zombies, for example, then you might have a movement vector of (-8, 4). And even though -8 is smaller than 4, it's still the direction you want to move in. So you need to be careful to look at the magnitude of the movement vector, not just its amount.
Logged
Jetblade - an open-source Metroid/Castlevania game with procedurally-generated levels

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Need some GML programming help...
« Reply #2 on: August 27, 2009, 11:52:38 am »

Let's start with the homing, because "keeping objects on the grid" and "keeping objects from running through stuff" is logic you'll need in multiple places.

Let's say the zombies are on the grid at location (x, y). And the nearest human object is at location (a, b). You can create a vector from (x, y) to (a, b) by subtracting (x, y) from (a, b). This vector is the amount you'd need to add to the zombies' location for them to be on top of the human.

For example, say the zombies are at (5, 8) and the human is at (12, 24). We subtract (5, 8) from (12, 24), getting (7, 16). And indeed, if you were to add (7, 16) to the zombies' location you'd be on top of the human.

Now of course, the zombies can't move that fast. Presumably they can only move one square at a time, and let's assume for now that they can't move diagonally. So take a look at that movement vector (7, 16). If they could only move in one direction, which direction would it be to get them closer to the human? They'd want to move downwards (assuming as always that Y distance increases as you go down, and X distance increases as you go right).

Of course, vectors can have negative components. If the human was to the left of the zombies, for example, then you might have a movement vector of (-8, 4). And even though -8 is smaller than 4, it's still the direction you want to move in. So you need to be careful to look at the magnitude of the movement vector, not just its amount.
Thanks, so all I have to do to determine which human is closer to which zombie is through this vector method as well?

I need to dig up my math notes now, can't seem to remember the magnitude formula.
Logged

Psyco Jelly

  • Bay Watcher
  • It begins!
    • View Profile
Re: Need some GML programming help...
« Reply #3 on: August 27, 2009, 03:35:20 pm »

Just use the absolute value. So if the vector is (3, -5) absolute value will make the values (3, 5). Since 5 is greater than three, add (0, -1).
« Last Edit: August 27, 2009, 03:37:03 pm by Psyco Jelly »
Logged
Not only is it not actually advertising anything, it's just copy/pasting word salads about gold, runescape, oil, yuan, and handbags.  It's like a transporter accident combined all the spambots into one shambling mass of online sales.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Need some GML programming help...
« Reply #4 on: August 27, 2009, 07:18:43 pm »

Just use the absolute value. So if the vector is (3, -5) absolute value will make the values (3, 5). Since 5 is greater than three, add (0, -1).

That won't work, since you need to keep the sing information. After all, if you convert everything to absolutes, then the vecotrs are always in the same quadrant (in this case the first one, or the lower rigth quarter of an axis system with Y pointing down and X to the right), so your zombie is always going towards the lower right.

If the direction vector is (-5; -9) then you'll need to move up and left, so you need movement vectors of (-1; 0) or (0; -1)
« Last Edit: September 01, 2009, 08:03:06 am by Virex »
Logged

Psyco Jelly

  • Bay Watcher
  • It begins!
    • View Profile
Re: Need some GML programming help...
« Reply #5 on: August 27, 2009, 08:15:13 pm »

No, no, I mean use the absolute value to determine whether or not to go in that direction. Like so:

if abs(x_vector) > abs(y_vector)
{
  if x_vector < 0 x -= 1;
  if x_vector > 0 x += 1;
}
if abs(x_vector) < abs(y_vector)
{
  if y_vector < 0 y -= 1;
  if y_vector > 0 y += 1;
}

This would be called whenever a zombie moves. y_vector would be set to the y vector, in case you don't know.
Logged
Not only is it not actually advertising anything, it's just copy/pasting word salads about gold, runescape, oil, yuan, and handbags.  It's like a transporter accident combined all the spambots into one shambling mass of online sales.

Poltifar

  • Bay Watcher
    • View Profile
Re: Need some GML programming help...
« Reply #6 on: August 31, 2009, 08:07:26 pm »

And once he gets the vector for each human relative to the zombie, would he calculate the 'length'/magnitude of all the vectors (using Pythagorean theorem to calculate each?) and compare them to each other to find the shortest one and thus the closest human? What would happen if 2 humans are at the exact same distance from the zombie?

And of course, what would he do once he implements obstacles? We cant have zombies bashing into walls to get the humans on the other side (unless in appropriate cases).
« Last Edit: August 31, 2009, 08:09:30 pm by Poltifar »
Logged
Quote
<@Poltifar> yeah i've played life for almost 23 years
<@Poltifar> i specced myself into a corner, i should just reroll
<@Akroma> eh
<@Akroma> just play the minigames until your subscription runs out

Psyco Jelly

  • Bay Watcher
  • It begins!
    • View Profile
Re: Need some GML programming help...
« Reply #7 on: August 31, 2009, 09:20:07 pm »

I suppose if two humans were equally far away, a coin would be flipped. a 50% chance of going to one or the other. Of course, if the humans move right, the zombie could shamble around in circles, but zombies aren't smart.
Logged
Not only is it not actually advertising anything, it's just copy/pasting word salads about gold, runescape, oil, yuan, and handbags.  It's like a transporter accident combined all the spambots into one shambling mass of online sales.

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Need some GML programming help...
« Reply #8 on: September 01, 2009, 10:39:13 am »

And once he gets the vector for each human relative to the zombie, would he calculate the 'length'/magnitude of all the vectors (using Pythagorean theorem to calculate each?) and compare them to each other to find the shortest one and thus the closest human?
I found a way to get the closest humans using GML, but I have three different types of humans, so I'm taking the distance formula of each of the three types and having it choose between the closest instance.

Quote
What would happen if 2 humans are at the exact same distance from the zombie?
It will "prefer" one direction over another, although I haven't given it a preference yet. You probably would try to pull off the "put the zombie in the middle of moving people technique" but it falls apart as soon as you realise you are out numbered. I don't plan on having respawns yet, but if people are foling around then I might as well have a wave spawn offscreen every X turns.


Quote
And of course, what would he do once he implements obstacles? We cant have zombies bashing into walls to get the humans on the other side (unless in appropriate cases).
I'll just have to write a code for that, but in the initial version a building would probably only give a bonus to defense, or even make you have to engage in CQC to drive them out.
Logged

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Need some GML programming help...
« Reply #9 on: September 02, 2009, 06:01:18 am »

Ooookay...
For some reason, the code I wrote makes my zombies move downwards by 32 pixels.
Can anyone see what is wrong? It is a pretty long (104 lines) of code.

Spoiler (click to show/hide)
Logged

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Need some GML programming help...
« Reply #10 on: September 02, 2009, 11:10:50 am »

So yeah, problem solved. The details for those interested:
Spoiler (click to show/hide)
Logged

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Need some GML programming help...
« Reply #11 on: September 03, 2009, 06:31:21 am »

Aaaaand bug number 2, game crashes once a unit type no longer is on the map. (one of these days I'll have to work on having only one type of unit as far as the game is concerned.)
Logged

timmeh

  • Bay Watcher
    • View Profile
    • My Portfolio
Re: Need some GML programming help...
« Reply #12 on: September 06, 2009, 11:38:12 am »

So basicly, if you remove the zombie from the room/map it crashes?  It may be that something is trying to mess with it and can't find it... it's been a while for me, but I believe there is a GML command for checking if an object exists.  You'll want to make sure there is at least one of an object before trying to change/move/etc. it.
Logged
On the Wall is a Masterfully engraved carving of Urist McHardcastle and Goblins. Urist McHardcastle is surrounded by the Goblins. The Golbins are stamping on Urist McHardcastle. Urist McHardcaste is laughing at the Goblins. The carving related to the prolonged and bloody death of Urist McHardcastle in the Fall of 1659, the Winter of 1659, and the Spring of 1660. On the engraving is an image of Cheese.

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Need some GML programming help...
« Reply #13 on: September 06, 2009, 12:48:35 pm »

So basicly, if you remove the zombie from the room/map it crashes?  It may be that something is trying to mess with it and can't find it... it's been a while for me, but I believe there is a GML command for checking if an object exists.  You'll want to make sure there is at least one of an object before trying to change/move/etc. it.
Damn, there is? and I was wasting my time trying to get my game to count. It wasn't going well. I'll look for that command in the help files.
Logged

timmeh

  • Bay Watcher
    • View Profile
    • My Portfolio
Re: Need some GML programming help...
« Reply #14 on: September 06, 2009, 01:03:31 pm »

Code: [Select]
instance_number(obj) Returns the number of instances of type obj. obj can be an object or the keyword all.
I wouldn't consider it a waste of your time though, I'm trying to learn C++, and if I'd been doing some of that sort of thing by hand instead of using shortcuts provided by the program I might be having an easier time...
Logged
On the Wall is a Masterfully engraved carving of Urist McHardcastle and Goblins. Urist McHardcastle is surrounded by the Goblins. The Golbins are stamping on Urist McHardcastle. Urist McHardcaste is laughing at the Goblins. The carving related to the prolonged and bloody death of Urist McHardcastle in the Fall of 1659, the Winter of 1659, and the Spring of 1660. On the engraving is an image of Cheese.