Jump to content


Photo

Flawless platformer ally


  • Please log in to reply
17 replies to this topic

#1 Yal

Yal

    Even though the GMC may be gone, our love will prevail eternally

  • Global Moderators
  • 11774 posts
  • Version:GM:Studio

Posted 08 December 2010 - 04:45 PM

In some platformers, you want a friendly character to jump around behind the hero. E.g. a sidekick (Miles "Tails" Prower), another playable character you can change to by pressing space (Cave Game), or simply another character that doubles as an extra life when you die (Donkey Kong Country).

The problem with an ally following you around is to make sure it won't lose you by falling down somewhere and not getting up again. If enemies does that it's neglible, since after all the player's glad when enemies - that are a sort of obstacle - sorta gets rid of themselves. It's something completely different with having your healer falling down twenty storeys since she didn't jump properly... Sonic games solved this problem by having Miles Prower being able to fly, and every time he got lost he would appear flying (and being able to fly trough walls) and land himself on the very piece of ground Sonic stood at. (In the more recent games, such as Shadow The Hedgehog, the ally just respawn near you. That looks ugly, especially on moving platforms where the ally instantly falls down again)

The idea I got was... The player character can only be at "accessible" places. The player won't ever be halfway into a wall. The player will only pass trough OK places. The player's ally should be just fine if (s)he took the same path as the player...
So what if the player saved this path somehow? Let's look into expanding xprevious/yprevious into something even better... the Previous Array.

In the create event of the player we put something that defines this array:
for(c = 0;c < 20;c += 1){
 xprev[c] = x
 yprev[c] = y
}
allyoffset = image_xscale * 5
//allyoffset will make ally appear behind player when
//standing still, rather than being at the same spot

In each step (begin, normal, OR end - your choice) we update this array, as well as the allyoffset.
for(c = 18;c >= 0;c -= 1){
 xprev[c + 1] = xprev[c]
 yprev[c + 1] = yprev[c]
}
xprev[0] = x + allyoffset
yprev[0] = y
allyoffset = max(-5,min(5,allyoffset - image_xscale))
//Assuming you use image_xscale to flip the hero's
//sprite rather than using one spr_left and one spr_right
Finally, we should make the ally move to the memorized position. The ally is pretty easy to code, (s)he only needs a End Step event and nothing else:
with(obj_hero){
 other.x = xprev[19]
 other.y = yprev[19]
 //Look towards the hero by flipping sprite
 if(other.x > x){
  other.image_xscale = -1
 }
 else{
  other.image_xscale = 1
 }
}
You may of course want to have some code there to animate the ally to e.g. have jump animation whilst in mid-air and so on, but you'll figure out how to do that with the method you're using, I'm sure.

Are you concerned about problems with the hero standing on moving platforms? There's of course a fix for that as well! Assuming the hero gets the speed values of the moving platform and save them in a variable...
//Put this code in the end of the Hero Step Event where
//you update the array (the second code snippet of this topic)
for(c = 0;c < 20;c += 1){
 xprev[c] += platform_hspeed
 yprev[c] += platform_vspeed
}
...that'd simply shift the entire memorized path along the vector the platform will move, and thus the ally will follow you flawlessly.


If something's unclear, just ask ahead. But this should be kinda easy to grasp, I think. It's just 3 short pieces of code.

And YES, I've fixed the code now and it should finally work.

Edited by Yal, 14 January 2011 - 03:02 PM.

  • 0

- The above is my personal opinion and in no way representative of Yoyogames or the GMC, except when explicitly stated -

 

Open this spoiler for my games:

Spoiler

Some useful game engines, music and other resources at affordable prices:

My collection of game resources at itch.io

 

New user? Can't draw but want to look unique? You can request a new avatar in this thread!


#2 Christa

Christa

    GMC Member

  • GMC Member
  • 267 posts

Posted 08 December 2010 - 05:49 PM

I've been trying to work this out myself, actually. :lol: This didn't seem to work, though. I'm not sure why. It seems like it should work in theory, but the ally just isn't responding aside from facing "obj_hero" ... hmm.
  • 0

#3 paul23

paul23

    GMC Member

  • Global Moderators
  • 4147 posts
  • Version:GM:Studio

Posted 08 December 2010 - 06:35 PM

Problem with this method is still that your "ally" has 1 different value than you have: time.

In games where the environment changes over time (moveing platforms) this won't work!
  • 0

#4 Christa

Christa

    GMC Member

  • GMC Member
  • 267 posts

Posted 08 December 2010 - 06:43 PM

Problem with this method is still that your "ally" has 1 different value than you have: time.

