0% found this document useful (0 votes)
319 views4 pages

Essential Gamemaker Code Snippets

This document provides an overview of useful Gamemaker code concepts and functions for controlling gameplay, graphics, user interaction, and more. It explains how to use code instead of drag and drop actions, and highlights syntax coloring when writing code. Examples are given for many common functions like instance_create, draw_sprite, and logical operators. The document encourages readers to try out code examples and explore the Gamemaker help for additional reference.

Uploaded by

edmusic88
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Topics covered

  • game performance,
  • instance destroy,
  • game logic,
  • draw sprite,
  • user input handling,
  • key press events,
  • Gamemaker resources,
  • Gamemaker Language,
  • user interaction,
  • coding best practices
0% found this document useful (0 votes)
319 views4 pages

Essential Gamemaker Code Snippets

This document provides an overview of useful Gamemaker code concepts and functions for controlling gameplay, graphics, user interaction, and more. It explains how to use code instead of drag and drop actions, and highlights syntax coloring when writing code. Examples are given for many common functions like instance_create, draw_sprite, and logical operators. The document encourages readers to try out code examples and explore the Gamemaker help for additional reference.

Uploaded by

edmusic88
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Topics covered

  • game performance,
  • instance destroy,
  • game logic,
  • draw sprite,
  • user input handling,
  • key press events,
  • Gamemaker resources,
  • Gamemaker Language,
  • user interaction,
  • coding best practices

Gamemaker Code – useful things

Tony Forster August 2005

Rather than drag and drop actions, you can use the more powerful code. The “execute a
piece of code” action looks like this:

You can type code into this window or paste to it from help or elsewhere. Note how the
colour changes when Gamemaker recognises something, for example:

if(mouse_x<200 && mouse_y<200) set_cursor(cr_hourglass) else set_cursor(cr_default)

light blue for variables


dark blue for functions
red for constants
magenta for objects
green for comments
bold for language words

If you don’t get the colour you expect, you have made a typing error

Game play
x Its x-position.

y Its y-position.
Example:
In a keypress<space> event
x=100
y=100 jump to position 100 100 when you press space

hspeed Horizontal component of the speed.


Example
In the keypress<right> event
hspeed =10 move to the right speed 10 when you press right cursor

vspeed Vertical component of the speed.

direction Its current direction (0-360, counter-clockwise, 0 = to the right).

speed Its current speed (pixels per step).


instance_create
instance_create (x,y,obj) Creates an instance of obj at position (x,y).
Example:
In a keypress<space> event
instance_create (100,100,object0) create an object0 at position 100 100 of the screen

instance_change
instance_change (obj,perf) Changes the instance into obj. perf indicates whether to
perform the destroy and creation events.
Example:
instance_change (object0,false) change into object0 without performing the destroy and
creation events

instance_destroy()
Destroys the current instance.

User interaction
mouse_x X-coordinate of the mouse.
mouse_y Y-coordinate of the mouse.
Example:
In the step event
x= mouse_x
y= mouse_y the object moves with the mouse

Game graphics
image_scale A scale factor to make larger or smaller images.
example :
image_scale =1.8 makes the sprite 1.8 times bigger

draw_sprite
draw_sprite (n,img,x,y) Draws the sprite.
Example:
In the draw event
draw_sprite(sprite0,-1,x,y) just draws the sprite as normal

Language overview
&& ||
logical and, or
example:
if(x<0 && y<0) means if x is less than zero and y is less than zero

What the dot means


object0.x is the x value or horizontal position of object0

for
does something a number of times
example:
for (i=0 ; i<10 ; i=i+1) instance_create(x,y,object0) creates 10 object0’s

with
this allows you to do something with all instances of an object
example:
with (object0) instance_destroy() destroys all object0’s

//
// this is a comment, the line following // does nothing

Computing things
random(x)
random(x) Returns a random real number between 0 and x. The number is always
smaller than x.

