16 Aug, 2009, Bojack wrote in the 1st comment:
Votes: 0
Just need a little example of how to go about this. I was thinking union but it uses string, not int. Anyways I have a data structure and I want a pointer to go like this ch->random7->vnum. i would be set as MAX_NUMBER 7. What im trying to do is to print, load and access a vnum from file and/or memory which would be 7 different vnums.
Something like this:
x->location[i]->vnum = pRoomIndex->vnum;  //Saving
i = fread_number (fp);
x->location[i]->vnum = fread_number (fp);

Of course it will be in a loop. My problem is im not sure how to set the pointer within merc.h in the actual data structure itself. Any ideas?
Edit - damn italic.
16 Aug, 2009, Koron wrote in the 2nd comment:
Votes: 0
I don't really understand what you mean by it would be "7 different vnums."
Based on what you've said, I'm assuming that you want to store an array of 7 different random vnums. You could go about doing this a whole lot of ways. Here's one possibility:

in char_data (probably near vnum, eh?):
int random[7];


And then when you want to randomize them, make a function like this:

void randomize(char_data *ch)
{
for(x = 0 ; x<=6 ; x++)
ch->random[x] = get_random_room();
}


and because we just referenced it…

int get_random_room()
{
ROOM_WHATEVER_DATA *room;

for ( ; ; )
{
room = get_room_index( number_range( 1, MAX_VNUM ) );
if (room == NULL) continue;

break;
}

return room->vnum;
}

You would then just save and load each of these values like the mud saves obj->values (if your mud has these, which I assume it does).

(Note that if you don't have a whole lot of rooms on your mud, that get_random_room thing is going to make your mud cry for its mother.)
16 Aug, 2009, Bojack wrote in the 3rd comment:
Votes: 0
Nah thats not what im going for, I have a major code ive been writing and its going to be set into a file just like a table. This is the data structure I have set in merc.h.. what I need is location[MAX_BALL] to actually work. I just dont know how to go about making it to an actual pointer. Keep in mind that we have to be able to edit it online too the easiest way possible.
typedef struct ball BALL_DATA;
struct ball
{
int vnum;
};

struct dragon_data
{
DRAGON_DATA * next;
bool valid; /* new */
char * name;
int vnum;
int time;
BALL_DATA *location[MAX_BALL];
long flags;
};

Im wondering if I could use union in the structure and set int vnum to sh_int vnum.
16 Aug, 2009, Kline wrote in the 4th comment:
Votes: 0
Why do you need a struct of just a single int? I'm not sure I understand this instead of simply making an int array of int location[MAX_BALL].
16 Aug, 2009, Bojack wrote in the 5th comment:
Votes: 0
Because I want to be able to do this and more:
DEDIT (dedit_location)
{
DRAGON_DATA *dragon;
char arg1[MIL];
char arg2[MIL];
int x, y;

EDIT_DRAGON (ch, dragon);

argument = one_argument(argument, arg1);
argument = one_argument(argument, arg2);

if (!IS_NULLSTR (argument) || !is_number (arg1)
|| !is_number (arg2))
{
sendch ("Syntax: location <#> <vnum>\n\r", ch);
return FALSE;
}

x = atoi(arg1);
y = atoi(arg2);
if (x < 0 || x >= MAX_BALL)
{
sendch ("Value must be 0 through 6\n\r", ch);
return FALSE;
}
dragon->location[x]->vnum = y;
sendch ("Dragon location set.\n\r", ch);
return TRUE;
}
16 Aug, 2009, Koron wrote in the 6th comment:
Votes: 0
So are you looking to have an external variable track seven balls on the mud? Or will each character have seven balls? That code doesn't really say what you want to do, so it's hard to recommend the best way to go about doing it.
16 Aug, 2009, Kline wrote in the 7th comment:
Votes: 0
How is that so different from dragon->location[x] = y? Just to call it location[x]->vnum?? You can make a 3D array… int location[MAX_X][MAX_Y]

So if MAX_X = 5 and MAX_Y = 5 it will have int[0][0] int[0][1] int [0][2] … etc
16 Aug, 2009, Bojack wrote in the 8th comment:
Votes: 0
Id have to post all the functions which id rather not do, in another code im setting the other argument "x" to the vnum of the ball then y will equal the room its in. If a crash happens, it resets the room to a different room by scattering the x vnums, after this is done it saves it to a file. This code is already 3k lines as it is.. rather not keep adding more lines then I have to.
16 Aug, 2009, David Haley wrote in the 9th comment:
Votes: 0
You have three thousand lines of code for randomizing the location of an object? Forgive me for saying so, but maybe that's an indication of a problem somewhere?

Also, a suggestion: if 'x' means ball vnum and 'y' means ball room, it would be a lot less confusing to simply name them "ball_vnum" and "ball_room" in the first place. This would also make it a lot easier to communicate to people (e.g., us) what exactly you're trying to do. After all, we will be completely unable to help you if we don't understand what you want – we might even say things wildly wrong or confusing if the task is not clear.
17 Aug, 2009, Bojack wrote in the 10th comment:
Votes: 0
I didnt mean just this code, meant the whole file, my bad on that. Anyways task is simple, I just want to make this
x->location[i]->vnum
work. Dont want any other way but this, is it possible to make
location[i]
a pointer which is in a dynamic structure, not a table so then I can point to ->vnum.
So if I want to I can do dragon->location[i] = or do dragon->location[i]->vnum =.
All I need is a structure setup in merc.h that will make it work. I have 4 void functions, 2 load and save functions, 2 online editors and 2 do_functions all using this setup which is why I meant I have 3k lines of code in one file.
17 Aug, 2009, Davion wrote in the 11th comment:
Votes: 0
You already can do that. BALL_DATA location[MAX_BALL];. If you want to know how to set those, you first have to allocate a BALL_DATA struct, and set it to the new array you've created. You'll either have to have that done for each node in location[MAX_BALL] or make them share the same data (probably not what you want, so go with the former.)
18 Aug, 2009, Bojack wrote in the 12th comment:
Votes: 0
I figured it out.. I was using *location[MAX_BALL] instead of BALL_DATA location[MAX_BALL].. thanks davion, you showed me my error.
18 Aug, 2009, Bojack wrote in the 13th comment:
Votes: 0
This is my end result.. soon as I took that away my update.c part did all of this automatically.
dedit create shenron

Dragon created.

Name: [shenron]
Time: [0]
Vnum: [0]
Location[0] [0] [0]
Location[1] [0] [0]
Location[2] [0] [0]
Location[3] [0] [0]
Location[4] [0] [0]
Location[5] [0] [0]
Location[6] [0] [0]

TICK!

Name: [shenron]
Time: [1251170370]
Vnum: [30]
Location[0] [3993] [1632]
Location[1] [3994] [1180]
Location[2] [3995] [1140]
Location[3] [3996] [1750]
Location[4] [3997] [1204]
Location[5] [3998] [968]
Location[6] [3999] [1786]

Everything worked perfectly soon as I took the asterisk symbol away.
18 Aug, 2009, Davion wrote in the 14th comment:
Votes: 0
Heh! Well! Don't go thankin me :P. That was totally a typo! I think your problem was that you didn't know (understand) to allocate memory for the variables you wanted to use! Take a look at [link]C_Pointers:_The_Basics[/link]. It talks about all this kinda stuff!
0.0/14