AngelScript scripting quickstart guide.

ohlidalp

Infamous Developer
Administrator
Developer
Joined
Jan 31, 2018
Messages
170
Location
Prague, CZ
We have a decently-capable scripting environment, and it will get expanded in a close future. This thread should be a catch-all introductory spot.
Credit is due: the scripting subsystem, along with a race manager script, was developed by former member 'neorej16' in circa 2010 - correct me if I'm wrong.

What's the programming language?

AngelScript http://www.angelcode.com/angelscript/ is a high-performance scripting library written with games in mind. The author provides support at GameDev forums: https://www.gamedev.net/forums/forum/34-angelcode/.

The language itself resembles C++/C#/Java, source files have extension ".as". The official documentation is exellently written: http://www.angelcode.com/angelscript/sdk/docs/manual/index.html

What can scripts do?
What docs do we provide?

Online docs:

In game help:
How to run a script?

Either by including it with terrain or invoking by hand:
  1. By including the ".as" file(s) with terrain - see terrn2 fileformat, section '[Scripts]': https://docs.rigsofrods.org/terrain-creation/terrn2-subsystem/. This is the classic old method, used for i.e. races and event boxes. Note that if no script is provided, a bundled "default.as" is invoked. It provides the basic handling for spawners, garages and races.
  2. By placing the ".as" file(s) in 'Documents\My Games\Rigs of Rods\Scripts' and:
    1. [since 2022.04] running the game with command line parameter '-runscript <filename>'. You can use this command multiple times at once.
    2. [since 2022.04] setting 'app_custom_scripts' in RoR.cfg to a comma-separated list of filenames. Spaces in filename are acceptable.
    3. [dev build only] in game, opening console (tilde key) and using command `loadscript <filename>`.
How to diagnose scripts?
Any questions?

Ask right in this thread!
 
Last edited:
Is there a way i can make the script do something, wait for x seconds and then do something else? I tried a game.getTime() while loop and the game crashed...
Edit: What i mean is that i need some suspend function or something like that, which right now doesn't seem to be implemented.
 
Last edited:
Is there a way i can make the script do something, wait for x seconds and then do something else? I tried a game.getTime() while loop and the game crashed...
Edit: What i mean is that i need some suspend function or something like that, which right now doesn't seem to be implemented.
Sorry about response lag.
This is possible, but it's done another way around... instead of trying to suspend the script for some time, you take advantage of the fact game runs your `void frameStep(float dt)` every frame, and just do nothing until enough time has passed. Like this:

Code:
float totalSecondsPassed = 0.f;
int whatsOn = 0; // 0=idle, 1=doing the_thing, 2=waiting, 3=doing other_thing.
float whenToDoOtherThing = 0.f; // <===== This is the magic timer.

void frameStep(float dt) // Remember, this runs each FPS!
{
  totalSecondsPassed = totalSecondsPassed + dt; // dt is frame time in seconds.
 
  if( ImGui::Button("Do the_thing, wait 10 sec, then do other_thing"))
  {
     whatsOn = 1;
  }
 
  switch (whatsOn)
  {
  case 1:
      game.log("Doing the_thing");
      whenToDoOtherThing = totalSecondsPassed + 10; // <====== set magic timer 10 seconds to the future.
      whatsOn = 2;
      break;
  case 2:
      if (whenToDoOtherThing <= totalSecondsPassed) { whatsOn = 3; } // < ======= check if magic timer has elapsed.
      break;
  case 3:
      game.log ("Doing other_thing");
      whatsOn = 0;
      break;
   default:
      break; // nothing's on!
  }

}
 
Last edited:
Thank you for the code example.
Now, i have another question: Is it possible to stop a script (with void frameStep(float dt)) from itself?
 
Back
Top