Guest User


a guest
Jun 14th, 2012
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.44 KB | None | 0 0
  1. This
  2. ====
  5. I've made this short tutorial to give you a crash course on what exactly "this" or "_this" is.
  7. There are basically three places where you see them:
  9. In "init" fields of units
  10. In scripts
  11. In the condition fields of triggers & waypoints
  13. Each of these three areas covers a different skill level of mission editing, so this tutorial is for editors of a wide range of skill.
  15. If you are just starting out editing, you only need to understand the first part of the tutorial ("This" in Init Fields).
  16. If you are starting to get into advanced editing or scripting, you should read and understand the entire tutorial.
  17. There really isn't that much to it, because I'm only covering two little words...
  19. Now, let's get into it....
  23. "This" in Init Fields
  24. =====================
  26. First off, what is an "init" field?
  27. Basically, it is a place that you put little lines of code that will be run at the beginning of the mission.
  28. I'm sure you've seen many bits of code that you can put in init fields, to do different things with that unit, like:
  30. this setdammage 1 -> kills the unit
  31. this addweapon "m16" -> adds an m16 to the unit
  32. this moveindriver helo1 -> moves unit into driver seat of helo1
  34. Now, the question is, how come you have to put these lines in a unit's init field?
  35. Is there something special about the init field?
  36. Could I put something in one unit's init field, and have it affect another unit?
  37. Without getting into scripting, I'll just tell you that what you are looking at is almost exactly the same as the kinds of things you put in scripts.
  38. The difference is, yes, there is something special about the init field of a unit.
  39. And that thing is "this".
  41. "This" (or "this", or "tHiS"; capitalization doesn't matter) is a special word ("command"). When you put it in the "init" field of a unit, "this" is just another way of saying "the unit who's init field I'm editing right now".
  43. Lets look at one of the above lines again:
  45. this setdammage 1
  47. The line has three parts.
  48. The middle part is the command ("setdammage").
  49. What is put in the rest of the line, and what it all does, depends on the command you are using.
  50. In this case, the command will hurt a certain unit to a certain damage level.
  51. The right part of the line is what damage level we hurt the unit to (0 being no damage, 1 being full damage).
  52. The first part of the line is what unit we are going to hurt.
  54. In this case, we put "this" in as the "unit" we want to hurt.
  55. Since we are putting this in a unit's init field, that unit will be hurt.
  56. Again: "this" is another way of saying "the unit who's init field I am editing right now".
  58. Instead of using "this", we could simply name a unit (by entering something in his "name" box), and then use that name in one of the code bits we looked at above.
  59. So we could make a unit, and name him "bob".
  60. Then we could place a new unit, and in his init field, put:
  62. bob setdammage 1
  64. In this case, the first unit we placed ("bob") would be killed, NOT the unit who's init field we put that line in!
  65. So really, "this" is just a way to save us time, because it lets us refer to a unit without using his name.
  67. To give you an example of this, let's say we want to make all the units we place stand up, all the time.
  68. To do so, we could place a unit, name him "a", and then put this in his init field:
  70. a setunitpos "up"
  72. Then we could make a unit, name him "b", and put in his init field:
  74. b setunitpos "up"
  76. Then make a unit named "c", and one named "d", and so on.
  77. This would be a real pain, wouldn't it?
  79. Well, instead of doing all that, we could just make one unit, and put in his init field:
  81. this setunitpos "up"
  83. Then we could just copy and paste that unit to our heart's content, without ever changing his init field.
  84. Doesn't that seem easier?
  87. A last word on init fields:
  89. There is nothing else special about them, except that they run code at the start of the mission, and "this", which I just talked about.
  90. Lines of code that don't use "this" can be put anywhere, the only difference is when that code is run.
  91. Init fields always run at the start of the mission, while "on activation" fields of triggers will run when that trigger goes off.
  92. That is really all there is to know about "this" and init fields.
  93. You likely won't understand the rest of the tut unless you know a little about scripting. For those of you who do, I'll go on.
  97. "This" in Condition Fields
  98. ==========================
  100. Place a new trigger.
  101. Open it up, and all you will see in it's condition field is "this".
  102. So what the heck does that mean?
  104. It isn't the same as in init fields, that wouldn't make any sense.
  105. Well, you know all those conditions that you set in the "activation" part of the trigger? Those are "this".
  106. Okay, I know that doesn't make sense, so a little explanation is in order:
  109. The Quick and Dirty on Boolean Expressions
  110. ------------------------------------------
  112. The "condition" field uses something called a boolean expression.
  113. "Boolean" (named after the English mathematician George Boole) means that it is either TRUE or FALSE.
  115. You likely have seen something similar in algebra classes:
  117. x > 7
  119. In this case, the above expression is TRUE when x is greater than 7, and is FALSE when x is less than or equal to 7.
  120. Many OFP commands return "boolean" values (true or false).
  122. For example, "alive player" will return true when the player is alive, and false when he is dead.
  124. Boolean expressions can be more complicated than one little operation.
  126. That is where the commands "and" (same as "&&") and "or" (same as "||") come in.
  127. They mean the same thing as they do in english.
  129. "AND" means "this condition AND that condition must be true", while "or" means "this condition OR that condition must be true".
  131. For example:
  133. (x > 7) AND (alive player)
  135. The entire expression will return true when x is greater than 7, AND the player is alive.
  136. If either the left or the right statement is false, the expression as a whole will be false.
  137. "OR" works in a similar way:
  139. (x > 7) OR (alive player)
  141. This will be true when EITHER x is greater than 7, OR the player is alive.
  142. If either side (or both) of the expression is true, then the whole expression will be true.
  144. There is one last boolean operator, as they are called, in OFP: NOT (same as !).
  145. "NOT" will return the opposite of whatever is next to it.
  147. So for example:
  149. NOT (x > 7)
  151. The part in parenthesis (x > 7) will be true when x is above 7.
  152. However, since the NOT is there, the expression as a whole will be FALSE when x is above 7.
  154. Generally you should avoid using NOT if you can, since it can be confusing.
  155. In the above example, we could have just said (x <= 7).
  157. But sometimes we have to use NOT, like if we want to return true when the player is dead:
  159. !(alive player)
  161. There is no limit to how much you can use boolean operators.
  162. And remember, there are 2 different ways to "spell" each one (AND + &&, OR + ||, NOT + !).
  164. For example:
  166. ((x > 7) || (alive player)) && (!(alive bob))
  168. My last word of advise when using boolean expressions is to USE LOTS AND LOTS OF PARENTHESIS!
  169. Just like in normal math, there is an order of operations for boolean expressions.
  170. If you don't use parenthesis, the expression may be evaluated differently than you expect!
  173. Back to "This"
  174. --------------
  176. The condition field of triggers use boolean expressions, like we learned about above.
  177. The trigger will only "fire" once the boolean expression in it's condition field is true.
  179. So what about "this"?
  181. "This" refers to everything you set in the "activation" part of the trigger.
  182. If you make a trigger that is set for "west present", then "this" will become true once a west unit is in the trigger area.
  183. Normally there is only "this" in the condition field, so normally the trigger will only fire when the condition ("west present") is met.
  185. However, using the stuff we learned above, we can now make more complex triggers.
  186. For example, we could set a trigger to activate when you radio "alpha".
  188. In it's condition field, we could write:
  190. this && (alive bob)
  192. Now, the trigger will only "fire" when you radio, AND bob is alive.
  194. As you can see, you can now make much more complex conditions for your triggers!
  196. One last note is that if you erase "this" entirely from the condition field, it then doesn't matter what properties you set in the 'activation' section of the trigger, it will only depend on what is in the condition field.
  199. Waypoints are very similar.
  200. With waypoints, "this" is true when the unit goes to the waypoint.
  202. So if we made a waypoint with the condition:
  204. this && (alive bob)
  206. The waypoint would only be "completed" (meaning the unit will go to his next waypoint, and the 'on activation' field of the waypoint will be executed) when the group goes to the waypoint, AND bob is alive.
  207. If bob died before the group made it to the waypoint, then the waypoint would never be completed, and the group would never move on.
  209. Simple, right?
  210. Well, now you know all there is to know about condition fields and using "this" in them.
  211. You'll be happy to know that boolean expressions are also HIGHLY useful in scripting (in the ? command), so if you didn't know about them or understand them before, you are now ready to learn more about scripting from your favorite scripting tutorial.
  213. Now, onto the next section, which deals exclusively with scripts.
  217. "_This" in Scripts
  218. ==================
  220. Alright, if you're still with me, it means you have started to make your own scripts.
  221. That's good; now I'll clear up something that seems to be confusing for most beginning scripters, and is even slightly misunderstood by many experienced scripters.
  223. In every script, we have access to a special variable called "_this".
  225. What is held in that variable changes depending on how you call the script, and by using and understanding that variable, we can move beyond just making a list of commands, and into really making a script.
  227. First, lets take an example:
  229. player exec "myscript.sqs"
  231. That will execute myscript.sqs.
  232. Now in that one instance (a script can be run multiple times at the same time, each one that is running is called an "instance") of myscript.sqs, the variable "_this" will refer to the player.
  233. Why?
  234. Because the player ("player") is what we put next to "exec"; so that is what is passed into "_this".
  236. Notice that "_this" has an underscore next to it, which means that it is a local variable.
  238. If you don't understand what that means, you need to go back and read your scripting tut again, because it is CRUCIAL that you understand the difference between a local variable and a global variable.
  240. I'll say it again, whatever you put next to "exec" when executing a script, will be passed to the script in the variable "_this".
  242. One important thing worth noting, is that if you put a variable next to "exec", the value currently in that variable will be sent to the script (not the actual variable).
  244. For example:
  246. VAR exec "myscript.sqs"
  248. Will pass whatever the current value of VAR is to the script.
  250. If you then make changes to that value (_this) in the script, VAR will not change.
  251. Likewise, if VAR changes after you run the script, the value in the script (_this) will not change either.
  253. You can put any type of variable next to exec.
  254. An object, a number, an array, etc.
  255. Most of the time, arrays are used, because you can then pass more than one piece of data to the script.
  257. For example, if we say:
  259. [player, 5] exec "myscript.sqs"
  261. Then "_this" in that instance of the script will be an array, that holds the player and the number 5.
  262. If we want to access certain elements in the array, we need to use the "select" command.
  264. For example, to get the first element of the array (the player, in this case), we need to type:
  265. _this select 0
  267. Usually (but not always) you will want to save all the elements in the "_this" array to new variables, to make things easier for yourself, which is why most scripts look something like this:
  269. _unit = _this select 0
  270. _count = _this select 1
  272. But keep in mind that you don't have to 'extract' all the elements of the array to their own variables.
  273. If you only use a certain part of the array once or twice, it is better to not make a new variable, because variables just take up CPU power, which believe it or not makes a difference when you are using lots of scripts.
  275. So, in the above example, if we only needed to use "_count" one time, we could do it like this:
  277. _max = 2*(_this select 1)
  279. One final thing, is that functions use _this just like scripts do, as you will see if you begin writing functions.
  282. Special Scripts and _This
  283. =========================
  285. There are two special scripts where you can't specify the value of _this, because it is defined for you.
  287. First, there are scripts run by actions, added via the addaction command.
  288. For these scripts, _this is always an array with three elements:
  290. The object the action was attached to
  291. The unit that used the action
  292. The index of the action
  294. Next, there are eventhandlers.
  296. Each different eventhandler has different values for _this.
  300. The Last Word
  301. =============
  303. I hope you found this tutorial useful.
  304. It is the first OFP tutorial I have written, but I hope it is understandable.
  306. Please post some comments and suggestions, such as what was unclear, what needs to be explained better, what to add, etc, so that I can revise the tut in the future.
  308. Have fun editing!
Add Comment
Please, Sign In to add comment