User Tools

Site Tools


hpl3:engine:script

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
hpl3:engine:script [2013/07/19 06:52]
nebej [Saving]
hpl3:engine:script [2014/02/18 07:24]
thomas [Comments]
Line 22: Line 22:
 ||m|| a member variable || class cMyClass {\\ int mlMember;\\ } || ||m|| a member variable || class cMyClass {\\ int mlMember;\\ } ||
 ||g|| a global variable, defined outside of a class or function.|| int glMyGlobal; \\ class cMyClass {} || ||g|| a global variable, defined outside of a class or function.|| int glMyGlobal; \\ class cMyClass {} ||
 +||id|| this is for tID type.|| tID m_idEntity ||
  
 a variable name always starts with a lower case letter, so anything of type not specified must start with a lower case word. Example: a variable name always starts with a lower case letter, so anything of type not specified must start with a lower case word. Example:
Line 61: Line 62:
 cMonster@ pMonster = CurrentHit();​ cMonster@ pMonster = CurrentHit();​
 float fDamage = RandomDamage();​ float fDamage = RandomDamage();​
-fDamage -= pMonster->Defense();+fDamage -= pMonster.Defense();
 if(fDamage<​0) fDamage =0; if(fDamage<​0) fDamage =0;
 fDamage *= mfHitMultiplier;​ fDamage *= mfHitMultiplier;​
-pMonster->DecHealth(fDamage);​+pMonster.DecHealth(fDamage);​
 </​code>​ </​code>​
  
Line 75: Line 76:
 float fDistSqr = vDiff.x*vDiff.x +vDiff.y*vDiff.y;​ float fDistSqr = vDiff.x*vDiff.x +vDiff.y*vDiff.y;​
 </​code>​ </​code>​
 +
 +Only do this in rare cases when it is really hard to figure what the code means. Normally, simply using section comments is enough. If you split the code into nice sections and name them well, that should be more than enough for other people to figure out what it is about.
  
  
Line 230: Line 233:
  
  
-This saves a unqiue identifier to the body. There are script functions for converting the ID to a class handle.+This saves a unqiue identifier to the body. The ID of a object can be retrived by calling GetID() function. There are script functions for converting the ID to a class handle.
  
  
Line 253: Line 256:
  
  
-If the object that the ID handle points to gets deleted anywhere else in the code the ID will still be safe. When trying to retrive a deleted object the function will return null. Accessing null pointers will never cause the game to crash. ​Had this been a class handle ​the code had tried to access deleted ​memory and caused ​the game to crash.+If the object that the ID handle points to gets deleted anywhere else in the code the ID will still be safe. When trying to retrive a deleted object the function will return null. Accessing null pointers will never cause the game to crash. ​When accessing ​a class handle ​that has been deleted that leads to accessing deallocated ​memory and the game will crash.
  
 ===== Script Callbacks ===== ===== Script Callbacks =====
Line 311: Line 314:
 bool bArg0 = cScript_GetGlobalArgBool(0);​ bool bArg0 = cScript_GetGlobalArgBool(0);​
 cVector3f vArg1 = cScript_GetGlobalArgVector3f(1);</​code>​ cVector3f vArg1 = cScript_GetGlobalArgVector3f(1);</​code>​
-**5)** When calling a global function, it is okay to have one or more asterix in the object name and then several object will be called. For example //"​My*test"//​ would call: //"​MyNiceTest"//​ and //"​MyBadTest"//​. However, this will be a bit slower, so be careful when using it (eg not during things called every update)+**5)** When calling a global function, it is okay to have one or more asterix in the object name and then several object will be called. For example //"​My*test"//​ would call: //"​MyNiceTest"//​ and //"​MyBadTest"//​. However, this will be a bit slower, so be careful when using it (eg not during things called every update)
 + 
 +Asterix is also supported for the class name (eg "​cMyClass*"​). If you do not care about the class of the objects the class param can simply be empty (""​). The following will call MyGlobalFunc in all script modules named "​MyClassObj"​ no matter the class name: 
 + 
 +<code c++> 
 +cScript_RunGlobalFunc("​MyClassObj",​ "",​ "​MyGlobalFunc"​);​ 
 +</​code>​
 ===== Saving ===== ===== Saving =====
  
Line 328: Line 337:
 <code c++>​[nodatasave] cMyClass@ mNoSaveHandle;​ <code c++>​[nodatasave] cMyClass@ mNoSaveHandle;​
 </​code>​ </​code>​
 +
 +However it is almost always better to use the tID type for these situations!
  
  
Line 337: Line 348:
  
  
-Instead of using class handler ​here it is better to store the ID to the object. The ID can be saved and will work correctly on load.+Instead of using class handle ​here it is better to store the ID to the object. The ID can always ​be saved and will work correctly on load even if the object no longer exists. Worst thing that can happen is that when getting the actual class NULL is returned. This leads to an excpection, stopping further execution of the current script file, but does not lead to a crash or similar major failure.
  
  
Line 346: Line 357:
  
 In case you have an array of handles where none of the properties should be saved, this is NOT possible. Instead save names of IDs of the objects in the array. In case you have an array of handles where none of the properties should be saved, this is NOT possible. Instead save names of IDs of the objects in the array.
- 
 ===== Optimizing ===== ===== Optimizing =====
  
Line 370: Line 380:
  
 C++ scriptable classes with classes in can not be abstract ones. It must always be the top class in the hierarchy that is used and saved. C++ scriptable classes with classes in can not be abstract ones. It must always be the top class in the hierarchy that is used and saved.
 +
 +===== Specific Guidelines =====
 +
 +==== Helper Functions ====
 +
 +  * Helper functions should use degrees, not radians
hpl3/engine/script.txt · Last modified: 2020/07/01 07:07 by thomas