## Example code of Platformer AI

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

### Example code of Platformer AI

Does Anyone know of anywhere I can see any type of example code (lua preferably, but others are fine to) of a platformer AI?
me: I don't always code but when I do it's done flawlessly.
also me:

Code: Select all

 function Gunroar:Cannon()
for x, enemy in ipairs(self.allEnemies) do
self:Cannon(enemy)
end
end

Code: Select all

Lua Error: [file Gunroar.lua]:18: C stack overflow

MrFariator
Party member
Posts: 243
Joined: Wed Oct 05, 2016 11:53 am

### Re: Example code of Platformer AI

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

Code: Select all

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

### Re: Example code of Platformer AI

My top down game (not a platformer) uses decision trees implemented as deeply nested if/then statements.
Gunroar:Cannon()
Party member
Posts: 253
Joined: Thu Dec 10, 2020 1:57 am

### Re: Example code of Platformer AI

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...
me: I don't always code but when I do it's done flawlessly.
also me:

Code: Select all

 function Gunroar:Cannon()
for x, enemy in ipairs(self.allEnemies) do
self:Cannon(enemy)
end
end

Code: Select all

Lua Error: [file Gunroar.lua]:18: C stack overflow

ivan
Party member
Posts: 1721
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: Example code of Platformer AI

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.
Xii
Citizen
Posts: 90
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

### Re: Example code of Platformer AI

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: 243
Joined: Wed Oct 05, 2016 11:53 am

### Re: Example code of Platformer AI

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.
togFox
Party member
Posts: 150
Joined: Sat Jan 30, 2021 9:46 am

### Re: Example code of Platformer AI

... 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)
Gunroar:Cannon()
Party member
Posts: 253
Joined: Thu Dec 10, 2020 1:57 am

### Re: Example code of Platformer AI

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
me: I don't always code but when I do it's done flawlessly.
also me:

Code: Select all

 function Gunroar:Cannon()
for x, enemy in ipairs(self.allEnemies) do
self:Cannon(enemy)
end
end

Code: Select all

Lua Error: [file Gunroar.lua]:18: C stack overflow


### Who is online

Users browsing this forum: No registered users and 7 guests