Example code of Platformer AI
- Gunroar:Cannon()
- Party member
- Posts: 1141
- 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?
-
- Party member
- Posts: 548
- 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.
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.
-
- 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
Re: Example code of Platformer AI
My top down game (not a platformer) uses decision trees implemented as deeply nested if/then statements.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
- Gunroar:Cannon()
- Party member
- Posts: 1141
- Joined: Thu Dec 10, 2020 1:57 am
Re: Example code of Platformer AI
I want to see any type of enemy AI that can jump gaps and walls.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.
So I don't think this will work...
Re: Example code of Platformer AI
You answered your own question there, you are looking for path finding and possibly obstacle avoidance.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.
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
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.
-
- Party member
- Posts: 548
- Joined: Wed Oct 05, 2016 11:53 am
Re: Example code of Platformer AI
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.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.
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.
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)
(aka decision trees)
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
- Gunroar:Cannon()
- Party member
- Posts: 1141
- 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
Who is online
Users browsing this forum: No registered users and 2 guests