Example code of Platformer AI

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Example code of Platformer AI

Post by Gunroar:Cannon() »

Does Anyone know of anywhere I can see any type of example code (lua preferably, but others are fine to) of a platformer AI?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Example code of Platformer AI

Post by MrFariator »

What kind of AI do you need, specifically? If you mean enemy AI, then often those are driven by querying player's position in some fashion (eq. to check which direction to attack, when to awaken and become aggressive, etc), and some sensors (an enemy that might jump over obstacles queries the world for walls, and jumps over them when appropriate), and so forth. On the latter, here's an example gif from the recently released Cyber Shadow by its developer.

Of course, it's going to depend a bit on the game what you might want to do. What I described works for action platformers like Mega Man that have fairly simple enemy AI, but if a game needs some sort of pathfinding then it's going to get a bit more involved.
skabz_cool
Prole
Posts: 1
Joined: Mon Feb 22, 2021 7:59 pm

Re: Example code of Platformer AI

Post by skabz_cool »

Code: Select all

function skabz:survive()
  if covid.cases > 0 then
    stay_at_home = true
  end
end
User avatar
togFox
Party member
Posts: 763
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Example code of Platformer AI

Post by togFox »

My top down game (not a platformer) uses decision trees implemented as deeply nested if/then statements.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Example code of Platformer AI

Post by Gunroar:Cannon() »

MrFariator wrote: Sun Mar 21, 2021 6:44 pm What kind of AI do you need, specifically? If you mean enemy AI, then often those are driven by querying player's position in some fashion (eq. to check which direction to attack, when to awaken and become aggressive, etc), and some sensors (an enemy that might jump over obstacles queries the world for walls, and jumps over them when appropriate), and so forth.
I want to see any type of enemy AI that can jump gaps and walls.
togFox wrote: Sun Mar 21, 2021 9:22 pm My top down game (not a platformer) uses decision trees implemented as deeply nested if/then statements.
So I don't think this will work...
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Example code of Platformer AI

Post by ivan »

Gunroar:Cannon() wrote: Mon Mar 22, 2021 8:15 am I want to see any type of enemy AI that can jump gaps and walls.
You answered your own question there, you are looking for path finding and possibly obstacle avoidance.
When working with tile-based maps you can just check how large the gap is easily and determine if the player can jump over it.

Having said that, most 2D platformers just use scripted enemy attacks. Like MrFariator said you just need to know the player position so you know which way to attack. Having real pathfinding is rarely necessary in side scrolling games because most enemies do not stay on the screen for very long. With overhead games it might be more useful. You don't need real AI or pathfinding for most 2D platformer games.
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Example code of Platformer AI

Post by Xii »

If the levels are handcrafted (not procedural), you can manually place "jump points" on platform edges that instruct entities they can jump across. Then it's just a matter of deciding to go left or right to find the goal. If there's lots of verticality, it gets more complicated and you probably need pathfinding.
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Example code of Platformer AI

Post by MrFariator »

Gunroar:Cannon() wrote: Mon Mar 22, 2021 8:15 am I want to see any type of enemy AI that can jump gaps and walls.
What Xii works, or alternatively take a look again at the Tigsource post linked. You can use a sensor system like that for a wide variety of purposes, including checking for gaps in the floor, or if the enemy is in front of a wall. Since I know you're using bump.lua, you can implement these sensors using queryRect or similar functions. After that, it becomes a matter of just having some logic like "if there is a gap and I'm moving towards it, jump over it" and "if there is a wall, jump over it", and so on and so forth. If your enemies are fairly simple (in the style of most 2d action platformers) this sort of if-else tree is often more than enough.

I have something like that in my game, but I have abstracted the sensors and collisions enough that it wouldn't be straight forward to just copy-and-paste here, and any pseudo code would be along the lines of what I already wrote.
Last edited by MrFariator on Mon Mar 22, 2021 10:46 am, edited 1 time in total.
User avatar
togFox
Party member
Posts: 763
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Example code of Platformer AI

Post by togFox »

... If your enemies are fairly simple (in the style of most 2d action platformers) this sort of if-else tree is often more than enough.


(aka decision trees)
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Example code of Platformer AI

Post by Gunroar:Cannon() »

Procedural nap, want the enemy AI to preferably also be able to be used as a player companion. Though just looking for examples. Even java :ultrahappy:
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests