Alex or others: Functions/Actions clarification please
sbangs
04 Dec 2010, 07:56Hi Alex, you made the below comment in another thread I found in a search. I have some questions on those statements.
A. Functions are useful if you want to re-use the same script in multiple places, or maybe just split your scripts up so they are smaller, more self-contained and therefore easier to edit. Actions are useful if you want different script for different objects. For example, when the player eats an apple, a different thing happens to when the player eats poison. You can create "eat" actions for the apple and the poison, and call these actions from an "eat" command.
Functions: re use the same script in multiple places (can you give an example?), what do you mean, split up your scripts (example)?
Actions: Ok using "eat" for diff results depending on eating and apple and poison. Can you explain how it differs or saves time using an action, verses just adding a "eat" verb to the object in question with the appropriate result? I mean, i guess I am always thinking I am using diff scripts for diff objects. Can you give an example I guess?
The more I read what I'm asking and what I'm reading, I think I am just asking for some good examples of these different "methods" of scripting.
Procedures vs functions vs command vs actions and so on.
Thanks for your time and help.
Scott
A. Functions are useful if you want to re-use the same script in multiple places, or maybe just split your scripts up so they are smaller, more self-contained and therefore easier to edit. Actions are useful if you want different script for different objects. For example, when the player eats an apple, a different thing happens to when the player eats poison. You can create "eat" actions for the apple and the poison, and call these actions from an "eat" command.
Functions: re use the same script in multiple places (can you give an example?), what do you mean, split up your scripts (example)?
Actions: Ok using "eat" for diff results depending on eating and apple and poison. Can you explain how it differs or saves time using an action, verses just adding a "eat" verb to the object in question with the appropriate result? I mean, i guess I am always thinking I am using diff scripts for diff objects. Can you give an example I guess?
The more I read what I'm asking and what I'm reading, I think I am just asking for some good examples of these different "methods" of scripting.
Procedures vs functions vs command vs actions and so on.
Thanks for your time and help.
Scott
Alex
04 Dec 2010, 16:55Let's start with procedures first.
If you ever find yourself entering the same load of script commands in multiple places, you can make things easier for yourself by putting that script into a procedure. Then you can call that script from multiple places, and if you need to change it you only need to change it in the one place.
Procedures are covered in section 12.6 of the tutorial. In that example, we use a procedure so that we don't have to repeat ourselves when we want to make "use defibrillator" do the same thing as "use defibrillator on bob".
What I mean by "splitting up scripts" is that if you have big series of nested "if" commands, you might want to split some of them out into procedures so that you don't have to open up loads of windows to get to the inner parts - you can just edit the procedure instead.
A function is like a procedure, except that it returns a value. So instead of just calling the procedure somewhere, you can embed it in a "print" script. Functions are covered briefly in section 22.3 of the tutorial.
Regarding actions, you're correct that in the simple case of eating apples and poison, you'd just add an "eat" verb to those objects. But actually by doing that you ARE creating an action! That's how verbs work internally. An "eat" verb looks for an "eat" action on an object, and runs it if it finds one.
You might set up an action manually if it's not something that would be directly done by a verb - maybe you can think of an action as being like a "script verb". Maybe you have a timer, and after a while the food in your game decomposes. Then your timer can call the "decompose" action of various objects. So the apple has a "decompose" action that relates only to the apple, and you don't have to clutter up other parts of your game code - all the script that relates to the apple can be part of the apple definition.
If you ever find yourself entering the same load of script commands in multiple places, you can make things easier for yourself by putting that script into a procedure. Then you can call that script from multiple places, and if you need to change it you only need to change it in the one place.
Procedures are covered in section 12.6 of the tutorial. In that example, we use a procedure so that we don't have to repeat ourselves when we want to make "use defibrillator" do the same thing as "use defibrillator on bob".
What I mean by "splitting up scripts" is that if you have big series of nested "if" commands, you might want to split some of them out into procedures so that you don't have to open up loads of windows to get to the inner parts - you can just edit the procedure instead.
A function is like a procedure, except that it returns a value. So instead of just calling the procedure somewhere, you can embed it in a "print" script. Functions are covered briefly in section 22.3 of the tutorial.
Regarding actions, you're correct that in the simple case of eating apples and poison, you'd just add an "eat" verb to those objects. But actually by doing that you ARE creating an action! That's how verbs work internally. An "eat" verb looks for an "eat" action on an object, and runs it if it finds one.
You might set up an action manually if it's not something that would be directly done by a verb - maybe you can think of an action as being like a "script verb". Maybe you have a timer, and after a while the food in your game decomposes. Then your timer can call the "decompose" action of various objects. So the apple has a "decompose" action that relates only to the apple, and you don't have to clutter up other parts of your game code - all the script that relates to the apple can be part of the apple definition.