arrays, structs, and includes, oh my!
Displaying 1-4 of 4 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
locke

(warning: manually formatted because of wonky forum formatting problems)

Got a couple more questions:

Arrays: can I use {-notation for 2D arrays?. Example:

int nums[2][5];
nums[0] = { 0,1,2,3,4 };
nums[1] = { 5,6,7,8,9 };

EDIT: are generic 2d arrays even available? Meaning:

nums[n][n] = 1;

I'm having trouble assigning a value to an element that way. I suppose I can fake it with a struct... and that might even work better for what I'm doing, but being able to dive through a 2D array with a for-loop is... err... valuable.

Next...

Are there any issues with setting variables for a struct defined in a different file? Let me explain:

I have a file, skills.vc, that is included from system.vc. In skills.vc I am defining a struct of type "skill", and I define it with a handful of variables. Nothing fancy at all. However, from "skills.vc" I include a file called "backstab.vc", which contains only the following:


skill backstab;

backstab.cost = 10;
backstab.class = 1; // 1=rogue

backstab.first_att = 5; // how many rounds reset?
backstab.second_att = main_char.to_hit / 2; // chance to-hit
backstab.third_att = main_char.level * 10 / 2; // how much damage bonus?

backstab.name = "backstab";
backstab.cap_name = "Backstab";
backstab.action_text = "backstabs";



Again, nothing fancy... and since I moved the "#include" line in skills.vc beyond
the structure's definition (lower down in the actual file) it seems to get to the code in
backstab.vc, but it halts at line 3 (according to vcc), saying "expecting a variable for function definition..."

Got any thoughts? It's almost like it doesn't want me to touch the variables in the struct.

For reference... here's the contents of skills.vc:


#define NUM_SKILLS 3

struct skill
{
int cost; // to use
int class;

int first_att; // \
int second_att; // | store data in atts
int third_att; // /

string name; // "backstab"
string cap_name; // "Backstab"
string action_text; // "backstabs"
};

#include "backstab.vc" // moved after skill-template struct def
// to eliminate errors because these
// includes use the struct



I'm wondering if it's just sloppy code... like am I over looking something?? ...or am I
breaking some rule of structs, variable scope, or includes?

Thanks for your help.

-lok

Posted on 2004-03-01 20:43:47 (last edited on 2004-03-01 23:00:38)

vecna

I dont know if its just the way your code came out from the formatting here, but from what I see:

skill backstab;

backstab.cost = 10;
backstab.class = 1; // 1=rogue

backstab.first_att = 5; // how many rounds reset?
backstab.second_att = main_char.to_hit / 2; // chance to-hit
backstab.third_att = main_char.level * 10 / 2; // how much damage bonus?

backstab.name = "backstab";
backstab.cap_name = "Backstab";
backstab.action_text = "backstabs";


These need to be in some function, eg, autoexec() or some initialize() type function. struct members cannot be 'initialized upon declaration' in this manner at this time.

If that wasnt the problem. uh. let me know ^_^

To answer your other questions, no, { } syntax array initialization is not supported at this time, tho that is something I plan on doing, and I started on it some time ago but never got around to finishing it.

Otherwise... #include order in general should never matter. VC does a full "declaration scanning" pass where it collects all struct, variable, and function declarations before it begins actually compiling code. It will only matter on what error you get first, since VC will stop on the first error it encounters.

Posted on 2004-03-01 22:54:46

locke

No, you got it on the nose... and I sort of worked that out as I was fixing something completely different. It struck me that the code would never get run... simply including it doesn't actually execute it.

That was moronic of me not to piece that together. ;| I'm struggling with structs... it's been ages since I've used them this extensively, and for whatever reason they bend my mind in bad ways.

Thanks for the background on VC. I think I need to slow down and spend more time on (literally) the structure of my code.

Thanks for your patience. :)

-l

Posted on 2004-03-01 23:05:56

locke

Err... note: I edited it to add more questions about 2D arrays.

Basically: can I use the C standard var[n][n] notation?

Turns out I might just use a struct to store "table data", but I'd rather be able to access individual elements with for-loops.

Also, on a side note... any thoughts on doing mappings? I have many many years of experience with LPC (the mud scripting language) and have used mappings to good effect there.

EDIT: Woot! with some minimal changes, I got the whole thing to compile. Thanks again for the pointers.

ANOTHER EDIT: Yeah, I dug through the arrays problem, and all is well now. I'm still stuck in an OO universe, but slowly my brain is thawing, and I'll be thinking procedurally again soon.

-l

Posted on 2004-03-01 23:15:09 (last edited on 2004-03-02 03:18:25)


Displaying 1-4 of 4 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.