Welcome to the Builder Academy

Question Events and Variables

More
05 Jul 2014 22:20 #4948 by WhiskyTest
Hi all,

I'm trying to use the event system to create an event called repair. The scenario is the player is repairing a door in a room. Where I am stuck is how to send the "int door" variable to the event and then return it in the EVENTFUNC.
I've been trying to use the "char sVariables" but it always ends up as 0 during the EVENTFUNC. I had a crack at casting door as a char but just managed to crash the MUD.

Call for new event:
Code:
NEW_EVENT(eREPAIR, ch, (char) door , 4 * PASSES_PER_SEC);

and in the EVENTFUNC:
Code:
door = (int) pMudEvent->sVariables;

I have got it working by adding in a new "int" inside the event structure itself, but was thinking the sVariables field is there for use so I should be able to use it for integers, victims etc?

Please Log in or Create an account to join the conversation.

More
06 Jul 2014 21:24 #4949 by thomas
Replied by thomas on topic Events and Variables
Look at raw.githubusercontent.com/welcor/tbamud/...fe/doc/dg_events.txt

You need to make an event_obj structure for your event. It can be as simple as
Code:
struct repair_event_obj { int door; }

The reason for this roundabout way of passing a variable is that the event_obj is free()'d after the event is done. char or int are stored on places where a free() causes crashes.

Please Log in or Create an account to join the conversation.

More
07 Jul 2014 04:17 #4951 by WhiskyTest
Replied by WhiskyTest on topic Events and Variables
Is there a way to reference the repair_event_obj from a normal mud event?
If I had my repair event created with NEW_EVENT(), is it possible to add the int door to it using the new repair_event_obj?

Or will I be making the event using repair_event_obj as the structure - so it would need the eventID and the ch. Then attach it with new functions specifically for the repair_event_obj eg: NEW_REPAIR_EVENT() attach_repair_mud_event(new_repair_mud_event() etc.. ?

Please Log in or Create an account to join the conversation.

More
07 Jul 2014 20:51 #4952 by thomas
Replied by thomas on topic Events and Variables
Yes, the whole idea is to reuse the code.

What you do is similar to what's happening here:

/* the event object for the sniff event */
struct sniff_event_obj {
struct char_data *ch;
byte type;
};


EVENTFUNC(sniff_event)
{
[2] struct sniff_event_obj *seo = (struct sniff_event_obj *) event_obj;
struct char_data *ch, *victim;

ch = seo->ch;

GET_CHAR_SNIFF(ch) = NULL;

if (type == SNIFF_COLD)
act("$n sniffs loudly.", FALSE, ch, NULL, NULL, TO_ROOM);
else
act("$n sniffs some cocaine.", FALSE, ch, NULL, NULL, TO_ROOM);

act("You sniff.", FALSE, ch, NULL, NULL, TO_CHAR);

if (--seo->severity <= 0) {
/* we're done with sniffing */
free(event_obj);
}
else
return PULSE_SNIFF;
}


ACMD(do_sniff)
{
struct sniff_event_obj *sniff;

[1] CREATE(sniff, struct sniff_event_obj, 1);
sniff->ch = ch;
sniff->severity = 5;
if (GET_CLASS(ch) != CLASS_THIEF)
sniff->type = SNIFF_COLD;
else
sniff->type = SNIFF_COCAINE;

GET_CHAR_SNIFF(ch) = event_create(sniff_event, sniff, PULSE_SNIFF);

send_to_char(OK, ch);
}

[1] When the event is created, you add the object of the type you want.
[2] Then, in the actual event, you cast it back to your object type.

Please Log in or Create an account to join the conversation.

Time to create page: 0.196 seconds