Sign in to follow this  

Pre-made instances of a class

This topic is 4600 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

Right, here's what i've got. I have an 'Item' class, of which have name, damage etc. I want some pre-created when my game starts, but would i have to initialise them in 'int main()' or can i put them all into a header, as a sort of database?

Share this post


Link to post
Share on other sites
Er, XML isn't really relevant here as far as I can see...

Basically, all instances are the same - you start off with none, end with none, and create and destroy some in the middle. That could be in int main(), or in a function it calls, or above int main() if they are going to be globals. Really it depends on what scope and lifetime you want the instances to have.

Share this post


Link to post
Share on other sites
Well the items are part of a small RPG im making, so does that basically mean create them all at the beginning of the program, and then delete them all at the end or create and destroy as needed?
This is why i was think of collecting all of them into a header file or something, and then call them from main, that way i dont need to create a switch(case) statement with a million different cases all over the place

Share this post


Link to post
Share on other sites
Why don't you just make a data file with all the items that should be in the game world at start-up, and then load it each time the game starts? This will make it easier to edit the items as well, as you won't have to recompile the program to do so. Also, what were you thinking you might need a switch statement for? I can't think of a reason why you would need one which would magically disappear just because you moved the code to another file.

Share this post


Link to post
Share on other sites
One of these data files are what i had in mind, but would it have to be a dll or what? Ignore the switch case statement part, it was late.....

Share this post


Link to post
Share on other sites
No; a data file is data while a DLL is code. (Of course, the distinction is conceptual - it's all 1s and 0s to the computer - but yeah.)

What you *do* need is to work out some sort of file format, so that you know which byte in the file has what meaning. Then when you load from the file, you need to use the data to create your objects. Normally this will mean storing values in the file that will either be used as ctor parameters, or written directly into data members of a "blank" object.

You really should read this at this point.

Share this post


Link to post
Share on other sites
Ah thanks Zahlman, that's the kind of thing i wanted, just wasnt sure what it was, because as you guessed im teaching myself so these boards are my fountain of knowledge. Thanks again :)

Share this post


Link to post
Share on other sites

This topic is 4600 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this