Re: [CODE] Ferry code

From: Thomas Arp (t_arp@stofanet.dk)
Date: 11/05/02


From: "Brian" <borlick@MINES.EDU>
> On Tue, 5 Nov 2002, Bill Pascoe wrote:
>
> > I implemented the ferry code on the FTP website it works great
> > (after I fixed the send to room error). But
> > was I happy NO.... I decided it needed more. I wanted it so that when
> > <---snip-->
> > I think you should be able to get the picture. Now what in the world
> > is wrong with this.
> > <---snip-->
>
> Well, this:
>
> > struct obj_data *tobj;
>
> The pointer, tobj, gets reset every time the function is called since it's
> a local variable.  You can solve this by making the variable "static".
>
>   static struct obj_data *tobj;
>
> This comes with the disadvantage that you can only use the ferry routine
> for one ferry.  With multiple ferries, every time a new object was creted,
> it would overwrite the old value of tobj.  And if we assume the object
> can't be moved, there's still the problem that the object may get purged
> before the ferry routine is run.  In that case, tobj points to memory
> which may longer be valid.  That is, you may get random crashes from that
> code.
>
Nope. Have a look at mag_creations(). That's actually the exact way it is
supposed to be.

> If you really wanted to go so far, you could add another member to
> obj_data, a callback function pointer, called when the object is
> destroyed.  Then, in your ferry routine, you would set up that callback
> pointer when you create the new object.  Then, in extract_obj, call the
> callback function.  That function, in turn, would have to update tobj to
> NULL (which means you'd have to make tobj global so callback function
> could access it).  But that's overkill.
>
Yep. Since read_object() already adds the object to the object list,
the object is still valid after the function returns. *tobj, however,
is not.
What should be done is something aling these lines:
#define FERRY_VNUM 1220
struct obj_data *tobj;
int found = FALSE;

if(time_info.hours == ferrys[onferrynum].f_time_arrive_to_room2){
<---snip to make shorter--->
  if (!tobj = read_object(FERRY_VNUM, VIRTUAL))
    log("SYSERR:No ferry object designed!");
  else
    obj_to_room(tobj, real_room(ferrys[onferrynum].f_to_room));
<---snip to make shorter--->
}

if(time_info.hours == ferrys[onferrynum].f_time_leave_from_room2){
<---snip to make shorter--->
/* search this room for the object */
  for (tobj = world[real_room(ferrys[onferrynum].f_to_room)].contents;
       tobj && !found; tobj = tobj->next_content)
    if (GET_OBJ_VNUM(tobj)==FERRY_VNUM)
      found = TRUE;
  if (found) {
    obj_from_room(tobj);
    extract_obj(tobj);
  }
<---snip-->
}


> DGScripts may allow you to do this in some fashion, but I've never used
> it.

They do. Easily, even. But it's a big much to implement just to get
a few ferries.

Welcor

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT