Build a 3D level from text file?

I’m storing level information in a script and creating the level and everything in it when the editor runs.

I don’t know how to write a text file that contains the info and read it to build the level.

How do you build a 3D level from a text file?

what kind of level and 3d objects (prefabs) you have,or is it created from procedural meshes?

The level info is vertices in a list and indexes of that list to build polygons or portals.

Items and enemies locations.

The level is made of convex polyhedrons that are created.

Just serialize the data into a JSON file.

Based on those two statements, what are we actually talking about here? Do you already have a level stored in a text file and you just want to load that? If that’s the case, you have to tell us what format it has. If on the other hand you have the leveldata already and you want to store it in some kind of text format, you are free to choose how you want to represent the data. Text is just text.

Since you mentioned “convex polyhedrons”, do you by any chance talk about valve’s map / VMF format? Loading this format is really tricky as it essentially contains CSG brushes which are defined just by the enclosing mathematical planes. A long time ago (around 2002 I think) I’ve written a primitive map file loader in delphi. This is a screenshot of my old delphi program which shows one of the location were we held our lan parties back then. Someone modeled the sports hall in worldcraft (now known as the “Hammer” editor)
Screenshot


ps: If someone is interested in this horrible piece of art, here’s the map file of that sports hall. I never have been an artist and this was done for organizational purposes only ;). I created so much useless stuff back then.

Constructing actual triangle geometry from those brushes require some good understanding of linear algebra as you essentially have to find the intersection points of mathematical planes. Also calculating the texture coordinates was also quite a task, as they are constructed from the worldspace positions but the provided axis, scale and rotation values aid in the reconstruction. If that’s what you’re after, I would highly recommend you look for a ready-to-use solution for your format of choice.

If that’s not what you wanted to ask, you should be more clear what you want to do. “Levels” don’t fall from the sky. Someone has to make them. So you already have those levels it seems. So in what format are they or how have they been created? What’s the overall goal you want to achieve?

2 Likes

Sorry.

Adding details.

I have vertices and indexes stored in a script.

A script makes sector game objects then children game objects are made representing polygons or portals.

If it is a polygon then add the script that takes vertices from index and triangulates the vertices to create the polygon.

If it is a portal then get the vertices and put them in a list.

The sector hold a list of all polygon indexes.

The sector gets adjacent sectors by checking if other sectors contain the same index in lists.

The portal script adds the target sector, it checks if sectors contain the portal index and it adds the sector that is not the parent sector.

Objects enemies and player starts have vector3 locations and are children of the sector.

Multiple sectors make the level and polyhedrons are made of polygons with the normal of each polygon pointing inward.

I’m trying to make something like Unreal 1, but the sectors can overlap and do impossible space.

I am a little confused, you seem to know what you are talking about, but don’t know what you are doing?

You will have to be a little more specific as I am interpreting this as if you have in possession a text file that a) you didn’t create? b) you created but can’t reassemble?

If so, why not post an example of what this file looks like. And explain the extent of your coding knowledge here. At the moment I’m not sure whether you are asking how to open and read a text file? Or whether your are asking for the code to recreate a level from a text file?

Starting with the first hurdle what specifically don’t you know how to do?

1 Like

Okay, so you’re having trouble turning this text file into objects / data in memory?

How did it become this text file in the first place? Did a computer program write it? Does that computer program have the ability to read it?

If it is just text, then what you want is string parsing, as many above have noted.

If you have the info already parsed, is this just a question of how to produce procedural geometry from lists of data? That’s up to you, but you’re welcome to check out some procgen examples in my MakeGeo project.

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

Otherwise I have no idea what you want. You could always follow the time-tried and trusted formula:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

Sorry, I’ll try again.

The original information is a OBJ model file that I made in blender.

A OBJ file is like a text file.

It has vertices and indexes.

I don’t know how to read the text from file and turn it into something Unity can use.

I’m asking how to turn that information into game objects.

How do you modify a OBJ file to hold other info about the level?

How do you read a text file like a OBJ and turn it into game objects that contain polygons?

Unity can read OBJ files at import time, just like FBX files (or even .blend files if you have Blender installed)

If you must read an OBJ file AFTER building the game (eg, runtime), there are OBJ readers on the asset store, such as this one:

If you want to write your own OBJ importer, be my guest! It will involve studying the format and learning how it all interrelates, then building the data structures to hold the data, writing the parsing code, then creating geometry.

You may be able to encode some information in the object name, or else provide specially-named objects that are interpreted as meaning something in-game, rather than just geometry, such as an activation trigger area or spawn areas.

How you do this would be up to you. It could be as simple as “if the object name starts with ‘spawnXXX’ then every vertex will spawn an XXX enemy.”

To do anything complex you would need another file that contains the info you want, and it could be linked into the OBJ file via object name, or else via position. The format of this file would be up to you, but unless you want to solve your problem AND create a new file format, you should just use JSON and then you only have to solve your problem, not design a new file format. :slight_smile:

1 Like