Welcome to the Builder Academy

Question Trigger of the Day - Eval and Set Usage

More
29 Jul 2013 00:53 #3613 by Rumble
Code:
Name: 'Eval and Set Example', VNum: [ 49], RNum: [ 49] Trigger Intended Assignment: Rooms Trigger Type: Speech , Numeric Arg: 100, Arg list: test Commands: * By Rumble of The Builder Academy tbamud.com 9091 * This is a speech trig @RHELP TRIGEDIT ROOM SPEECH@n, say 'test' to activate. * There is much confusion about the difference between set and eval. So this is * the simplest way I can think of to explain it (assume %actor.level% = 34): * * Set stores the variable and will not process it until called. * In the example below %set_variable% will contain '34 + 1' set set_variable %actor.level% + 1 %echo% Set Variable: %set_variable% * * Eval immediately evaluates the variable. * In the example below %eval_variable% will contain '35' eval eval_variable %actor.level% + 1 %echo% Eval Variable: %eval_variable% * %echo% Level: %actor.level%
This trig outputs:

You say, 'test'
Set Variable: 34 + 1
Eval Variable: 35
Level: 34

Mordecai went even more indepth for power users:

Mordecai wrote: Eval and set are brilliantly different.

When you want to preserve the layout of a string, you can use set.
When you want to evaluate a string into a result, you can use eval.

For a simple example:

Code:
set a 1. Listen to your mum eval b 1. Listen to your mum %echo% a: %a% %echo% b: %b%
Depending on your version you will see.
a: 1. Listen to your mum
b: 1

Another example:
Code:
set a 60 ****** 60 ******* 60 eval b 60 ****** 60 ******* 60 %echo% a: %a% %echo% b: %b%
You will see:
a: 60 ****** 60 ******* 60
b: 0

Another example for the more advanced among you:
Code:
set noise woof set a %%%%noise%%%% eval b %%%%noise%%%% %echo% a: %a% %echo% b: %b%
You will see:
a: %%noise%%
b: %noise%
Which is a big difference there!

Again with a small difference:
Code:
set noise woof set woof pineapple set a %%%noise%%% eval b %%%noise%%% %echo% a: %a% %echo% b: %b%
Which displays as:
a: %woof%
b: pineapple

Eval also uses more processing power of the server too. So it really pays to know what set and eval do when you are starting to do more and more complex scripts.

If this was helpful please vote or leave a review for TBA.

Rumble
The Builder Academy
tbamud.com 9091
rumble@tbamud.com
The following user(s) said Thank You: thomas

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

Time to create page: 0.202 seconds