Page 1 of 2
Example code of Platformer AI
Posted: Sun Mar 21, 2021 6:37 pm
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?
Re: Example code of Platformer AI
Posted: Sun Mar 21, 2021 6:44 pm
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.
Re: Example code of Platformer AI
Posted: Sun Mar 21, 2021 7:52 pm
by skabz_cool
Re: Example code of Platformer AI
Posted: Sun Mar 21, 2021 9:22 pm
by togFox
My top down game (not a platformer) uses decision trees implemented as deeply nested if/then statements.
Re: Example code of Platformer AI
Posted: Mon Mar 22, 2021 8:15 am
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...
Re: Example code of Platformer AI
Posted: Mon Mar 22, 2021 8:50 am
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.
Re: Example code of Platformer AI
Posted: Mon Mar 22, 2021 10:26 am
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.
Re: Example code of Platformer AI
Posted: Mon Mar 22, 2021 10:41 am
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.
Re: Example code of Platformer AI
Posted: Mon Mar 22, 2021 10:46 am
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)
Re: Example code of Platformer AI
Posted: Mon Mar 22, 2021 8:43 pm
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