[F-Script-talk] Re: Problem with variables defined in block
Brought to you by:
pmougin
From: Philippe M. <pm...@ac...> - 2003-09-15 21:20:56
|
Dear Knud, You get the expected behavior. However, note that, actually, it is not the fact that you load the =20 source code of the block from a file that gets you this behavior but =20 the use of the "asBlock" method on a string. As stated in the manual: =20= "... each block created by this method is in charge of its own =20 top-level workspace". In other words, the block created by this method =20= has no knowledge of your F-Script session's workspace. Instead, it runs =20= in its own workspace. This may seems surprising at first. To better understand the rational =20= of this behavior, ask yourself: how could the NSString asked to create =20= a block (with the 'asBlock' method) know about your F-Script session? =20= The answer is: it can't. Your NSString is an Objective-C object, like =20= every object manipulated with F-Script. When it receives the 'asBlock' =20= message, it doesn't know that this message comes from an F-Script =20 interpreter. Indeed, you can also directly send this message from an =20 Objective-C program as well. What you need in order to achieve what you want is a way to ask the =20 current F-Script interpreter to create a block configured to use the =20 current workspace. By chance, the pre-defined identifier 'sys' refers =20= to an object (of class System) which represent the current F-Script =20 interpreter, and provides the method you want (i.e., blockFromString:). =20= Thus, if you write "sys blockFromString:codeFromFile", a new block will =20= be created which will be configured to use the workspace of the =20 interpreter represented by the receiver (i.e., the current F-Script =20 interpreter). Finally, I should add that you can directly save and load blocks =20 to/from files. See section 9 in the manual. Hope this clarify things. BTW, feel free to let us know about your game and how you use F-Script =20= in this project. Best, Phil Le lundi, 15 sep 2003, =E0 19:37 Europe/Paris, Knud M=F6ller a =E9crit : > Hi, > > I'm new to fscript, and stumbled upon something that really puzzles =20= > me. When I define a block directly from the prompt within the =20 > fscript-application, and define a variable within that block, the =20 > variable will be visible outside the block, too (after the block has =20= > been sent 'value'). This is as I expected it. See this session for =20 > example: > > --------------------------------------------------------------------- > > blockFromPrompt :=3D [ otherString :=3D 'this works!'. ] > > > blockFromPrompt value > > > otherString > 'this works!' > --------------------------------------------------------------------- > > However, when I load a block from a file, any variable defined in this = =20 > block will NOT be visible outside. Compare this: > > --------------------------------------------------------------------- > > codeFromFile :=3D NSString alloc initWithContentsOfFile: =20 > '/Users/dunk/Desktop/testScript.fscript' > > > blockFromFile :=3D codeFromFile asBlock > > > blockFromFile > [ > someString :=3D 'test me!'. > ] > > > blockFromFile value > > > someString > > error: undefined identifier "someString" > --------------------------------------------------------------------- > > Why is the 'someString' variable not visible, just as the =20 > 'otherString' variable? Is this a bug or a feature? If it is a =20 > feature, I don't really get it. Someone please enlighten me! =3D8-) > > Apart from that, I think that fscript is very cool! I plan to use it =20= > in a game project of mine. > > Cheers, > Knud > > PS: I use version 1.2.4 > > = -----------------------------------------------------------------------=20= > -------- > Knud M=F6ller - Institut f=FCr Sprachliche Informationsverarbeitung, > Universit=E4t zu K=F6ln > (Department of Linguistic Data Processing, University of Cologne, =20 > Germany) > http://www.spinfo.uni-koeln.de/~kmoeller |