Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Ambelghan
Novice


Joined: 04 Apr 2010
Posts: 49

PostPosted: Tue Feb 22, 2011 11:30 pm   

How to create an inventory returning function [BUSTED]
 
I do this quite often
Code:
<class name="TestClass" id="3684">
  <trigger priority="36850" id="3685">
    <pattern>Inside the*</pattern>
    <trigger name="">
      <pattern>^*$</pattern>
    </trigger>
    <trigger name="">
      <pattern>^(*)$</pattern>
      <value>  #IF (%begins(%1,"[Type")) {#ECHO OUT;#STATE 0} {bagItem =  %additem(%1,@bagItem);#STATE 2}</value>
    </trigger>
  </trigger>
</class>


Which basically looks in a container and starts to generate a list of items in that container. I do it so often that I thought about getting fancy and building a function I could call globally, instead of rewriting this set of triggers for whatever purpose I need. This little bit is actually from inside the function:
Code:
<func name="ContainerList" id="3651">
  <value>//#T+ ContainerList
i my $container
bagItem = ""
#TR {Inside the*} {} TestClass
#COND {^*$} {}
#COND {^(*)$}
{
  #IF (%begins(%1,"[Type")) {#ECHO OUT;#STATE 0} {bagItem = %additem(%1,@bagItem);#STATE 2}
}
</value>
  <arglist>$container</arglist>
</func>


Which actually works just like it should, however when I tried to add, #DELCLASS TestClass, and do a #RETURN, as clean up, the function quickly stopped working. My guess on the #DELCLASS is that it deleted the class before it could do anything, not sure on the # RETURN. Also it would be nice if my list variable, @bagItem, wasn't a gobal variable sitting somewhere, but the system balked at me when I tried to use a temporary variable. Any insight as to how I might accomplish the task of having a function that takes in a bag name and outputs its contents, in a #RETURN, would be very helpful, to this project and as a template for others, thanks in advance.


Last edited by Ambelghan on Wed Feb 23, 2011 8:10 am; edited 1 time in total
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Wed Feb 23, 2011 1:50 am   
 
Well, one problem is that you are recreating the trigger each time you run this function. That's a bad way to do this. Another problem is that the capture is happening inside the trigger that you are creating, not inside the function which is creating the trigger. That's why the local variable wouldn't work--the trigger can't pass that information back to the function. In fact, i don't see a way to do what you want inside a function. A function really isn't designed to capture lines from the mud. It is designed to take an input and produce an output. Triggers are designed to capture information from the mud. But using the #TRIGGER command inside a function simply (re)creates a trigger, which is _outside_ the function.

Basically, you can't capture it in a function.
Reply with quote
Ambelghan
Novice


Joined: 04 Apr 2010
Posts: 49

PostPosted: Wed Feb 23, 2011 2:32 am   
 
I don't have an issue with the function creating a trigger class I can always delete the class when the event is done.

I don't know if that is why the local won't work since the actual capture is occurring in the trigger which, as we both know, is outside the function, in fact when I alter the class directly, (the function creates a class)
Code:
  #IF (%begins(%1,"[Type")) {#ECHO OUT;#STATE 0} {$bagItem =  %additem(%1,$bagItem);#STATE 2}
I still get an error, so the function has nothing to do with the ability to use a local or global in that part of the trigger. What I am hoping is that someone can give a why or a work around to using a local in that particular part of the function. I suppose what I would ultimately need is some sort of loop in the function that returns the each bag item to an external variable that list it.

You are absolutely right, each time the function runs it creates that class, what is not shown, is that it is also suppose to destroy that class when it is finished. These are not side effects, they are desired goals. I suppose I have to figure out some wait to install a pause in the function till till the bagItem, is complete, then it can post it out.
Reply with quote
Ambelghan
Novice


Joined: 04 Apr 2010
Posts: 49

PostPosted: Wed Feb 23, 2011 8:09 am   
 
Code:
<func name="ContainerList" id="3651">
  <value>//#T+ ContainerList
i my $container
bagItem = ""
#TR {Inside the*} {} TestClass
#COND {^*$} {}
#COND {^(*)$}
{
  #IF (%begins(%1,"[Type")) {#ECHO OUT;#DELCLASS TestClass} {bagItem = %additem(%1,@bagItem);#STATE 2}
}
</value>
  <arglist>$container</arglist>
</func>
Works, the function creates a class that goes through any bag I give it and puts the contents in bagItem. Then the class turns around and deletes itself when it is done, basically only existing when it is called to do actual work. Of course the function itself returns nothing and I would be dependent on using the variable @bagItem to access the contents, instead of just remember to currentItemList=@ContainerList(backpack), which is what I was originally going for, no way for the triggers of the class to be inadvertently triggered, no more #T+ and #T-, no more #STATE 0, a black box, input; output, no idea what is going on behind the scenes or evidence of it. The addition of #DELCLASS TESTCLASS, causes an error [it runs through it anyway], which I assume means that any class that deletes itself would do the same... oh well, was an interesting thought.
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Wed Feb 23, 2011 1:30 pm   
 
It makes an error because it is trying to delete the class that the trigger itself is in, while the trigger is still running. If you aren't getting any output from it, there is really no reason to make this a function--it will work just as well as an alias. But why do you want to create and delete the trigger all the time instead of just enabling and disabling the class? Enabling a class is much more efficient than creating a trigger. Do you have a specific reason, or do you just dislike enabling and disabling?
Reply with quote
Ambelghan
Novice


Joined: 04 Apr 2010
Posts: 49

PostPosted: Fri Feb 25, 2011 4:10 am   
 
well with this method the triggers would only exist for the instant of the function, or alias, calling them. There is almost no chance of an accidental trigger and if I was performing some action where I needed to look into the same container, but for 2 different reason, I wouldn't need to worry about enabling and disabling classes, yes I know #T- and #T+ enables them but this seemed like an extra layer of protection.
Reply with quote
Myrkul
Wanderer


Joined: 21 Aug 2008
Posts: 85

PostPosted: Fri Feb 25, 2011 4:58 am   
 
[edit][/edit]
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net