Skip to main content
GameDev.net gamedev.net
🔒 Locked

Text to Code

Started by Pactuul Dec 19, 2001 at 10:59 PM 10 replies 450+ views
Original Post
Pactuul
Pactuul
How do you go about reading from a file to inserting the text from that file into your code for example, I want to be able to read classnames from a text file, example MyClass MyClass2 So in the code, I''d like to read that in and say pseudo code: Text = ReadFromFile(); So now Text = "MyClass" then be able to say "MyClass"* pointer = new "Myclass" of course the quotation marks don''t work, but you get the general idea. How do get it to insert the name that I read? Is there a conversion or something?
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
Pactuul
Pactuul
Well, not sure if that applies here.

Basically what I need to do is I have a scenegraph management system that works by attaching instances of new objects

all objects in the game are derived from class Cobject. Like class Knight is derived from cobject, etc...

Well I want to be able to read in a sort of level loader, that tells me what type of objects to load. I thought about just making a big switch statement

switch (text)

if text == "knight"
attach(new knight);

that would work, but it would really hard to keep it up esp when adding multiple types of objects. I know I have to make my own parser to read in the tokens and the special layout, but I thought there would be a direct way to read in a class name from the text and then be able to make a new instance of that class, with out having to deal with switch statements.
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
Arcus
Arcus
Why not using scripting. There are lots of examples of loading classes and executing functions using scripts. That is the most robust way of doing dynamic management of objects and processes. Most games are simply scripts built around a game engine (Unreal, Tribes, etc). Now, that is a very simplified and over-generalized statement, but most games support scripting.

Try starting here.
Arcus
JonStelly
JonStelly
You could look into Pluggable Class Factories, but I think a switch statement will probably be cleaner in this instance. There''s nothing wrong with that, I don''t see the big headache of maintaining it. If you''re creating a new class, just add a line to the switch statement.
a person
a person
do complex operations when you dont understand the basics only leads to diaster.

learn what a compiler is and be enlightened to the way of writing a scripting engine (which amazingly enough when parsing will have many if statments).

you cant switch on a char* with text values. since you would be comparing the memory locations and not what they contain. learn c++ before trying to do big things like scene graph mangment for a 3d engine.
Pactuul
Pactuul
Ok thanks all for the help... I know how to use c++ actually and I know basically how to do scripting, don''t know why you jumped to the conclusion that I don''t know c++ (considering i''ve been coding it for over 4 years now)

I figured there was something I was missing, and I thought to at least ask if there was a command or utility. Since you can use

#define whatever {this statement does}

and just insert it into the code, i figured there might be a easier utility to read text and use it as code...


Btw my "3d engine" with scenegraph management is complete...I just need a way to write the editor and store the world''s objects to quickly load. (which I was going to use scripting for).

jeez,
Pactuul
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
Pactuul
Pactuul
Anon- hmm good idea to look into, that''s for responding!

I guess if your still checking a string and converting to CLSID structure, then using com, might as well use if statement scripting (but that was the type of reply I was looking for)
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
gmcbay
gmcbay
Also take a look at The Nebula Device:

http://www.radonlabs.de

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.