Page 1 of 1
Basic block question.
Posted: Sun May 30, 2010 1:42 pm
by ptbptb
OK, I'm just confirming things here really, but say you had code like ...
Code: Select all
(if (eq (random 0 50))
(plyMessage gplayer
(cat "Adding " (itmGetName tempItem 8) " to station stocks.")
)
(lnkAppend itemList tempItem)
)
If you want to be sure (plyMessage) runs before (lnkAppend) then you have to use a (block) function?
Code: Select all
(if (eq (random 0 50))
(block Nil
(plyMessage gplayer
(cat "Adding " (itmGetName tempItem 8) " to station stocks.")
)
(lnkAppend itemList tempItem)
)
)
Like that?
Posted: Sun May 30, 2010 2:08 pm
by Prophet
there are a few errors in that code.
First off, (eq (random 0 50)) is not a valid boolean statement.
eq requires 2 parameters to compare and (random 0 50) is only one.
(eq (random 0 50) 50) is valid
Second, the 'if' function must have 2 parameters with an optional third
(if (boolean) param1 optionalParam2)
if the boolean evaluates to True, param1 is run, if it evals to Nil the optionalParam is evaluated.
so, if you want to send the message AND modify the list when the IF statement is true, then wrap both of those functions in the block:
Code: Select all
(if (eq (random 0 50) 50)
(block Nil
(plyMessage ....)
(lnkAppend ....)
)
)
I hope that makes sense

Posted: Sun May 30, 2010 2:12 pm
by ptbptb
Prophet wrote:I hope that makes sense

Yeah, thanks. ^ ^;
Er, so apart from choosing a really bad example _and_ making a mistake in the code - what about my original question?
The block function description says ... "Allows you to run several expressions one right after the other."
Which makes is sound like if you have
(function_a stuff)
(function_b stuff)
there is a chance that function_b will run before function_a or something.
Posted: Sun May 30, 2010 2:39 pm
by Prophet
All code is evaluated from left to right, top to bottom, inside out.
So,
(block nil
(function1)
(function3 (function2))
)
A function is finished evaluating when you reach it's end bracket.
the functions are named in the order that they are evaluated, function2 is a parameter of function3 in this example so it completes its evaluation before function3. (function2's end bracket appears before function3's)
They will ALWAYS run in that order (infact, they must)
Posted: Sun May 30, 2010 2:47 pm
by alterecco
The block statement basically acts as a wrapper around a series of expressions, turning them all into one big expression.
(if ..) is actually a really good example. If you take the followin common if statement, where you only want to evaluate something when the condition is True:
Code: Select all
(if (eq (random 0 1) 1)
(some_expression_to_run_if_true)
(some_further_expression_to_run_if_true)
)
This expression actually turns into an (if ..) with both a True and Nil statement. The only way to get the two expressions to run in the same condition, is to wrap them in a (block ..) effectively turning them into one big expression:
Code: Select all
(if (eq (random 0 1) 1)
(block Nil
(some_expression_to_run_if_true)
(some_further_expression_to_run_if_true)
)
)
I guess this is basically what Prophet has been saying also

Posted: Mon May 31, 2010 1:22 am
by Atarlost
ptbptb wrote:Prophet wrote:I hope that makes sense

Yeah, thanks. ^ ^;
Er, so apart from choosing a really bad example _and_ making a mistake in the code - what about my original question?
The block function description says ... "Allows you to run several expressions one right after the other."
Which makes is sound like if you have
(function_a stuff)
(function_b stuff)
there is a chance that function_b will run before function_a or something.
What will happen usually is that (function_b stuff) won't run at all. Sometimes if it's an argument to another function it will be run incorrectly or fail to compile.