Page 1 of 2

Implementing levels

Posted: Thu Jul 29, 2021 6:32 pm
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)

Re: Implementing levels

Posted: Mon Aug 02, 2021 11:58 pm
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?

Re: Implementing levels

Posted: Tue Aug 03, 2021 10:09 am
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.

Re: Implementing levels

Posted: Tue Aug 03, 2021 10:23 am
by darkfrei
The simple level file:

Code: Select all

-- levels.lua

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

return levels

Re: Implementing levels

Posted: Tue Aug 03, 2021 10:20 pm
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.

Re: Implementing levels

Posted: Tue Aug 03, 2021 11:02 pm
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?

Re: Implementing levels

Posted: Wed Aug 04, 2021 4:05 am
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.

Re: Implementing levels

Posted: Wed Aug 04, 2021 6:41 pm
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.

Re: Implementing levels

Posted: Sat Aug 07, 2021 8:35 pm
by Josh.G
I just dont know how i would implement this into my project.

Re: Implementing levels

Posted: Sat Aug 07, 2021 11:08 pm
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.