In games where the environment changes over time (moveing platforms) this won't work!


I thought of that, too, but it still didn't work even when I tried it in a room that was completely blank save for "ally" and "hero."
  • 0

#5 LSnK

LSnK

    NaN

  • GMC Member
  • 1188 posts

Posted 08 December 2010 - 07:43 PM

Problem with this method is still that your "ally" has 1 different value than you have: time.

In games where the environment changes over time (moveing platforms) this won't work!

It should work fine, except it won't look right sometimes. Personally I'm willing to make that compromise for the sake of simplicity. Programming reliable AI companions is a pain in the butt and a maintainability nightmare; let's have them be as dumb and sure as possible.
  • 0

#6 Christa

Christa

    GMC Member

  • GMC Member
  • 267 posts

Posted 08 December 2010 - 07:44 PM

It should work fine, except it won't look right sometimes. Personally I'm willing to make that compromise for the sake of simplicity. Programming reliable AI companions is a pain in the butt and a maintainability nightmare; let's have them be as dumb and sure as possible.


I agree with that. It is a nightmare. But right now, it doesn't seem to be working at all. Have you been able to get it working?
  • 0

#7 Christa

Christa

    GMC Member

  • GMC Member
  • 267 posts

Posted 12 December 2010 - 05:41 AM

Has anyone managed to get this to work yet?
  • 0

#8 Yal

Yal

    Even though the GMC may be gone, our love will prevail eternally

  • Global Moderators
  • 11774 posts
  • Version:GM:Studio

Posted 13 December 2010 - 01:42 PM

Not working.

Not working? I'll check it out...


AAARGGGHHHHHHHH!!!!! I just realized I missed a crucial part of the code: updating the first array indicie with the current position. Sorry, sorry, sorry about that! Replace the end step code with this:

for(c = 18;c > 0;c -= 1){
xprev[c + 1] = xprev[c]
yprev[c + 1] = xprev[c]
}
xprev[0] = x + allyoffset
yprev[0] = y

allyoffset = max(-5,min(5,allyoffset - image_xscale))

Edited by Yal, 13 December 2010 - 01:44 PM.

  • 0

- The above is my personal opinion and in no way representative of Yoyogames or the GMC, except when explicitly stated -

 

Open this spoiler for my games:

Spoiler

Some useful game engines, music and other resources at affordable prices:

My collection of game resources at itch.io

 

New user? Can't draw but want to look unique? You can request a new avatar in this thread!


#9 Christa

Christa

    GMC Member

  • GMC Member
  • 267 posts

Posted 13 December 2010 - 06:56 PM

It's still not working - have you tested this yourself yet? If so, do you have a downloadable example? I think you may be onto something here but it seems to need tweaking.
  • 0

#10 Yal

Yal

    Even though the GMC may be gone, our love will prevail eternally

  • Global Moderators
  • 11774 posts
  • Version:GM:Studio

Posted 15 December 2010 - 06:41 AM

It's still not working - have you tested this yourself yet?

I've tested it myself, but I'm not copying the code from the file since I don't have it on this computer. The code's so simple I can remember it by heart... Or, at least I believed so.

...I surveyed the code, and I found the problem!

