Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script editor #3074

Merged
merged 57 commits into from
Sep 26, 2023
Merged

Script editor #3074

merged 57 commits into from
Sep 26, 2023

Conversation

ohlidalp
Copy link
Member

@ohlidalp ohlidalp commented Jul 23, 2023

Script writing is powerful but clumsy:

  1. edit in external editor, save to Documents\My Games\Rigs of Rods\scripts
  2. switch to RoR, either type loadscript ... to console or use ScriptMonitor menu to Load/Reload the script
  3. if there's an error, switch to Angelscript.log and figure the error
  4. repeat

To make scripting fun, the dev/test cycle needs to be quick and comfy. So we needed an ingame editor with a "(RE)START" button and error reporting.

Behold the 'script_editor.as' AngelScript editor written in AngelScript:

obrazek

At the moment it's somewhat glitchy but the START/STOP button works perfectly. It relies entirely on ImGui::InputTextMultiline(), errors and syntax highlight is done by overdrawing. The numbers are: line number, line length, comment offset, num errors - most are not useful but look cool, you can disable them in the View menu.

Update: yes, the hyperlink opens the default browser (only tested on Win10).
obrazek

Update2: added game.findResourceFileInfo() which lists files in pre-defined resource groups. This allowed me to list up files in user's 'scripts' dir:
obrazek

@ohlidalp
Copy link
Member Author

I'm getting ahead of myself... but just a little.

BTW is it weird to run a physics sandbox game just to make and use your own text editor in there? 😅

obrazek

@AnotherFoxGuy
Copy link
Member

I just tested this, and one crash that I found is that if you try and load a script from a terrain folder the game crashes

@ohlidalp ohlidalp marked this pull request as draft July 26, 2023 18:18
@ohlidalp
Copy link
Member Author

ohlidalp commented Jul 26, 2023

I just realized I'm not nearly done here, so I've converted this to draft.

@AnotherFoxGuy Probably an uncaught exception. I just added an exception-reporting event type, but I need to manually wrap all troublesome funcs. The save/loadStringResource() aren't wrapped yet.

I just added an API giving script info - I need it to make nice exception window, and also to tell which exceptions belong to who - the editor needs to catch all so it can diagnose it's guest script. The thisScript is a new automatic global var.

obrazek

@ohlidalp
Copy link
Member Author

If this isn't cool, IDK what is 😎
obrazek

@ohlidalp
Copy link
Member Author

@AnotherFoxGuy I can't seem to reproduce the crash you mentioned.
obrazek

@ohlidalp
Copy link
Member Author

ohlidalp commented Aug 25, 2023

Latest development: 👼script bindings of OGRE overlay system and a prototype editor script allowing you to add/edit/delete elements and export OGRE's .overlay files.. Also the editor now has tabs and autosave.
image

CODECCHANGES:
* scriptEngine.h: framestep() now returns void (retval was meaningless since multiple scripts got supported); aded `executeContextAndHandleErrors()` and `exceptionCallback()`
* scriptEngine.cpp: framestep() now uses `executeContextAndHandleErrors()`
* gameScript: added functions `get/setRegisteredEventsMask()` so that script editor can listen for the guest script's exceptions.
* scriptEvents.h: added SE_ANGELSCRIPT_EXCEPTIONCALLBACK
* doc/script2game/globals.h: added events SE_ANGELSCRIPT_EXCEPTIONCALLBACK (new) and SE_ANGELSCRIPT_LINECALLBACK (omitted by mistake).

script_editor.as updated - angelscript exceptions are displayed alongside forwarded C++ exceptions
Folding is not implemented yet, only a visual highlight is present for testing.
overlay_editor.as was marked up as example.
Note the markers must be in // comments not to disrupt AngelScript.
`triggerEvent()` and `envokeCallback()` now use `[prepare/execute]ContextAndHandleErrors()` helpers.

Coded while investigating Xploder98's as_CONTEXT_NOT_PREPARED problem which turned out to be a callback with incorrect signature.
….as'

New conventions:
* scripts with name 'example_*.as' are examples and have Examples menu in the script editor (aready estabilished)
* scripts with name '*_utils.as' are includes and have Includes menu in the script editor (new)

Documenting style (example):
/// \title SCRIPT NAME
/// \brief One-line short description
/// Blah blah details...
/// Usage:
///   `#include "scriptname.as"` ~~ adds the utils to your script
@ohlidalp ohlidalp marked this pull request as ready for review September 17, 2023 23:16
- loading new file also focuses the new tab
- fixed Save menu "Select" button writing to Load menu's inputbox
- fixed script title & brief strings not appearing in menus.
Copy link
Collaborator

@CuriousMike56 CuriousMike56 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to work fine, though my knowledge with scripting is quite limited. A search feature and the ability to duplicate a line (CTRL+D in Notepad++) would be nice, but those could come later.

apparently `getAsString()` has some Linux-x64 issues (`eof()` triggers SIGINT):
see https://discord.com/channels/136544456244461568/189904947649708032/1155952230130778262
Observed with OGRE 1.11.6
* changed "select+load" to just "load" in menubar.
* workaround: clamp error line num.s to actual line count
* Renamed "Welcome" tab to "Tutorial.as".
* fixed bogus filesize of Recent scripts.
@ohlidalp ohlidalp merged commit 91f99be into RigsOfRods:master Sep 26, 2023
2 checks passed
@ohlidalp ohlidalp deleted the script_editor branch September 26, 2023 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants