Skip to content

Examples

Mtax edited this page Jul 31, 2023 · 17 revisions

This page contains examples illustrating the GML-OOP workflow and if possible, compares them their native GML equivalents.


Data Structure usage

This example will use a List Data Structure for a basic demonstration on how Data Structures are interacted with in GML-OOP.
The following code will create a List, ensure it is empty, add three numbers to it, assign one of them to a variable and destroy the List.
Native GML
exampleList = ds_list_create();
ds_list_clear(exampleList);
ds_list_add(exampleList, 5, 20, 21);
var listValue = exampleList[| 1];
ds_list_destroy(exampleList);
GML-OOP
exampleList = new List();
exampleList.add(5, 20, 21);
var listValue = exampleList.getValue(1);
exampleList = exampleList.destroy();

The Data Structure created by a GML-OOP constructor can be interacted with through its methods. This is the recommended way of operating the Data Structure created by the constructor, however the reference to the actual Data Structure is held in its property in the ID variable and it can be still operated by the accessor (exampleList.ID[| 1]) and the native GML function (ds_list_find_value(exampleList.ID, 1)).

The List has been automatically cleared during the construction, but it still has to be manually destroyed when it is no longer intended to be used. Since the destroy() method will always return {undefined}, assigning its call to the reference of its own struct will dereference it, causing the Garbage Collector to clear it from the memory.


Particle Type configuration

This example will setup the properties of a Particle Type to demonstrate how GML-OOP interacts with the features of GameMaker, as well as how constructors can be operated through the with statement.
Native GML
exampleParticleType = part_type_create();

part_type_life(exampleParticleType, 150, 2500);
part_type_shape(exampleParticleType, pt_shape_disk);
part_type_scale(exampleParticleType, 0.25, 0.25);
part_type_size(exampleParticleType, 0.5, 0.5, 0, 0);
part_type_speed(exampleParticleType, 0.25, 1, 0, 0);
part_type_direction(exampleParticleType, 0, 359, 0.1, 0);
part_type_color_rgb(exampleParticleType, 0, 255, 0, 255, 0, 255);
part_type_alpha3(exampleParticleType, 1, 0.4, 0);
GML-OOP
exampleParticleType = new ParticleType();

with (exampleParticleType)
{
    setLife(new Range(150, 2500));
    setShape(pt_shape_disk);
    setScale(new Scale(0.25, 0.25));
    setSize(0.5);
    setSpeed(new Range(0.25, 1));
    setDirection(new Range(0, 359), 0.1);
    setColorRGB(new Range(55, 255), new Range(55, 255), new Range(55, 255));
    setAlpha(1, 0.4, 0);
}

Since GML-OOP operates on constructors, their methods can be called through the use of with statement, which can reduce the number of times a variable referring to them has to be mentioned. These methods treat arguments that are not absolutely necessary to execute a function as optional and can be skipped by not specifying them or using {undefined}. In that case, default values will be supplied to skip the use of the feature that the argument is responsible for. Some features in the above code, such as the ones responsible for increase and wiggle parts of the particle properties are not used, therefore the value of 0 has to be used in the native code for them and they can be omitted in GML-OOP.

All of the properties set using the above methods will be saved as properties of the constructor as they have been declared. Therefore, the declared properties, such as the speed of the particle can be read through exampleParticleType.speed, which will refer to the declared Range. Across different constructors, some of the properties saved in such way are used for further reference by the code of the constructor itself, whereas other are saved only for the manual reference by the user. The documentation page of each constructor lists which properties are freely modifiable without causing the constructor to become nonfunctional, as long as the new value is of a supported type.


Stringifying constructors

