Implementing levels

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Josh.G
Prole
Posts: 16
Joined: Thu Jul 29, 2021 6:16 pm

Implementing levels

Post by Josh.G »

Im kind of a newbie to Lua coding so I'm kinda lost.
If anyone can help me out, I'd like to know how to have levels with save data, and how to have a level screen for a game im making.
(im not using Tiled btw)
applebappu
Prole
Posts: 37
Joined: Thu Jun 24, 2021 5:49 pm

Re: Implementing levels

Post by applebappu »

The nice thing about Lua is also the bad thing about Lua: it's a language without a lot of built-in commands and functions. So, while it gives you the flexibility to code in things that you might need for a particular project, you do have to code a lot of those things from scratch.

So! That said, what you come up with for levels and save data and level screens, that's all going to depend on the kind of game you're making, in many ways.

What else can you tell us about these levels, and about your game?
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Implementing levels

Post by ReFreezed »

As applebappu says - you'll have to decide yourself how everything should work in your game, and how it should work depends on what game you're making. Most likely your "levels" will be represented by tables with some information inside. As for menus there are many GUI libraries to help with that, if you don't wanna implement your own solution.

You might wanna check out Sheepolution's beginner's tutorial.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Implementing levels

Post by darkfrei »

The simple level file:

Code: Select all

-- levels.lua

local levels = {}
levels[1] = {seed = 123}
levels[2] = {seed = 456}

return levels
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Josh.G
Prole
Posts: 16
Joined: Thu Jul 29, 2021 6:16 pm

Re: Implementing levels

Post by Josh.G »

The type of game im making is basically a more simple game, basically picture a block going through an obstacle course, and if it falls it dies.
applebappu
Prole
Posts: 37
Joined: Thu Jun 24, 2021 5:49 pm

Re: Implementing levels

Post by applebappu »

Josh.G wrote: Tue Aug 03, 2021 10:20 pm The type of game im making is basically a more simple game, basically picture a block going through an obstacle course, and if it falls it dies.
Simple games are a fantastic place to start!

Are you looking to make all the levels yourself? Or write code to do procedural generation? Single-screen, or scrolling around?

Either way, it sounds like a "level" in your game is going to be a screen or map that goes away when the player succeeds at it, maybe?
Josh.G
Prole
Posts: 16
Joined: Thu Jul 29, 2021 6:16 pm

Re: Implementing levels

Post by Josh.G »

Yes! its not a single screen, and also yes, it would be sort of like a map, i would want it to be when you get to a certain spot the game ends and saves your progress then you have the choice to go to the next map and so on.
EDIT: Also im coding all of the levels myself using the Windfield physics library.
applebappu
Prole
Posts: 37
Joined: Thu Jun 24, 2021 5:49 pm

Re: Implementing levels

Post by applebappu »

Ah, neat!

So, Lua makes this kind of thing super easy, actually. What you'll want is probably to set up a table in advance, that you'll use to store your maps. Because, those maps, they're just tables too! You can shove tables inside other tables, and keep them there, pull them back out later, etc.

Here's a code example from my own project. Keep in mind, this is just one way of doing it and your project will look very different. But you might get some ideas from looking at it! This snippet is taken from my love.keyreleased function, because in my game, I change levels when the player is on an up staircase or down staircase tile and they push the upstairs or downstairs key.

Code: Select all

if k == "." and resources.current_map.map_table[mob_db.Player.position.x][mob_db.Player.position.y] == ">" then
			if resources.tower_level == 1 then
				print("leaving the tower")
				love.event.quit()
			else
				resources.tower_level = resources.tower_level - 1
				resources.current_map = resources.world_map_memory[resources.tower_level]
				resources.spawn_table = resources.world_spawn_memory[resources.tower_level]	
			end
		elseif k == "," and resources.current_map.map_table[mob_db.Player.position.x][mob_db.Player.position.y] == "<" then
			resources.world_map_memory[resources.tower_level] = resources.current_map
			resources.world_spawn_memory[resources.tower_level] = resources.spawn_table
			resources.tower_level = resources.tower_level + 1
			tools.NewLevel()
		end
Obviously this is making reference to things I'm putting elsewhere in the code, like my "resources" module and such. Here's a more generic version (that won't actually work in ANY project, it's what's called "psuedocode"). This makes the logic flow more clear.

Code: Select all

if key == "go to the next level" and current_tile == "the tile that changes levels" then
    if "you're on the final level" then
        yourgame.Win()
    else
        put the current level in a table of levels for storage
        level = level + 1
    end
elseif key == "go to the previous level" and current_tile == "the tile that changes levels" then
    if "you're on the first level" then
        yourgame.End()
    else
        retrieve the previous level from that storage table
        level = level - 1
    end
end
The fun part is, YOU get to choose all this stuff. Where the player will change levels, how many levels, what those levels look like, it's all just tables and tables full of tables. You have ultimate control over all of it. It's exciting and overwhelming at the same time.

You're not beholden to doing it exactly this way, either. There's lots of ways to do this.
Josh.G
Prole
Posts: 16
Joined: Thu Jul 29, 2021 6:16 pm

Re: Implementing levels

Post by Josh.G »

I just dont know how i would implement this into my project.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Implementing levels

Post by ReFreezed »

I've created a small example game with a level menu and a game world with a win condition. Maybe this will help you understand things better.
Attachments
SimpleAppleGame.love
(2.68 KiB) Downloaded 188 times
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests