Joined: Jan 09, 2004 Posts: 781 Location: Monterey, CA USA
Posted: Sat Apr 07, 2007 8:38 pm Post subject: TBA Trigger of the Day 070407 Beginner Switch Example
All these trigger posts sparked a few ideas, so as the topic implies here is the plan:
Every day anyone who wants to can post or request a trigger of the day (just make sure trigger of the day is in the topic, also include beginner, intermediate, advanced). Now the point of this is to demonstrate common ways to use trigedit AND explain how it is done.
Again, anyone can post an example or if they have a trigger idea they want implemented they can post it for others to write a trigger for. This is meant as a community project so the more participation we have the more ideas everyone can come up with. We still have builders on TBA dreaming up new ways to use trigedit all the time.
Some Example Requests:
Give me the best object trigger that incorporates case/switch that you can.
I want a weapon that levels up as it is used, give me a way to do this.
This will be a free for all. First person to post for the day wins. But, there is nothing saying we can't post more than one a day. Also, please reply to the post with feedback if you have another idea or way to do the trig. _________________ Rumble
The Builder Academy
tbamud.com 9091
Last edited by Rumble on Sun Apr 08, 2007 12:23 pm; edited 2 times in total
Joined: Jan 09, 2004 Posts: 781 Location: Monterey, CA USA
Posted: Sat Apr 07, 2007 8:39 pm Post subject:
Quote:
Always use names that explain your trigger and include the VNUM for the
mob/obj/room the trigger is going to be attached to. It is important to give
your trigger a name you and others can understand. This way others can search
"vnum trigger <keyword>." This will also make it easier when using tlist and
tstat. Allowing others to quickly comprehend what the trigger does. Here are
some good examples:
Princess Shout for Help - M2229 --> Trigger 2204 attached to mob 2229.
Tutorial Example Quest Offer - M14 --> Trigger 1 attached to mob 14.
No Recall - R0 --> Trigger 1201 attached to room 0.
Code:
Name: 'Obj Command Magic Eight Ball - O47', VNum: [ 6], RNum: [ 6]
Trigger Intended Assignment: Objects
Trigger Type: Command , Numeric Arg: 2, Arg list: shake
Commands:
* By Rumble of The Builder Academy builderacademy.net 9091
* Numeric Arg: 2 means in character's carried inventory.
* Command trigs do not work for level 32 and above.
* There are 20 possible answers that the Magic Eight Ball can give.
* Of these, nine are full positive, two are full negative, one is
* mostly positive, three are mostly negative, and five are abstentions.
*
* Check arguments if they match. /= checks abbreviations.
if ball /= %arg% || eightball /= %arg%
* Echo text to everyone else in the room and the actor.
%echoaround% %actor% %actor.name% shakes %self.shortdesc% vigorously.
%send% %actor% You shake %self.shortdesc% vigorously.
* Use a switch to choose a random response (1-20).
switch %random.20%
* Send the answer! %self% is the 8ball, or whatever the trig is attached to.
* Only the actor sees the answer.
* Case is what we are trying to match. Does %random.20% == 1?
case 1
%send% %actor% %self.shortdesc% reveals the answer: Outlook Good
* We are done with this case so check the next one.
break
case 2
%send% %actor% %self.shortdesc% reveals the answer: Outlook Not So Good
break
case 3
%send% %actor% %self.shortdesc% reveals the answer: My Reply Is No
break
case 4
%send% %actor% %self.shortdesc% reveals the answer: Don't Count On It
break
case 5
%send% %actor% %self.shortdesc% reveals the answer: You May Rely On It
break
case 6
%send% %actor% %self.shortdesc% reveals the answer: Ask Again Later
break
case 7
%send% %actor% %self.shortdesc% reveals the answer: Most Likely
break
case 8
%send% %actor% %self.shortdesc% reveals the answer: Cannot Predict Now
break
case 9
%send% %actor% %self.shortdesc% reveals the answer: Yes
break
case 10
%send% %actor% %self.shortdesc% reveals the answer: Yes, definitely
break
case 11
%send% %actor% %self.shortdesc% reveals the answer: Better Not Tell You Now
break
case 12
%send% %actor% %self.shortdesc% reveals the answer: It Is Certain
break
case 13
%send% %actor% %self.shortdesc% reveals the answer: Very Doubtful
break
case 14
%send% %actor% %self.shortdesc% reveals the answer: It Is Decidedly So
break
case 15
%send% %actor% %self.shortdesc% reveals the answer: Concentrate And Ask Again
break
case 16
%send% %actor% %self.shortdesc% reveals the answer: Signs Point To Yes
break
case 17
%send% %actor% %self.shortdesc% reveals the answer: My Sources Say No
break
case 18
%send% %actor% %self.shortdesc% reveals the answer: Without A Doubt
break
case 19
%send% %actor% %self.shortdesc% reveals the answer: Reply Hazy, Try Again
break
case 20
%send% %actor% %self.shortdesc% reveals the answer: As I See It, Yes
break
* Every switch should have a default. A catch-all if the cases do not match.
default
%send% %actor% %self.shortdesc% explodes since your question is unanswerable.
break
* Every switch must have a done! Just like every if needs an end!
done
* The actor didn't use the command shake with arg ball or eightball.
else
* Return 0 allows the command to continue through to the MUD. The player will
* get the Huh!?! response or the shake social if you have one.
return 0
end
_________________ Rumble
The Builder Academy
tbamud.com 9091
Joined: Mar 29, 2007 Posts: 89 Location: Virginia, USA
Posted: Sat Apr 07, 2007 10:39 pm Post subject:
I hope everyone gets involved with this nomatter how simple or complex they think their trigger is. I will be posting a few things I have made over the years hope the other regulars will do the same so we can get the rolling.
Joined: Jun 18, 2006 Posts: 1685 Location: Halesowen, England
Posted: Sun Apr 08, 2007 6:37 am Post subject:
This is a great idea!
I have learned over the years that one of the fastest ways to learn is by example. When I learned coding, a lot of what I did was copy/pasting chunks of code and altering them slightly.
I do not yet have the skills needed to just write a trigger without referring to references and examples, and as an experienced C coder, I find this frustrating, so I want to learn everything there is to know about triggers.
In many ways it is a lot like C (if statements, and switch statements), but in many ways is very different (enclosing variables in %% and the set command) - I need to learn when it appropriate to use set, and when to use eval. These things come naturally to anyone who has been scripting for a while, but to me, it's like learning C when all I knew was BASIC!
I have seen some fabulous scripts (I love the Sorceress of the Underworld in the Memlin Caverns - when attacked, turns into a staff, which can't be picked up, for 5 mins) and can see the immense possibilities for a wonderfully interactive MUD. I'm sure I'll be using scripts MUCH more in future... _________________
Joined: Mar 29, 2007 Posts: 89 Location: Virginia, USA
Posted: Sun Apr 08, 2007 10:52 am Post subject:
As to when to use set and when to use eval...
If you are setting a variable to a specific value or string ex set is_open 1 or set name Dingodove, you use set. If you are putting an expression into a variable you use eval, for example eval damage 1 + %actor.maxhit% or eval outcome %random.10% + 100. Hope that helps.
Joined: Mar 31, 2004 Posts: 316 Location: New Zealand
Posted: Wed Apr 11, 2007 2:54 am Post subject: Quick examples of switch.
Switch can be used in multiple ways.
For example, you may want to check if a player said a certain word in a long list of words.
Some builders may tackle this challange by using an IF statement.
Which, is the right thing to do, when you have only a few comparisons you want made.
For example:
Code:
if %actor.name% == Polly || %actor.name% == Sue
%echo% You are polly or sue!
else
%echo% You aren't polly or sue
end
If you have a lot of names, or fruit, or spells, or keywords you want to compare then using an IF statement can look messy, AND it is hard to update using the OLC editor.
Code:
switch %actor.name%
case Polly
case Sue
case Mary
case Albert
%echo% You are a name in my first list!
%echo% You can have a Key.
%load% obj 123
break
case Eugine
%echo% You are a different name.
%echo% You can have a locked box that needs a Key.
%load% obj 124
break
Default
%echo% You are not on my list on names!
%echo% You can have a small mountain of untouchable gold
%load% obj 125
break
done
Switch is basically used to compare one value to many values.
You can put multiple CASE statements above each other, which means that the SWITCH will compare the value to all those CASEs, as i did in the example above.
A SWITCH block is the area of a script, that starts with SWITCH and ends with DONE.
When a SWITCH finds a candidate that matches its key, it will execute the ACTIONS below the CASE's.
If the CANDIDATE has more cases below it, the SWITCH's logic will FALL THROUGH the other CASEs to the ACTIONS part, that are above a break.
Code:
Switch KEY
case CANDIDATE
case CANDIDATE
*actions
break
case CANDIDATE
*actions
break
case CANDIDATE
*actions
break
default
*actions
break
done
What happens here?
Code:
Switch apple
case pear
case orange
case pineapple
%echo% Keyboards are an input device
break
case cheese
case apple
case wine
%echo% Monitors are an output device
break
done
What you would see when this script was run is:
Code:
Monitors are an output device
You can use numbers for comparison also in switch's.
Code:
switch %random.5%
case 1
case 2
%echo% The random number landed on 1 or 2
break
case 3
%echo% The random number landed on 3
break
default
%echo% The random number wasn't 1, 2 or 3.
break
done
You may have noticed the
Code:
default
Lines in the SWITCH blocks.
The DEFAULT line is like a CASE line.
Only, it doesnt have an argument, and DEFAULT actions will be gone to if the SWITCH can't find a matching CASE candidate.
If a SWITCH block doesn't have a DEFAULT entry, then when a CASE isn't found, the SWITCH block will end.
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