Jump to content


Photo

Javascript based extensions - regular expressions


  • Please log in to reply
1 reply to this topic

#1 paul23

paul23

    GMC Member

  • Global Moderators
  • 4147 posts
  • Version:GM:Studio

Posted 25 September 2011 - 04:10 PM

Hi,

I've worked around a bit with how gamemaker handles extensions and I've created a first extension:
JSRegex

Download gregex.gex from Host-A
Download gregex.gmz from Host-A
The GMZ can be opened with any unzip program. It can also be opened by directly typing the filepath + name into the "load gamemaker 8.1 file" bar when starting a new application.



This extension enables the use of regular expressions in gamemaker. (For those who don't know here is an extensive tutorial).
This build I've created over the night and today, so should be considered (literary) "nightly". There are some bugs in gm:html5 which prevent me from progressing further with the extension, as they depend on how gamemaker becomes.

Usage

Regex
The regex flavour is simply directly from Javascript, no further limits or extensions. Here you can find a quick overview of the possible special characters in pattern.
Possible modifiers are: g - Global, for multiple matches in a string, i case insentive matching, m multiline matching. Multiple modifiers are possible by just "stringing" them together.

Matching
JSGregexMatch(heap, pattern, modifiers) - This function should be called first, it returns a "json" which has to be passed to the other functions. This function does the main calculation already, the rest of the functions are just to extract it.
JSGregexMatchNum(jsonResultString) - Returns the number of times the pattern was matched (if the modifier was set the g, "global"). If no match is found it returns 0
JSGregexMatchSubNum(jsonResultString) - Returns the number of subexpression matched
JSGregexMatchPos(jsonResultString, N) - Returns the position of the Nth match
JSGregexMatchResult(jsonResultString, N, Sub) - Returns the sub exppression "Sub" of the Nth match. Use "0" as Sub to retrieve the whole match.

Replacing
JSGregexReplace(heap, pattern, modifiers, replacestring) - Searches for "pattern" in heap and replaces it (using "g" as modifier would mean to replace all occurences). Replacing can use "$1, $2, $N" to match the nth subexpression.


What have I learned
Basics of javascript :) - using JSON is a good way to pass objects around.
Testing is very annoying. Without the lack of a build in debugger you're forced to use bugzilla. Normally not a problem, however any mistake means the code fails to launch. And the error in bugzilla simply shows the obfuscated code so you have no idea where to look.
Gamemaker has a few bugs with extensions: sometimes extensions weren't "registered", sometimes it executed another function than I typed. Deleting functions from extensions cause access violations.
The extensions are copied, and the code is stored inside [project_folder]/extensions/[extension_name]/ editing this gives live changes to the code.
Gamemaker handles string literals "weird". Any "\" sign is changed to "\\" before converting to a string. This works good for "show_message". However for "draw_text", and when doing string manipulation this is unexpectedly weird.
  • 0

#2 Mike.Dailly

Mike.Dailly

    Evil YoYo Games Employee

  • Administrators
  • 5277 posts
  • Version:GM:Studio

Posted 26 September 2011 - 06:54 PM

Use these with care... they're cool, but can be incredibly slow. :whistle:
  • 0