for(c = 18;c > 0;c -= 1){

See? This loop stops at value c = 1. It should've been...

for(c = 18;c >= 0;c -= 1){

A little symbol err. Sorry about that... :whistle: It should work find now, at least...
  • 0

- The above is my personal opinion and in no way representative of Yoyogames or the GMC, except when explicitly stated -

 

Open this spoiler for my games:

Spoiler

Some useful game engines, music and other resources at affordable prices:

My collection of game resources at itch.io

 

New user? Can't draw but want to look unique? You can request a new avatar in this thread!


#11 Christa

Christa

    GMC Member

  • GMC Member
  • 267 posts

Posted 15 December 2010 - 07:08 PM

The "ally" is now following the "hero," but it follows wild patterns around the hero instead of just staying off to the side a bit. It will fly above the hero, then fly below the hero, then shoot off the screen and disappear completely for a few minutes before eventually returning. It's very erratic.
  • 0

#12 Yal

Yal

    Even though the GMC may be gone, our love will prevail eternally

  • Global Moderators
  • 11774 posts
  • Version:GM:Studio

Posted 14 January 2011 - 03:02 PM

The "ally" is now following the "hero," but it follows wild patterns around the hero instead of just staying off to the side a bit. It will fly above the hero, then fly below the hero, then shoot off the screen and disappear completely for a few minutes before eventually returning. It's very erratic.


Hmm... are you entirely sure the ally has no gravity, collision events or similiar stuff? Is the origin of the ally in the same place as the origin of the hero (try giving them the same sprite for a while if you're unsure)?

What the f*ck?! Guess what I saw when I checked the code out again?
for(c = 18;c >= 0;c -= 1){
xprev[c + 1] = xprev[c]
yprev[c + 1] = xprev[c] }
xprev[0] = x + allyoffset
yprev[0] = y
allyoffset = max(-5,min(5,allyoffset - image_xscale))

Change that "x" to an "y" and the code will work. Seriously, facepalm. That kind of typos give me around 75% of my bugs...

And I absolutely hate them.

Sorry about all of this. I've checked out the entire code three times now and there's no more typoes or inherit glitches in it.

Edited by Yal, 14 January 2011 - 03:04 PM.

  • 0

- The above is my personal opinion and in no way representative of Yoyogames or the GMC, except when explicitly stated -

 

Open this spoiler for my games:

Spoiler

Some useful game engines, music and other resources at affordable prices:

My collection of game resources at itch.io

 

New user? Can't draw but want to look unique? You can request a new avatar in this thread!


#13 hardlylike

hardlylike

    GMC Member

  • New Member
  • 293 posts

Posted 05 November 2011 - 11:25 AM

this works like a charm:) I might use it in places where I don't have slopes or ladders. Do you know a way for it to work with slopes? atm the follower floats in the air or goes inside the slopes. And do you know how to prevent the object to go outside the ladder? Because atm it floats in the air above or under you when you are near a edge on the ladder, I changed it so it goes behind y while on the ladder.
  • 0
http://browse.deviantart.com/?qh=&section=&q=hardlylike#/d35tznj

#14 Yal

Yal

    Even though the GMC may be gone, our love will prevail eternally

  • Global Moderators
  • 11774 posts
  • Version:GM:Studio

Posted 07 November 2011 - 09:21 AM

And do you know how to prevent the object to go outside the ladder?

Set allyoffset to zero while on a ladder. Same thing should make slopes work better, too.
  • 0

- The above is my personal opinion and in no way representative of Yoyogames or the GMC, except when explicitly stated -

 

Open this spoiler for my games:

Spoiler

Some useful game engines, music and other resources at affordable prices:

My collection of game resources at itch.io

 

New user? Can't draw but want to look unique? You can request a new avatar in this thread!


#15 hardlylike

hardlylike

    GMC Member

  • New Member
  • 293 posts

Posted 09 November 2011 - 10:10 PM

And do you know how to prevent the object to go outside the ladder?

Set allyoffset to zero while on a ladder. Same thing should make slopes work better, too.


will that make the ally stop inside the player? I haven't tried it yet, I go to work early in the morning and comes home when it's night so I can only do gamemaker stuff at saturdays and sundays :ninja:
  • 0
http://browse.deviantart.com/?qh=&section=&q=hardlylike#/d35tznj

#16 Yal

Yal

    Even though the GMC may be gone, our love will prevail eternally

  • Global Moderators
  • 11774 posts
  • Version:GM:Studio

Posted 10 November 2011 - 10:08 AM

allyoffset is set to a constant times image_xscale each step, which makes the ally's target position get behind you more and more all the time until a certain max is reached. The point in this is to make the ally end up a bit behind you when you stop, as opposed to end up inside you. It's set with this transition effect to not make the ally jump a lot in one step if you stand still a long while and then change facing from right to left; the ally will rather walk there.

Obviously, you want allyoffset to rapidly approach zero when you're inside a ladder or on top of a slope object where the ally needs to be aligned.
  • 0

- The above is my personal opinion and in no way representative of Yoyogames or the GMC, except when explicitly stated -

 

Open this spoiler for my games:

Spoiler

Some useful game engines, music and other resources at affordable prices:

My collection of game resources at itch.io

 

New user? Can't draw but want to look unique? You can request a new avatar in this thread!


#17 Xommon

Xommon

    GMC Member

  • New Member
  • 4 posts

Posted 13 December 2014 - 01:07 PM

Thank you so much!! This works great!


Edited by Xommon, 13 December 2014 - 01:09 PM.

  • 0

#18 Yal

Yal

    Even though the GMC may be gone, our love will prevail eternally

  • Global Moderators
  • 11774 posts
  • Version:GM:Studio

Posted 13 December 2014 - 09:37 PM

No problem! ^__^


  • 0

- The above is my personal opinion and in no way representative of Yoyogames or the GMC, except when explicitly stated -

 

Open this spoiler for my games:

Spoiler

Some useful game engines, music and other resources at affordable prices:

My collection of game resources at itch.io

 

New user? Can't draw but want to look unique? You can request a new avatar in this thread!