Thread: [F-Script-talk] Problem with variables defined in block
Brought to you by:
pmougin
From: <kmo...@sp...> - 2003-09-15 17:36:52
|
Hi, I'm new to fscript, and stumbled upon something that really puzzles me. =20= 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 'otherString' =20= variable? Is this a bug or a feature? If it is a feature, I don't =20 really get it. Someone please enlighten me! =3D8-) Apart from that, I think that fscript is very cool! I plan to use it in =20= 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 |
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 |
From: <kmo...@sp...> - 2003-09-16 09:16:19
|
Hi, thanks for the quick and detailed answer. It really helped me to =20 understand what is going on behind the scenes a little bit better and =20= solves my problem. Am Montag, 15.09.03, um 23:20 Uhr (Europe/Berlin) schrieb Philippe =20 Mougin: > Finally, I should add that you can directly save and load blocks =20 > to/from files. See section 9 in the manual. Yes, I had seen that. but [Block save/load] will serialize/deserialize =20= the Block-object, and not save a text representation, correct? And =20 since I want to be able to generate and manipulate my scripts in a text =20= editor, I think a cannot use this method. > BTW, feel free to let us know about your game and how you use F-Script = =20 > in this project. Well, it's nothing professional, but a project I want to submit to a =20 Macintosh game programming contest called uDevGames (see =20 http://www.idevgames.com/content/contest.php?id=3D10). The game ("Retro =20= Maze") is an oldfashioned role playing game with a very simple, tile =20 based 3d-engine. The game's basic framework is done i Cocoa/ObjC. Now I =20= plan to use fscript to glue everything together. More important, I want =20= to attach scripts to various game objects (tiles, monster, walls, ...). =20= These scripts will be executed whenever some condition is met (e.g. a =20= tile's script is executed when the player walks over it) and provide =20 the game with the actual action. I guess I could have used just about any scripting language to do that. =20= However, fscript caught my eye through the articles on the =20 O'Reilly-network. It looked different, but very interesting and elegant =20= - I was really fascinated by the fact that control structures are =20 implemented using blocks and messages! Since portability to other =20 platforms is not a concern for me, it doesn't matter that fscript is =20 only available on OSX at the moment. Merci bien, Knud ------------------------------------------------------------------------=20= ---------------- Knud M=F6ller - Institut f=FCr Sprachliche Informationsverarbeitung, Universit=E4t zu K=F6ln (Department of Linguistic Data Processing, University of Cologne, Germany) http://www.spinfo.uni-koeln.de/~kmoeller ------------------------------------------------------------------------=20= ---------------- |
From: Philippe M. <pm...@ac...> - 2003-09-17 17:50:10
|
Le mardi, 16 sep 2003, =E0 10:56 Europe/Paris, Knud M=F6ller a =E9crit : >> Finally, I should add that you can directly save and load blocks=20 >> to/from files. See section 9 in the manual. > > Yes, I had seen that. but [Block save/load] will serialize/deserialize=20= > the Block-object, and not save a text representation, correct? Yes you're right. I hope your game will do well! Best, Phil= |