point_distance
point_distance (x1,y1,x2,y2) Returns the distance between point (x1,y1) and point
(x2,y2).

point_direction
point_direction (x1,y1,x2,y2) Returns the direction from point (x1,y1) toward point
(x2,y2) in degrees.

What do these do? Try them


(You can paste these from this document directly into the “execute a piece of code”
window)

In the step event:


if(mouse_x<200 && mouse_y<200) set_cursor(cr_hourglass) else set_cursor(cr_default)

in a keypress event
for (i=0 ; i<10 ; i=i+1) instance_create(x+10*i,y+10*i,object0)

Whats the difference between


with (object0) instance_destroy() and instance_destroy()
when placed in object0’s code? in another object’s code

In a keypress event:
instance_create(random(400),random(400),object0)
In the step event:
if (x<0) hspeed=5
if (x>300) hspeed=-5

try this in the draw event:


draw_sprite(sprite0,-1,x-5,y)
draw_sprite(sprite1,-1,x+5,y)

More reading
Go to gamemaker help contents and look at The Gamemaker Language, there are heaps
of useful things there.

Common questions

Powered by AI

'instance_destroy()' when used in an object's code will destroy only the current instance from which the code is being executed. In contrast, 'with (object0) instance_destroy()' destroys all instances of 'object0'. Differentiating between the two allows for specific or mass destruction of instances, thus providing nuanced control over instance lifecycle within the game environment .

The instance_create function in Gamemaker dynamically creates an instance of a specified object at given coordinates during gameplay. This allows developers to spawn objects, such as enemies or power-ups, in response to specific events, enhancing interactivity and variability in the game environment .

The set_cursor function enhances user interaction by changing the cursor icon based on specific conditions, thereby providing immediate visual feedback to the user. For example, 'if(mouse_x<200 && mouse_y<200) set_cursor(cr_hourglass)' temporarily changes the cursor to an hourglass in a defined area, indicating a waiting period or disabled action region, thus improving interaction clarity and user experience .

Using for loops in conjunction with instance_create allows developers to generate multiple instances of an object efficiently. For example, 'for (i=0 ; i<10 ; i=i+1) instance_create(x+10*i,y+10*i,object0)' creates 10 instances spaced apart, thus enabling quick implementation of repeated patterns or large-scale object creation, crucial for scenarios like spawning multiple enemies or platforms .

Logical operators like '&&' and '||' in Gamemaker are used to create compound conditions that must be met before executing code. This enhances game mechanics by allowing developers to specify complex conditions under which certain actions occur. For example, the expression 'if(x<0 && y<0)' checks if both x and y positions are less than zero before proceeding, providing precise control over game behavior .

You can implement continuous horizontal movement by checking position coordinates and reversing horizontal speed accordingly: 'if (x<0) hspeed=5; if (x>300) hspeed=-5;'. This code ensures that an object bounces back when reaching the defined left or right boundaries, thus creating a seamless movement loop .

The point_distance function returns the distance between two points, (x1,y1) and (x2,y2). This can be utilized in a game to determine the proximity of one object to another, which could trigger events like avoidance behavior in characters, engagement in combat, or collection of items .

A developer might use the 'draw_sprite' function in a draw event to directly control how and where sprites are rendered on the screen during that event. This function allows for precise placement and order of appearance of sprites, which is crucial for layered graphics. Unlike automatic rendering associated with object properties, 'draw_sprite' provides more customization and control over graphic operations .

Gamemaker's code editor identifies different elements by color-coding them: light blue for variables, dark blue for functions, red for constants, magenta for objects, green for comments, and bold for language words. This visual differentiation aids developers in understanding and debugging their code quickly by providing immediate context for different elements .

To make a sprite appear larger than its default size, the 'image_scale' function is used in Gamemaker. For example, setting 'image_scale = 1.8' will make the sprite 1.8 times bigger. This function is implemented by adjusting the scale factor of the image, allowing for visual scaling without altering the sprite file itself .

You might also like