Page 1 of 1

Platformer Levels

Posted: Tue Feb 07, 2012 8:28 am
by veethree
Hey. So i decided to try to make a platformer, and after thinking about it for a while i realized that i'll run into a few problems while making this..Here's the first one: Levels.

The problem is, I have no idea how to go about making the levels. I mean i have some ideas, but none of them are good. I did have one idea which i think could work, But i'm not sure how to execute it. I'd need some way of saving/loading tables to/from files.

I included what i have so far below , But there's basically nothing there yet..just a function that adds a platform at a random position (just a test)

EDIT: I came up with a way to do this that works pretty well. But i'm still open to suggestions..there could be a better way than mine. haha.

Re: Platformer Levels

Posted: Tue Feb 07, 2012 11:04 am
by coffee
veethree wrote: The problem is, I have no idea how to go about making the levels. I mean i have some ideas, but none of them are good. I did have one idea which i think could work, But i'm not sure how to execute it. I'd need some way of saving/loading tables to/from files.
You need to start digging this forum. Almost everything is already answered around.
Here is a very recent topic that could help.
viewtopic.php?f=4&t=7884. If you follow my response on that thread you get a way of loading/saving maps (tables).

Also you don't seem to have something to even think load/save anything. Work first in do some jumping/plataform engine.

EDITED If you don't present your ideas even that bad, lovers can't help you much... :(

Re: Platformer Levels

Posted: Tue Feb 07, 2012 9:03 pm
by Henko
Saving tables to strings (commonly known as serialization) is actually kind of easy in Lua:

Code: Select all

function serialize(val, name, skipnewlines, depth)
	depth = depth or 0

	local tmp = string.rep(" ", depth)

	if name then tmp = tmp .. name .. " = " end

	if type(val) == "table" then
		tmp = tmp .. "{" .. (not skipnewlines and "\n" or "")

		for k, v in pairs(val) do
			tmp =  tmp .. serialize(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "")
		end

		tmp = tmp .. string.rep(" ", depth) .. "}"
	elseif type(val) == "number" then
		tmp = tmp .. tostring(val)
	elseif type(val) == "string" then
		tmp = tmp .. string.format("%q", val)
	elseif type(val) == "boolean" then
		tmp = tmp .. tostring(val)
	elseif type(val) == "function" then
		tmp = tmp .. "loadstring(" .. serialize(string.dump(val)) .. ")"
	else
		tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\""
	end

	return tmp
end
This function creates legal Lua code that you can run at any time using loadstring().