2002
« on: July 01, 2016, 09:29:23 pm »
I prepare short tutorial for OXCE scripts. Before expanding it more I would like hear some feedback.
Is it easy to understand? What you would want be more elaborate? Anything is missing?
Tutorial of using scripts
List of available function and vales in script is available in log file with debug mode enabled.
Assume you want add reaction to melee attacks.
Then you can do it in two ways:
#script defined per item
items:
- type: STR_WEAPON_WITH_MELEE
scripts:
reactionWeaponAction: |
if eq action action_hit; #when you hit with this weapon enemy can react to it.
return 100;
else;
return reaction_chance;
end;
#script defined for all items
extended:
scripts:
offset: 1.0
reactionWeaponAction: |
if eq action action_hit; #all items used as melee will case enemies to react.
return 100;
else;
return reaction_chance; #return old reaction chance
end;
every thing after `#` is comments, exactly same as in yaml.
each operation need end with `;`.
`if` is condition operation. It will control what code will be executed.
someScriptCode: |
if OPERATION A B; #OPERATION can be: `lt` - less, `le` - less equal, `gt` - greater, `ge` - greater equal, `eq` - equal, `neq` - not equal
#some code 1
else OPERATION A B; #`else` can have same condition as first `if`.
#some code 2
else OPERATION A B; #you can repeat `else` with test multiple times.
#some code 3
else;
#final case, can be skipped.
end;
`reaction_chance` in `reactionWeaponAction` is current percent chance of enemy to react. You can modifi it and return it:
reactionWeaponAction: |
var int shade; #definition of new variable that can be used by script.
BattleUnit.getTileShade action_unit shade; #getting shade of tile that unit is standing on.
if gt shade 10; #shade of tile is greater than 10.
div reaction_chance 2; #now is two time less chance to react.
end;
div distance 16; #distance between units is in vexels, dividing by 16 give us distance in tiles.
if gt distance 10; #distance is greater than 10 tiles.
sub reaction_chance 30; #subtracting
end;
return reaction_chance;
Now if acting unit is in dark place (unit lighting count!) and 10 tiles away, then enemy units will have only 20% chance to
react (20 = 100/2 - 30).
We can define variables, each variable have fixed type and can't store anything else:
someScriptCode: |
var int var_name;
`var` mean that we create new variable. `int` is type of it, in this case this is number.
`var_name` is name of variable. New variable start with empty value, in case of `int` this is 0.
You can set different value when you define it:
someScriptCode: |
var int answer_to_every_thing 42; #42 is starting value of this variable.
set answer_to_every_thing 13; #it was 42 but now is 13.
We have other types than `int`. They are pointers to object from OpenXcom engine.
someScriptCode: |
var int unit_size;
var ptr BattleUnit unit; #read only pointer to battle unit, this mean unit that you can see in battlescape.
var ptr RuleArmor unit_armor; #read only pointer to armor that unit can use and what is defined in `armors` node.
set unit action_unit; #now `unit` and `action_unit` represents same unit.
BattleUnit.getRuleArmor unit unit_armor; #we get pointer to armor of that unit.
unit.getRuleArmor unit_armor; #shortcut of previous operation.
unit_armor.getSize unit_size; #now we have size of unit armor in variable `unit_size`.