Basic block question.

This is a moderated forum that collects tutorials, guides, and references for creating Transcendence extensions and scripts.
Post Reply
User avatar
ptbptb
Militia Lieutenant
Militia Lieutenant
Posts: 143
Joined: Mon May 10, 2010 7:34 pm

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?
User avatar
Prophet
Militia Captain
Militia Captain
Posts: 826
Joined: Tue Nov 18, 2008 6:09 pm

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 :)
Coming soon: The Syrtian War adventure mod!
A Turret defense genre mod exploring the worst era in Earth's history.
Can you defend the Earth from the Syrtian invaders?
Stay tuned for updates!
User avatar
ptbptb
Militia Lieutenant
Militia Lieutenant
Posts: 143
Joined: Mon May 10, 2010 7:34 pm

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? :oops:

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.
User avatar
Prophet
Militia Captain
Militia Captain
Posts: 826
Joined: Tue Nov 18, 2008 6:09 pm

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)
Coming soon: The Syrtian War adventure mod!
A Turret defense genre mod exploring the worst era in Earth's history.
Can you defend the Earth from the Syrtian invaders?
Stay tuned for updates!
User avatar
alterecco
Fleet Officer
Fleet Officer
Posts: 1658
Joined: Wed Jan 14, 2009 3:08 am
Location: Previously enslaved by the Iocrym

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 :)
User avatar
Atarlost
Fleet Admiral
Fleet Admiral
Posts: 2391
Joined: Tue Aug 26, 2008 12:02 am

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? :oops:

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.
Post Reply