This example will illustrate the general way GML-OOP constructor are stringified, as each of them has a toString() method, which can be called manually and will automatically alter the result of using the variable referring to the constructor as an argument of the string() function. This method will return the name of the constructor and the most relevant information in curly brackets or a pair of angle brackets if the constructor is not functional.
Setup
exampleVector2 = new Vector2(5, 50);
exampleSprite = new Sprite(TestSprite);
exampleList = new List();
exampleStack = new Stack();
exampleMap = new Map();
exampleList.add("GML-OOP", 55555, exampleSprite);
exampleStack.add("GML-OOP", [5, 25, 125, 625, 3125], exampleVector2);
exampleMap.destroy();

var result_Vector2 = string(exampleVector2);
var result_List = exampleList.toString();
var result_Stack = exampleStack.toString(true, all, all);
var result_Map = exampleMap.toString();
Result: Vector2
Vector2(x: 5, y: 50)
Result: List
List(3 - GML-OOP, 55555, Sprite("TestSp...)
Result: Stack
Vector2(x: 5, y: 50)
[ 5,25,125,625,3125 ]
GML-OOP   
Result: Map
Map<>

The output of Data Structures is shortened, as they can have a potentially infinite number of values inside of them. The constructors that are not functional because they are invalid or have been destroyed will output the name of the constructor and then a pair of angle brackets, as exemplified above with the destroyed Map constructor.

Calling the toString() method of a constructor allows for configuration of the resulting string through its arguments. This has been done in the above example with the Stack constructor to make the output multiline, as well as to change the number of elements shown and their length, in which case the all constant removed these limits.


Method chaining

Each GML-OOP constructor method that is not documented to return anything will return {self}. It is the reference to the constructor of which the method was called. Calling a method directly off that result allows for multiple calls of methods of the same constructor by mentioning the starting constructor only one time. This is comparable to the use of the with statement, however it does not change the scope in which the code is currently executed. If a method returns a value, it cannot be chained from, unless that value is another struct containing a callable method.
Setup
exampleVector2 = new Vector2(5, -15);
Inline
var result = exampleVector2.add(5).multiply(-1).sum(exampleVector2).x;
Multiline
var result = exampleVector2
	      .add(5)
	      .multiply(-1)
	      .sum(exampleVector2)
	      .x;

The methods in a chain are executed in order from closest to the reference to the constructor. Its values are updated instantly with each call, allowing for seamless operations, including the constructor using itself as an argument. The chain can be continued on a new constructor that was returned as a part of it. It can also end on a value that does not allow further chaining, which can then be read as the result.

Changes in whitespace are allowed in such chains if they are not breaking the names of their elements. The use of semi-colon is allowed only after the final element.

Contents

Constructors
Container
Angle
Management
isFunctional()
Getters
equals()
difference()
Setters
set()
modify()
Conversion
toString()
Color2
Management
isFunctional()
Setters
reverse()
set()
setAll()
Conversion
toString()
toArray()
Color3
Management
isFunctional()
Setters
reverse()
set()
setAll()
Conversion
toString()
toArray()
Color4
Management
isFunctional()
Setters
reverse()
set()
setAll()
Conversion
toString()
toArray()
split()
DateTime
Management
isFunctional()
Getters
compareDateTime()
compareDate()
compareTime()
spanOfYears()
spanOfMonths()
spanOfWeeks()
spanOfDays()
spanOfHours()
spanOfMinutes()
spanOfSeconds()
getDate()
getTime()
getDaysInYear()
getDaysInMonth()
getWeekOfYear()
getDayOfYear()
getHourOfYear()
getMinuteOfYear()
getSecondOfYear()
getWeekday()
isToday()
isLeapYear()
Setters
modify()
modifyYears()
modifyMonths()
modifyWeeks()
modifyDays()
modifyHours()
modifyMinutes()
modifySeconds()
setCurrent()
setDateTime()
Conversion
toString()
toStringDate()
toStringTime()
toArray()
toArrayDate()
toArrayTime()
Range
Management
isFunctional()
Getters
clampTo()
interpolate()
percent()
randomReal()
randomInt()
getMiddle()
isBetween()
isBoundary()
Conversion
toString()
toArray()
RangedValue
Management
isFunctional()
Getters
equals()
percent()
isBoundary()
isMinimum()
isMaximum()
Setters
modify()
modifyWrap()
modifyBounce()
interpolate()
set()
setMinimum()
setMaximum()
setOriginal()
setMiddle()
Conversion
toString()
Scale
Management
isFunctional()
Getters
contains()
equals()
getMinimum()
getMaximum()
Setters
approach()
grow()
shrink()
mirror()
mirrorX()
mirrorY()
setAll()
setAll()
Conversion
toString()
toArray()
TextAlign
Management
isFunctional()
Setters
mirror()
mirrorX()
mirrorY()
setXLeft()
setXCenter()
setXRight()
setYTop()
setYMiddle()
setYBottom()
Execution
setActive()
Conversion
toString()
toArray()
Vector2
Management
isFunctional()
Getters
contains()
equals()
sum()
difference()
product()
quotient()
dotProduct()
getAngle()
getDistance()
getMinimum()
getMaximum()
getMagnitude()
getNormalized()
getSign()
Setters
add()
substract()
multiply()
divide()
approach()
grow()
shrink()
clampTo()
flip()
mirror()
set()
setAll()
setFloor()
setRound()
setCeil()
setCursor()
Conversion
toString()
toArray()
Vector4
Management
isFunctional()
Getters
contains()
equals()
sum()
difference()
product()
quotient()
dotProduct()
interpolate()
percent()
getAngle()
getDistance()
getClosest()
getMinimum()
getMaximum()
getMiddle()
getMagnitude()
getNormalized()
getSign()
isBetween()
Setters
add()
substract()
multiply()
divide()
approach()
clampTo()
grow()
shrink()
flip()
mirror()
mirrorX()
mirrorY()
sort()
set()
setAll()
setFloor()
setRound()
setCeil()
setCursor()
Conversion
toString()
toArray()
split()
combine()
Data Structure
Grid
Management
isFunctional()
destroy()
clear()
copy()
Getters
contains()
containsRegion()
containsDisk()
count()
getValue()
getCellNumber()
getRow()
getColumn()
getMinimum()
getMinimumDisk()
getMaximum()
getMaximumDisk()
getMean()
getMeanDisk()
getSum()
getSumDisk()
getValueLocation()
getValueLocationDisk()
Setters
setSize()
Execution
forEach()
set()
setRegion()
setDisk()
setRegionCopied()
add()
addRegion()
addDisk()
addRegionCopied()
multiply()
multiplyRegion()
multiplyDisk()
multiplyRegionCopied()
mirrorX()
mirrorY()
transpose()
sort()
shuffle()
Conversion
toString()
toArray()
fromArray()
toEncodedString()
fromEncodedString()
List
Management
isFunctional()
destroy()
clear()
copy()
Getters
contains()
count()
getValue()
getFirst()
getLast()
getFirstPosition()
getPositions()
getSize()
isEmpty()
Execution
forEach()
add()
set()
replace()
removePosition()
removeValue()
insert()
sort()
shuffle()
Conversion
toString()
toArray()
fromArray()
toEncodedString()
fromEncodedString()
Map
Management
isFunctional()
destroy()
clear()
copy()
Getters
contains()
count()
getValue()
getAllValues()
getAllKeys()
getFirst()
getLast()
getPrevious()
getNext()
keyExists()
valueIsBoundList()
valueIsBoundMap()
getSize()
isEmpty()
Execution
forEach()
add()
addBoundList()
addBoundMap()
set()
replace()
remove()
Conversion
toString()
toArray()
fromArray()
toStruct()
fromStruct()
toEncodedString()
fromEncodedString()
secureToFile()
secureFromFile()
secureFromBuffer()
PriorityQueue
Management
isFunctional()
destroy()
clear()
copy()
Getters
contains()
count()
getFirst()
getLast()
getPriority()
getSize()
isEmpty()
Execution
forEach()
add()
setPriority()
remove()
removeFirst()
removeLast()
Conversion
toString()
toArray()
fromArray()
toEncodedString()
fromEncodedString()
Queue
Management
isFunctional()
destroy()
clear()
copy()
Getters
contains()
count()
getFirst()
getLast()
getSize()
isEmpty()
Execution
forEach()
add()
remove()
Conversion
toString()
toArray()
fromArray()
toEncodedString()
fromEncodedString()
Stack
Management
isFunctional()
destroy()
clear()
copy()
Getters
contains()
count()
getFirst()
getLast()
getSize()
isEmpty()
Execution
forEach()
add()
remove()
Conversion
toString()
toArray()
fromArray()
toEncodedString()
fromEncodedString()
Debug
ErrorReport
Management
isFunctional()
Execution
report()
Conversion
toString()

ErrorReport.ReportData
Management
isFunctional()
Getters
equals()
formatLocation()
formatDetail()
formatCallstack()
formatTime()
Conversion
toString()
Handler
ArrayParser
Management
isFunctional()
setParser()
create()
clear()
copy()
merge()
Getters
contains()
containsAll()
containsCondition()
equals()
getValue()
getUniqueValues()
getSharedValues()
getFirst()
getLast()
getFirstPosition()
getLastPosition()
getPositions()
getPositionsCondition()
getReduction()
getColumn()
getSize()
isEmpty()
Setters
setSize()
Execution
forEach()
add()
set()
insert()
removePosition()
removeValue()
sort()
Conversion
toString()
SpriteRenderer
Management
isFunctional()
Execution
render()
Conversion
toString()
toArray()
StringParser
Management
isFunctional()
setParser()
Getters
contains()
containsAll()
startsWith()
endsWith()
charEquals()
charIsWhitespace()
split()
getFirst()
getLast()
getBetween()
getByte()
getByteLength()
getChar()
getOrd()
getPart()
getSubstringCount()
getLetters()
getDigits()
getLettersAndDigits()
getSubstringPosition()
getSize()
getPixelSize()
Setters
remove()
formatNumber()
insert()
duplicate()
replace()
reverse()
trim()
setByte()
setLowercase()
setUppercase()
Execution
forEach()
displayOutput()
displayMessageBox()
Conversion
toString()
toNumber()
toArray()
fromArray()
SurfaceRenderer
Management
isFunctional()
Execution
render()
Conversion
toString()
TextRenderer
Management
isFunctional()
Getters
getBoundaryOffset()
Execution
render()
Conversion
toString()
Resource
Buffer
Management
isFunctional()
destroy()
copy()
Getters
getSeekPosition()
getType()
getAlignment()
getPointer()
getSize()
Setters
setSeekPosition()
Execution
write()
fill()
read()
compress()
decompress()
getValue()
Conversion
toString()
toHashMD5()
toHashSHA1()
toHashCRC32()
toEncodedString()
fromEncodedString()
secureFromMap()
fromSurface()
toFile()
fromFile()
fromFilePart()
Font
Management
isFunctional()
destroy()
Getters
getTexture()
getTexel()
getUV()
isActive()
Execution
setActive()
Conversion
toString()
Layer
Management
isFunctional()
destroy()
Getters
hasInstance()
getElements()
Setters
setLocation()
setSpeed()
setVisible()
setDepth()
setShader()
setFunctionDrawBegin()
setFunctionDrawEnd()
Execution
createBackground()
createInstance()
createTilemap()
createSprite()
createParticleSystem()
destroyInstance()
setInstancePause()
Conversion
toString()

Layer.SpriteElement
Management
isFunctional()
changeParent()
destroy()
Setters
setSprite()
setScale()
setColor()
setAlpha()
setFrame()
setSpeed()
Conversion
toString()
Layer.BackgroundElement
Management
isFunctional()
changeParent()
destroy()
Setters
setSprite()
setScale()
setColor()
setAlpha()
setFrame()
setSpeed()
setStretch()
setTiled()
setVisible()
Conversion
toString()
Layer.TilemapElement
Management
isFunctional()
destroy()
clear()
changeParent()
Getters
getFrame()
getMask()
getTileInCell()
getTileAtPoint()
getCellAtPoint()
Setters
setMask()
setTileset()
setSize()
Execution
render()
setTileInCell()
setTileAtPoint()
Conversion
toString()

Layer.TilemapElement.TileData
Management
isFunctional()
clear()
Getters
getTilesetIndex()
isEmpty()
isMirroredX()
isMirroredY()
isRotated()
Setters
setTilesetIndex()
setMirrorX()
setMirrorY()
setRotate()
Execution
render()
Conversion
toString()
Layer.ParticleSystem
Management
isFunctional()
destroy()
clear()
changeParent()
Getters
getParticleCount()
Setters
setLocation()
setDrawOrder()
setAutomaticUpdate()
setAutomaticRender()
Execution
createEmitter()
render()
update()
Conversion
toString()

Layer.ParticleSystem.ParticleEmitter
Management
isFunctional()
destroy()
clear()
Setters
setRegion()
setStreamEnabled()
setStreamCount()
Execution
burst()
stream()
Conversion
toString()
ParticleType
Management
isFunctional()
destroy()
clear()
Setters
setShape()
setSprite()
setScale()
setSize()
setSpeed()
setDirection()
setAngle()
setGravity()
setLife()
setColor()
setColorMix()
setColorRGB()
setColorHSV()
setBlend()
setAlpha()
setStep()
setDeath()
Execution
create()
createShape()
Conversion
toString()
Room
Management
isFunctional()
copy()
Getters
isActive()
Setters
setSize()
setPersistent()
Execution
createInstance()
setActive()
Conversion
toString()

Room.AddedInstance
Management
isFunctional()
Conversion
toString()
Shader
Management
isFunctional()
Getters
getSampler()
isActive()
Setters
setUniformFloat()
setUniformInt()
setUniformMatrix()
updateUniforms()
Execution
setActive()
Conversion
toString()
Sprite
Management
isFunctional()
destroy()
replace()
merge()
Getters
getNineslice()
getTexture()
getTexel()
getUV()
Setters
setNineslice()
setOrigin()
setSpeed()
setCollisionMask()
Execution
render()
renderTiled()
renderPerspective()
load()
generateAlphaMap()
Conversion
toString()
toFile()
Surface
Management
isFunctional()
create()
destroy()
clear()
copy()
Getters
getPixel()
getTexture()
getTexel()
isActive()
Setters
setSize()
Execution
render()
renderTiled()
setActive()
Conversion
toString()
toFile()
fromBuffer()
Shape
Arrow
Management
isFunctional()
Execution
render()
Conversion
toString()
Circle
Management
isFunctional()
Getters
collision()
containsPoint()
cursorOver()
cursorHold()
cursorPressed()
cursorReleased()
Execution
render()
Conversion
toString()
Ellipse
Management
isFunctional()
Getters
collision()
Execution
render()
Conversion
toString()
Line
Management
isFunctional()
Getters
collision()
Execution
render()
__createPixelSprite()
Conversion
toString()
Point
Management
isFunctional()
Getters
collision()
cursorOver()
cursorHold()
cursorPressed()
cursorReleased()
Execution
render()
__createPixelSprite()
Conversion
toString()
Rectangle
Management
isFunctional()
Getters
collision()
containsPoint()
cursorOver()
cursorHold()
cursorPressed()
cursorReleased()
Execution
render()
__createPixelSprite()
Conversion
toString()
RoundRectangle
Management
isFunctional()
Getters
collision()
containsPoint()
cursorOver()
cursorHold()
cursorPressed()
cursorReleased()
Execution
render()
Conversion
toString()
Triangle
Management
isFunctional()
Getters
containsPoint()
cursorOver()
cursorHold()
cursorPressed()
cursorReleased()
Execution
render()
Conversion
toString()
Clone this wiki locally