object creation from script

Started by
6 comments, last by ApochPiQ 12 years, 6 months ago
Ive been mulling this over for a while. Im at a point where the number of objects in my demo engine is growing painful manage. Rather than write out object something = new classT for each and every new object, is it possible to write a file parser / script interface that will generate objects and delete them?

for example:
if level1
run (level1script)

run(script)
//get create the objects
build(entities)
initialize(entities)

build(entities)
//get pointers to objects
for all entities
"name from file" = new entity

initialize(entities)
//initialize entities from file
for all entities
setAttributes(entity)

setAttributes(entity)
//read and return the attributes to initialize with
entity(findInFile(entity))

What I cant figure out is how to write code so that it will make objects based on whats written in a file. Can i just copy strings from a file and use some clever sprintf useage to get it to be a name, or do i set a vector to hold all the created objects of a given type and have the script manage whats where?

Sorry if this is all very confusing, Im just confused about it myself, but i cant imagine that every simgle object must be hard coded into c++. If its standard to do so please tell me though :)

ryan
Advertisement
Take a look into "deserialization" and "factory methods."

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

As a side note: how do you deal with arbitrary scripts and object lifetime?
I am increasingly positive full mark-and-sweep garbage collection is the only feasible/scalable method.

Previously "Krohm"

You would generally want to have as much external data as you can. It will make editing a lot easier (and open a window to user modding if you want).
The only problem is if you dont want people to mingle with your data.

As a side note: how do you deal with arbitrary scripts and object lifetime?
I am increasingly positive full mark-and-sweep garbage collection is the only feasible/scalable method.


Reference counting is a perfectly viable alternative. It's also built into C++11 via shared_ptr and can be used in C++98 or C++03 with boost. If that isn't good enough, writing your own reference counting system isn't terribly hard, unless you're multithreading, in which case it is treacherous but not impossible. For languages in which mark-and-sweep is unrealistic, refcounting is a great middle ground.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I use xml to define my "level", its trivial to add new objects and lets you change things without recompiling. Doesn't have to be xml but I find the human readable format very useful plus I didn't have to write a custom parser. Various factories then use the xml to load things up as apppropriate.

I try not to create objects in scripts (using lua) and instead do it indirectly by accessing factories. My factories return shared_ptrs.

I've had a great deal of trouble when it comes to creating things in scripts, even using shared_ptrs. It can be a nightmare when you can't control object lifetime. One example (lua):

Having a lua state that exists before physics/grapics/factories/etc, you initialise all your things (physics/../etc) and create an object. All goes well untill you come to exit. You need to delete the object first, then the physics/graphiccs/etc and finally the lua state. Alot of the time the object will hang around after its dependandies are gone, when the state is finally deleted it all messes up. I get around that by having seperate states (one that exists only as long as my "level" will).

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Reference counting is a perfectly viable alternative. It's also built into C++11 via shared_ptr and can be used in C++98 or C++03 with boost.
I don't know. I'm having a lot of problems wrapping my head around this... Cloning [font="'Courier New"]shared_ptr [/font]syntax and behavior is out of discussion for me. But I've tried requiring explicit management like COM and that's hardly better. I don't know.

Previously "Krohm"

Consider starting a separate thread to discuss it; I have some thoughts but I'd rather not drag this thread more OT than it already is :-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement