Pre-made instances of a class

Started by
7 comments, last by Welshy 18 years, 11 months ago
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?
Advertisement
Xml tutorial.

Xml is a way of storing data outside your program and is used by most programs (read games) today.
xml? it says its like html, would that work with C++? cant say i've heard of it
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.
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
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.
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.....
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.
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 :)

This topic is closed to new replies.

Advertisement