Jump to content
  • Advertisement
Sign in to follow this  
ryan mccabe

object creation from script

This topic is 2641 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :-)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!