Author Topic: Button Object code  (Read 2422 times)

vakuta

  • Guest
Button Object code
« on: October 13, 2006, 04:13:06 pm »

When I was coding this, I got that feeling.  I didn't really expect it to work, but since it was a good effort, at least, I'll post it anyway.   ;-)



 





 #include <lib.h>

#include <vendor_types.h>



inherit LIB_ITEM;



string color;



int once;



mixed is_pushed();





static void create() {

   ::create();

   SetMass(5);

   SetShort("A metal panel.");

   SetLong("A metal panel with a big red button in the middle labeled /"PUSH ME"/.");

   SetKeyName("panel");

   SetId("panel","metal panel","button",);

   SetItems( ([  ({ "button" : (: is_pushed :)   ]) );


   

 


   

}





void init() {

   ::init();

   once = 0;

   color = red;


   

}



 



add_action("push_button","push") {



mixed push_button(string str);

if( str != button) {  write("Push what, the button?");

else write("You push the button, which turns blue!");   color = blue;  once = 1;  say(this_player->GetName() + " pushes the button, which turns blue!");



return 1;



}



if( once == 1) { write("The button has already been pushed!");

return 0;

  }


 

}



 





And this is the error when I try to clone.





> /realms/zaroth/area/obj/button.c line 17: syntax error


   

/realms/zaroth/area/obj/button.c line 29: Illegal LHS


   

/realms/zaroth/area/obj/button.c line 30: Undefined variable 'red'


   

/realms/zaroth/area/obj/button.c line 30: Illegal LHS


   

/realms/zaroth/area/obj/button.c line 40: Undefined variable 'blue'


   

/realms/zaroth/area/obj/button.c line 40: Illegal LHS


   

---

*Error in loading object '/realms/zaroth/area/obj/button'

Object: /cmds/creators/clone at line 21



'<function>' at /secure/save/creators/z/zaroth (<function>) at /:0

'cmdAll' at /secure/save/creators/z/zaroth (/lib/command.c) at line 93

'cmd' at /cmds/creators/clone at line 21

'CATCH' at /cmds/creators/clone at line 21

Trace written to /log/catch

Error in cloning object: *Error in loading object

'/realms/zaroth/area/obj/button'



 



 





I see some undefined and uncalled stuff in my code, but I can't quite figure it out myself, since this is my first real attempt and add_action() on an object.


Offline Nulvect

  • BFF
  • ***
  • Posts: 127
    • View Profile
Button Object code
« Reply #1 on: October 13, 2006, 06:08:22 pm »

It looks like you've misunderstood how to use init() and add_action()s.



The init() function is called anytime a living thing comes in contact with this item, such as walking into the room that it's sitting in. So, as is, your variables will be reset every time someone walks in. You probably want to put those two lines at the end of create(), which is only called once, when the object is first created. An alternative is to make a reset() function and put them in there. The reset() is void; that is: void reset() { do stuff; }. Resets are called after create and then once every X seconds where X is set in a config file somewhere.



Also, the add_action() function has some strict requirements: the functions it calls have to be of the form "int funcname(string str)"; that is, they must return an int and take a string as an argument. Mixed might work, but I wouldn't count on it. Read man add_action, it really explains everything very well.



You also have is_pushed() prototyped (to return mixed, when int is probably what you'd want) but there's no is_pushed() function there. You might want to prototype the push_button function as well, as int push_button(string str).



Your usage of add_action here looks like you're trying to redeclare it (to the mud, anyway). You just need to do add_action("funcname", "command"); in your init().



Inside your push_button code, you should probably handle things like this: if (str != "button") return notify_fail("Push what?\n"). You should read the man page on notify_fail for more info. Some punctuation is missing as well, such as "" around the string blue, and some brackets are out of place for grouping things.



I should mention one thing in closing: you don't really need to use add_action(). Dead Souls is set up to use verbs, and it looks like a much cleaner way of doing things than using a hundred add_action()s. I believe that there are even examples of buttons you can push that come with the lib.

A quick search showed me /lib/events/press.c which you can inherit as LIB_PRESS; there is a button which does this in /domains/town/obj/ebutton1.c even. I don't know enough of the verb system to explain more though, sorry.


vakuta

  • Guest
Button Object code
« Reply #2 on: October 13, 2006, 07:46:18 pm »

Well, the item clones now, it just needs to be tweaked so the code operates.  Here is the currant code:



 



#include <lib.h>

#include <vendor_types.h>



inherit LIB_ITEM;



string color;



int once;



string is_pushed() {  return "A big, round button, currantly " +"color "+ "in color.";



}





static void create() {

   ::create();

   SetMass(5);

   SetShort("A metal panel");

   SetLong("A metal panel with a big " +color+ " button in the middle labeled \"PUSH ME\".");

   SetKeyName("panel");

   SetId("panel","metal panel");

   SetItems( ([  ({ "button" }) : (: is_pushed :)   ]) );

   once = 0;

   color = "red";


   

 


   

}





void init() {

   ::init();


   

add_action("push_button","push");



}


 



mixed push_button(string str) {

if( str != "button") return "Push what, the button?";

else { return "You push the button, which turns blue!";   color = "blue";  once = 1;  say(this_player()->GetName() + " pushes the button, which turns blue!");



return 1;



}



if( once == 1)  return "The button has already been pushed!\n";

return 0;

  }



 



The SetItems() doesn't seem to exist, the button in it, and the push commands and all don't display any messages - they just return a new prompt.


Offline llyr

  • Pottymouth
  • *
  • Posts: 1
    • View Profile
Button Object code
« Reply #3 on: October 13, 2006, 09:00:05 pm »
No clue about the SetItems bit, but maybe this will help with the add_action:



lpc code:

#include <lib.h>
#include <vendor_types.h>

inherit LIB_ITEM;

string color;

int once;

string is_pushed()
{
  return "A big, round button, currantly " + "color " + "in color.";
}


static void create()
{
  ::create();
  SetMass(5);
  SetShort("A metal panel");
  SetLong("A metal panel with a big " + color +
          " button in the middle labeled \"PUSH ME\".");
  SetKeyName("panel");
  SetId("panel", "metal panel");
  SetItems(([ ({ "button" }) : ( : is_pushed :) ]));
  once = 0;
  color = "red";
}


void init()
{
  ::init();
  add_action("push_button", "push");
}


int push_button(string str)
{
  if (str != "button")
  {
    write("Push what, the button?");
    return 1;
  }
  else
  {
    if (once == 1)
    {
      write("The button has already been pushed!\n");
      return 1;
    }

    color = "blue";
    once = 1;

    say(this_player()->GetName() + " pushes the button, which turns blue!");
    write("You push the button, which turns blue!");
    return 1;
  }

  return 1;
}