-
June 13th, 2010, 08:46 PM
#16
Re: Working with a database
simple. The array right now is:
When I add a new item to the list, I increase it (manually) to:
No programs are resizing anything (well, except my IDE), it's all hard-coded.
Well, I have another question. This one might be a bit trickier. Would it be possible to save a function's name as a variable, and then use that variable to call the function? For example:
Code:
std::string functionVariable = "MyFunction()";
//or
specialvartype functionVariable = MyFunction();
Can this be done, in part or in whole? This would let me store item scripts (as functions) as one of the variables in the definition. The alternative would be to store a script ID instead, and use the ID with a switch-case to determine which function to run. Any advice?
Last edited by candlemaster; June 13th, 2010 at 09:01 PM.
Reason: Added a question
-
June 13th, 2010, 08:59 PM
#17
Re: Working with a database
Originally Posted by candlemaster
simple. The array right now is:
When I add a new item to the list, I increase it (manually) to:
No programs are resizing anything (well, except my IDE), it's all hard-coded.
Instead of this, why not do this programatically?
The std::vector is resizable at runtime, so I don't see any need for doing what you're saying you're doing.
Code:
#include <vector>
//...
std::vector<Item> ItemDef;
//...
Then you can add a new item at runtime by just calling ItemDef.push_back(). Then you're not scrambling, recompiling a new executable every time.
Regards,
Paul McKenzie
-
June 13th, 2010, 09:21 PM
#18
Re: Working with a database
It could even be something as simple as this:
Code:
#include <fstream>
//...
std::ifstream infile("infile.txt");
//..
int num;
// this file contains the number of items.
// Assume you've read this file, and the number is stored in the variable called "num"
//...
std::vector<Item> ItemDef(num);
Now all you need to do is update the file, and not have to recompile your program if the number of items changes.
Regards,
Paul McKenzie
-
June 13th, 2010, 09:39 PM
#19
Re: Working with a database
Well right now, the item list is defined in a .h file, so I have to recompile every time I change the list anyway.
If I could store the item list in an external file, such as .txt, it would be very helpful.
-
June 13th, 2010, 10:33 PM
#20
Re: Working with a database
Originally Posted by candlemaster
If I could store the item list in an external file, such as .txt, it would be very helpful.
Please strive to do that. Then there would be no need to recompile every time something changes.
Regards,
Paul McKenzie
-
June 13th, 2010, 11:33 PM
#21
Re: Working with a database
Any idea where i should start?
-
June 14th, 2010, 04:40 AM
#22
Re: Working with a database
Place the information you want in a file, and read the information.
Regards,
Paul McKenzie
-
June 14th, 2010, 03:22 PM
#23
Re: Working with a database
Well, the best way I can think of to do that, I could just as easily do that with a .xml file, but from what I understand of those, reading them directly is really clumsy, plus there's a xml schema thing that I can use to make it easier (if I could figure it out that is...)
Do you know of any good tutorials that'll help me out?
-
June 15th, 2010, 02:49 PM
#24
Re: Working with a database
Originally Posted by candlemaster
Would it be possible to save a function's name as a variable, and then use that variable to call the function? For example:
Code:
std::string functionVariable = "MyFunction()";
//or
specialvartype functionVariable = MyFunction();
For the first ideea, the answer is no. That's because the name of a function is not a string. From the programmer's perspective it looks like a text but from the compiler's perspective it's just the relative address where the body of the function can be foud. Probably the best definition would be to say that a function name it's a "named address" (do I have too much imagination ?)
During the compilation all of the function names are replaced with their addresses. If you open the generated exe file with a binary editor, you'll find none of the names of your functions. You cannot relay on function names because they'll be all gone in the exe file.
For the second ideea, the answer is yes.
1. Declare a pointer type matching the declaration of your function:
Code:
typedef std::string (*MY_TYPE_PTR)();
2. Declare a pointer variable of that type:
Code:
MY_TYPE_PTR pMyFunc;
3. Assign the address of your function to that pointer variable:
Code:
pMyFunc = MyFunction;
4. Call the function using the pointer variable:
-
June 15th, 2010, 05:05 PM
#25
Re: Working with a database
Originally Posted by srelu
For the first ideea, the answer is no. That's because the name of a function is not a string. From the programmer's perspective it looks like a text but from the compiler's perspective it's just the relative address where the body of the function can be foud. Probably the best definition would be to say that a function name it's a "named address" (do I have too much imagination ?)
During the compilation all of the function names are replaced with their addresses. If you open the generated exe file with a binary editor, you'll find none of the names of your functions. You cannot relay on function names because they'll be all gone in the exe file.
For the second ideea, the answer is yes.
1. Declare a pointer type matching the declaration of your function:
Code:
typedef std::string (*MY_TYPE_PTR)();
2. Declare a pointer variable of that type:
Code:
MY_TYPE_PTR pMyFunc;
3. Assign the address of your function to that pointer variable:
Code:
pMyFunc = MyFunction;
4. Call the function using the pointer variable:
I'm trying, but it seems the closest I'm getting gives me this error message:
Code:
cannot convert from 'void (__cdecl *)(void)' to 'scriptVar (__cdecl *)'
Here's how I have it set:
Just before the "Item" class definition:
Code:
typedef std::string *(scriptVar)();
Inside the "Item" class definition:
Code:
scriptVar* itemScript;
In the "Item()" function, I set it to NULL
This is part of item #1's definition:
Code:
ItemDef[1].itemScript = &ItemScript_apple;
"ItemScript_apple()" is the name of the function that I want to use.
I tried using "&ItemScript_apple()" instead of "&ItemScript_apple", but I got
Code:
error C2102: '&' requires l-value
as an error code.
Last edited by candlemaster; June 15th, 2010 at 05:07 PM.
-
June 16th, 2010, 06:59 AM
#26
Re: Working with a database
Form what you wrote, I assumed that the declaration of your function is:
Code:
std::string MyFunction();
It seems I was wrong. Please specify the right declaration.
-
June 16th, 2010, 03:29 PM
#27
Re: Working with a database
Ah, I see. The function is a void, not a string.
-
June 16th, 2010, 05:13 PM
#28
Re: Working with a database
Getting back to basics for a moment---
Originally Posted by candlemaster
If given the unique ID of an item (short int), I need to be able to use that to look up various statistics about the item,
It sounds like you want a std::map, or a std::unordered_map (if your compiler supports it; if not, there's one in Boost).
You could of course use an array or vector, if you can be sure that all of your unique IDs will be contiguous. The map option is preferable if you don't want restrictions on what those IDs can be other than "unique". I'm going to use the word "map" below for simplicity.
without having to load the entire list of items into the memory.
Well, one option that hasn't been suggested yet----store everything a file, as a series of records. But then, instead of loading the entire file into memory (presumably into objects stored in the map), you can parse the file once and simply store offsets into the file in the map instead. Then, when you look something up in the map, you just jump to the appropriate offset and read that part of the file.
Will that be more efficient? I very much doubt it. But it would allow you to use a massive file without cluttering RAM up too much. Maybe that's worth it, I don't know.
-
June 16th, 2010, 05:50 PM
#29
Re: Working with a database
I changed "typedef std::string *(scriptVar)();" to "typedef void *(scriptVar)();", but nothing's changed...
-
June 16th, 2010, 08:11 PM
#30
Re: Working with a database
Originally Posted by candlemaster
I changed "typedef std::string *(scriptVar)();" to "typedef void *(scriptVar)();", but nothing's changed...
Code:
typedef void* (*scriptVar)();
Syntax for typedef declaration of 'C"-style function pointer:
Code:
typedef return_type (*typedef_name)(args);
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|