Jump to content


Photo
- - - - -

Instance Creation Code


  • Please log in to reply
100 replies to this topic

#1 Chronic

Chronic

    Administrator

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

Posted 27 March 2007 - 10:38 AM

The instance creation code is probably a hugely overlooked part of Game Maker. In this tutorial, I will show you how to access the instance creation code and also an example of a use.

For this tutorial I will be using Game Maker 6.1, however Game Maker 5.3a should also work fine.

Accessing

Gaining access to this part of Game Maker is really simple. First for all, ensure you have Game Maker in Advanced Mode by looking in the File menu. Next to Advanced Mode you should see a check mark, if this check mark is not present select the option.

Next add an object and a room, then add an instance of your object to this room. In the room editor, hold down your control (Ctrl) key and right click the instance. If you have done this correct you will see a context menu with several options in it.


Posted Image
Instance context menu


Clicking Creation Code will bring up an 'Execute a piece of code' type dialog for you to enter in your GML script. Any script that you place here will be specific to that instance.

Once code as been entered here, each time you hover your mouse over this instance in the room editor. The status bar will now say "has code" next to where its instance id is shown.

Example

To demonstrate one use of this, I will be making a generic teleport object and then use the creation code to set its destination point.

In a new project I added the following:
  • 2 sprites - spr_player, spr_telepad.
  • 2 objects - obj_player, obj_telepad.
  • 1 room - room0.
For the sprites I used:


Posted ImagePosted Image
Player and Telepad sprites


Add a step event to the obj_player and add in the following script to give it basic movement.

{
  move_towards_point(mouse_x,mouse_y,5);
}

Add an instance of obj_player in to room0 and then test the game to make sure things are working fine at this point. If the object moves towards the mouse cursor, then you are ready to continue, otherwise follow the steps above again.

Next we need to program obj_telepad. Add a collision with obj_player event and add the following script.

{
  other.x = teleX;
  other.y = teleY;
}

Then add an obj_telepad object in to room0, running the game at this point will fail as teleX and teleY have not yet been defined.

To do this we will use the creation code. Like shown at the start of the tutorial, open the obj_telepad's creation code and add the following script in to it.

{
  teleX = 100;
  teleY = 200;
}

You can now run the game to test the telepad object. Just run over it and the player object should be sent to the coordinates set in your creation code.

To prove that the creation code is instance specific, add another obj_telepad to the room and the creation code script, but this time give it a different desination coordinates.

One thing you should note, is that if you do not set the teleX and teleY variables, when you collide with that instance you will get an error. To prevent this error, you should add the following script in to the obj_telepad's create event. Adding it to the create event is an ideal place as the instance creation code is executed before the create event.

{
  if (!variable_local_exists("teleX")) { teleX = 16; }
  if (!variable_local_exists("teleY")) { teleY = 16; }
}

Taking it an extra step

Its also possible to make your object move to specified location in a totally different room. To do this we need to expand on the scripts a little, in the create event of obj_telepad change the script to the following.

{
  if (!variable_local_exists("teleX")) { teleX = 16; }
  if (!variable_local_exists("teleY")) { teleY = 16; }
  if (!variable_local_exists("newRoom")) { newRoom = -1; }
}

And the collision event to.

{
  other.x = teleX;
  other.y = teleY;
  if (newRoom > -1) { room_goto(newRoom); }
}

In the creation code for obj_telepad, you can now switch to a new room by setting a newRoom varible like in the example below

{
  teleX = 100;
  teleY = 200;
  newRoom = room1;
}

The last change needed is to change obj_player in to a persistant object.

Closing

As you can see, this part of Game Maker can be very useful. Another application for this could be a treasure chest. You could normaly give the player a random item, but by setting the creation code for an instance could give a certain item each time.

You can see the completed result of this example below.Thanks for reading.
  • 1

My recent tutorials: [Auto-Tiling] [Text Based Menu] [Sprite Based Menu] [View more at my site]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
R.I.P Mom (1951-2008)


#2 yellowsix

yellowsix

    GMC Member

  • New Member
  • 315 posts

Posted 27 March 2007 - 10:54 AM

WOW!
Didn't know this function existed at all...
This is VERY VERY useful.
Thanks!

Yellowsix
  • 0
"Go thy way, eat thy bread with joy, and drink thy wine with a merry heart; for God now accepeth thy works." - Ecclesiastes 9:7

English is not my native language. Please keep this in mind.

"The ruling class has the schools and press under its thumb. This enables it to sway the emotions of the masses." ~Albert Einstein

#3 Verion

Verion

    After the Storm

  • New Member
  • 926 posts

Posted 27 March 2007 - 10:56 AM

That actually opens up alot of possibilites.

The short response for your tutorial:

That was extremely good, 9.99/10
(i don't ever give out 10/10 cause nothing can be perfect)

the_Firestorm

Edited by Chronic, 27 March 2007 - 10:57 AM.

  • 0

#4 Chronic

Chronic

    Administrator

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

Posted 27 March 2007 - 10:59 AM

Thanks for the comments, i actually wrote this along time ago and posted it at Eo along with some others i've not posted here.

I'll be adding a simple expansion to this tutorial sometime today, so check back then.
  • 0

My recent tutorials: [Auto-Tiling] [Text Based Menu] [Sprite Based Menu] [View more at my site]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
R.I.P Mom (1951-2008)


#5 purple_pixie

purple_pixie

    GMC Member

  • New Member
  • 2647 posts

Posted 27 March 2007 - 11:23 AM

Very nice - I have heard this mentioned on the forums before, but I don't think I've used it.

I always got around it by have a "room maker" object, that makes each instance, and assigns it's variables.

Which is good if you want to set them *after* create events, but not half as easy to do as this.
  • 0

#6 Chronic

Chronic

    Administrator

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

Posted 27 March 2007 - 11:30 AM

I've just added the expansion i mentioned earlier, its a section at the end called "Taking it an extra step".
  • 0

My recent tutorials: [Auto-Tiling] [Text Based Menu] [Sprite Based Menu] [View more at my site]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
R.I.P Mom (1951-2008)


#7 Elmernite

Elmernite

    Kengine

  • GMC Member
  • 424 posts
  • Version:GM:Studio

Posted 27 March 2007 - 01:08 PM

Very nice! I already new about this, but you explained it nicely. I'd read this on Eo when you first posted it there.
I use this method for the AI in Cell Burst. If I every write a tutorial on how to do the AI. You won't care if I link here to explain the Creation Code part of it?
-Elmernite
  • 0

rsbanner.png
Kengine Gaming "Kengine Gaming is the best GM site I've seen" - flubmasterQ
"Good games never die" - Elmernite


#8 Chronic

Chronic

    Administrator

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

Posted 27 March 2007 - 09:35 PM

Linking to other tutorials isn't really recommended, as there is no guarantee that what you link to will remain at that location.
  • 0

My recent tutorials: [Auto-Tiling] [Text Based Menu] [Sprite Based Menu] [View more at my site]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
R.I.P Mom (1951-2008)


#9 georg

georg

    GMC Member

  • New Member
  • 319 posts

Posted 27 March 2007 - 11:50 PM

excellent stuff. Thanks chronic, you've just wounded two or three problems of mine. I think i can finish 'em off now... :)
  • 0

#10 the_muffin_man

the_muffin_man

    GMC Member

  • New Member
  • 100 posts

Posted 27 March 2007 - 11:57 PM

Great tutorial, I had no idea about that. Good explanation and very useful information.
  • 0

#11 Vizierde

Vizierde

    GMC Member

  • New Member
  • 96 posts

Posted 28 March 2007 - 02:18 AM


Edited by Vizierde, 14 January 2009 - 08:30 PM.

  • 0

How to ask for help - Novice/Intermediate FAQ - Dropbox - Free File Sharing! - If I was helpful, please click the plus icon below.


#12 Verion

Verion

    After the Storm

  • New Member
  • 926 posts

Posted 28 March 2007 - 03:38 AM

Woah!

I looked in to those tutorials on EO, the command line one was really helpful!
  • 0

#13 SquareWheel

SquareWheel

    Square Wheel Games

  • New Member
  • 851 posts

Posted 28 March 2007 - 03:43 AM

I didn't learn about this function until about 1 and a half years into GM. It is great for keeping down on objects. Anyways, it is a nice tutorial. Great job!
  • 0
Cook Console | Editor Engine | Mass File Namer

All of my examples are publicly available and can be found at

#14 Joerdgs

Joerdgs

    Clavus Studios

  • New Member
  • 393 posts

Posted 29 March 2007 - 10:41 AM

*still laughing about the amount of users that did not know this function*

:D Man, I've been using this function almost as long as I use GM. Found it when looking around the help file.
  • 0

#15 Spartan117John

Spartan117John

    GMC Member

  • GMC Member
  • 579 posts

Posted 29 March 2007 - 10:49 PM

I know you have heard this alot, but "I had no idea that function existed!"
Thanks alot for this excellent tutroial! :D
  • 0

#16 Bawk Bawk Boom

Bawk Bawk Boom

    GMC Member

  • New Member
  • 23 posts

Posted 29 March 2007 - 11:12 PM

Heh. I actually found this out a few weeks ago, but yes, it is very useful. I forget what I used it for, I think for some sort of door system.

Nice tutorial, Chronic. The examples were very good.
  • 0

#17 Big J

Big J

    GMC Member

  • GMC Member
  • 2853 posts
  • Version:GM8.1

Posted 30 March 2007 - 06:44 AM

I've known about this feature for a long time. It's uber-powerful.
  • 0

Get your GM 8.1 Anti-Aliasing here!

2712265.png

http://www.youtube.com/SporkinatorBZ


#18 HaRRiKiRi

HaRRiKiRi

    GMC Member

  • GMC Member
  • 1364 posts

Posted 30 March 2007 - 07:57 AM

Good tut. I am too wondering how people could not know this function. I have used it in ALOT of places. This can be extremely useful for map editors and external maps. I put the map loading in this event, so I don't have any unexpected errors, like can't find instance because its not created yet.
  • 0

#19 georg

georg

    GMC Member

  • New Member
  • 319 posts

Posted 30 March 2007 - 10:02 PM

The amount of new stuff you have to learn and pay attention to is overwhelming to new users. That's why i ignored/forgot this one. Hmm... maybe i should re-read tfm from time to time...
  • 0

#20 zephiel87

zephiel87

    GMC Member

  • New Member
  • 1649 posts

Posted 31 March 2007 - 02:30 AM

wow... ive been using gm for 3 years and i didnt know this... but its really helpful. i will definately be playing around with it in future games.
  • 0

#21 Blazed Pyro

Blazed Pyro

    Pyro Tech

  • New Member
  • 50 posts

Posted 31 March 2007 - 03:18 AM

Wow thanks Chronic, ive been banging my head against my desk wondering how to do this, and wondering why game maker didn't have it...

- Pyro
  • 0

#22 Kapser

Kapser

    GMC Member

  • GMC Member
  • 1644 posts

Posted 31 March 2007 - 03:28 PM

I just learned this function from another tutorial 2 minutes ago. I can't believe I did'nt know that. :D
  • 0

#23 Mattz

Mattz

    GMC Member

  • New Member
  • 57 posts

Posted 31 March 2007 - 11:01 PM

Wow, I never new about this, now I can have 1 object and rotate it within the room... Thankyou so much for this!

Mattz
  • 0

#24 cronus616

cronus616

    Cronus Games, co.

  • New Member
  • 394 posts
  • Version:Unknown

Posted 01 April 2007 - 09:00 PM

wow.
i didnt know that
  • 0

#25 Excalibur333

Excalibur333

    GMC Member

  • GMC Member
  • 458 posts
  • Version:Unknown

Posted 03 April 2007 - 07:02 PM

Cool I just found this a few days ago it is pretty nice.
Excalibur
  • 0

OsRsy9m.png pqe3LGj.png


#26 Twisted-Liquid

Twisted-Liquid

    GMC Member

  • GMC Member
  • 500 posts

Posted 03 April 2007 - 08:18 PM

Love it! wish i knew sooner. I already made a billion objects that one single object could have done for my current project.
  • 0

#27 GloryAndBen

GloryAndBen

    Game Programmer

  • GMC Member
  • 944 posts

Posted 04 April 2007 - 10:27 PM

Now I don't need to make so many goddam objects over minor differences...
Thanks for this!
  • 0
Green.

#28 Alex

Alex

    3lite Member

  • New Member
  • 3098 posts

Posted 06 April 2007 - 06:00 AM

Wow. I can't believe all this time it was written there in plain English and I never tried it. Thanks for this. This will definitely solve a lot of problems.
  • 0

#29 The_Cookie_Monster

The_Cookie_Monster

    GMC Member

  • New Member
  • 217 posts

Posted 06 April 2007 - 08:50 PM

Wow, nice, cool, this will help alot!

You deserve a cookie now!
  • 0

#30 Daniel-Dane

Daniel-Dane

    GMC Member

  • New Member
  • 3581 posts

Posted 06 April 2007 - 10:17 PM

I can't believe that so many didn't know of this. There are both the manual and the explanation in the room editor. Good someone wrote this :blink:.
  • 0

#31 Blue_Ninja_Frog

Blue_Ninja_Frog

    GMC Member

  • New Member
  • 304 posts

Posted 07 April 2007 - 02:13 AM

Okay, question. Exactly WHEN do the actions in the Instance Creation Code get executed? In order to use this properly I will need to know this.
  • 0

#32 Chronic

Chronic

    Administrator

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

Posted 07 April 2007 - 09:07 AM

All i know is that its sometime before the instances create event. You'll have to do your own tests to see if anything is ran any earlier if required.
  • 0

My recent tutorials: [Auto-Tiling] [Text Based Menu] [Sprite Based Menu] [View more at my site]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
R.I.P Mom (1951-2008)


#33 Daniel-Dane

Daniel-Dane

    GMC Member

  • New Member
  • 3581 posts

Posted 07 April 2007 - 09:37 AM

Okay, question. Exactly WHEN do the actions in the Instance Creation Code get executed? In order to use this properly I will need to know this.

<{POST_SNAPBACK}>

Why didn't you test it first?

Here is the order: (GM7)
1. Creation code
2. Create event
3. Game start event
4. Room start event
  • 0

#34 Jenner

Jenner

    Jenner

  • GMC Member
  • 904 posts

Posted 07 April 2007 - 09:54 AM

Thanks a lot Chronic, you just made my life a lot easyer. Please if you have more tricks like this one POST IT!
  • 0

#35 Grego

Grego

    GMC Member

  • New Member
  • 1155 posts

Posted 07 April 2007 - 11:32 AM

First, in the current room (if any) all instances get a room-end event. Next the non-persistent instances are removed (no destroy event is generated!).
Next, for the new room the persistent instances from the previous room are added.
All new instances are created and their creation events are executed (if the room is not persistent or has not been visited before).
When this is the first room, for all instances the game-start event is generated.
Now the room creation code is executed.
Finally, all instances get a room-start event.


Is that what you meant about the order..?
  • 0

#36 PLAY: More

PLAY: More

    Press F8 to enter BIOS

  • New Member
  • 824 posts

Posted 07 April 2007 - 11:53 AM

All i know is that its sometime before the instances create event. You'll have to do your own tests to see if anything is ran any earlier if required.

<{POST_SNAPBACK}>

I think it's after the Room Start but before Instance Create event.
  • 0

#37 Blue_Ninja_Frog

Blue_Ninja_Frog

    GMC Member

  • New Member
  • 304 posts

Posted 07 April 2007 - 04:24 PM

In my experiences with it, It runs before the create event which became a bother due to unknown variables and the like >.>
  • 0

#38 Splayer

Splayer

    GMC Member

  • New Member
  • 130 posts

Posted 17 April 2007 - 04:09 AM

Wow! Someone sticky this now! You have no idea how many problems this solves that i've had in the past. Thank you so much!
  • 0

#39 Igl00

Igl00

    GMC Member

  • New Member
  • 327 posts

Posted 19 April 2007 - 12:37 PM

Hmm... Why is it, that almost all the ppl here on GMC, didn´t knew that function? It was, kinda the first thing i learned, when i decided to start coding... Weird!
  • 0

#40 dark_master4

dark_master4

    GMC Member

  • GMC Member
  • 1230 posts
  • Version:GM:Studio

Posted 19 April 2007 - 01:32 PM

I knew this function existed 'cause I did read the whole manual... But I don't know why, I kinda forgot to use it because of everything I was learning at the same time. You tut is well done and did remind me this existed and I'll definetly use it in the futur, if needed.
  • 0

☣ Survival zombie game blog ☣ (Updated 2013-07-02) | My YoYoGames Marketplace (Has scripts, shaders and poker chips)


GM:Studio
Desktop Intel i5-4440 3.1ghz, 8gb RAM, NVIDIA GeForce GTX 750 1gb
Laptop AMD Athlon x64 2.1ghz, 4gb RAM, 256mb shared VRAM


#41 >Leroy222<

>Leroy222<

    GMC Member

  • New Member
  • 473 posts

Posted 21 April 2007 - 07:07 AM

Nicely written. I didnt know about this!

I must question the usefulness of the feature, however. Wouldn't it be better to use parents so you dont need to add creation code multiple times??
  • 0

#42 Jenner

Jenner

    Jenner

  • GMC Member
  • 904 posts

Posted 21 April 2007 - 01:39 PM

Is there anyway you can check if a local variable exists in another object? Without useing the with function?

Something like this:
object0 CREATION:
if object1.variable_local_exists("name") {fhspeed=2}
  • 0

#43 Dinsdale

Dinsdale

    GMC Member

  • New Member
  • 1389 posts

Posted 21 April 2007 - 08:47 PM

.....I never knew. 3 years, I never knew.
Is it me or is this not in the manual.
  • 0

This week I recommend:
Posted Image


#44 Chronic

Chronic

    Administrator

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

Posted 21 April 2007 - 11:37 PM

Is there anyway you can check if a local variable exists in another object? Without useing the with function?

Something like this:
object0 CREATION:
if object1.variable_local_exists("name") {fhspeed=2}

<{POST_SNAPBACK}>


This is untested but try using with(), for example

with(otherObject) {
  if variable_local_exists("name") {fhspeed=2}
}

  • 0

My recent tutorials: [Auto-Tiling] [Text Based Menu] [Sprite Based Menu] [View more at my site]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
R.I.P Mom (1951-2008)


#45 LegacyCrono

LegacyCrono

    GMC Member

  • GMC Member
  • 616 posts

Posted 22 April 2007 - 11:55 AM

I remember that I found it a long time ago, when I didn't knew GML at all, so I think I just forgot that... ^^

This is very useful for RPGs and what-not. You don't need to make hundreds of different obj_treasure, just make a single obj_chest and use Creation Codes! :) Great, I just want to try it right now! xD


SEE YA!!!!!
  • 0

Posted Image


#46 Serprex

Serprex

    GMC Member

  • New Member
  • 48 posts

Posted 22 April 2007 - 05:27 PM

Great how many people don't know about this,its a key part of lowering the number of objects I have.I'd like it if it were made easier to access and stuff.CTRL+Rclicking and copy pasting gets hard when almost every object needs a creation code with minor tweaks and stuff
I found it reading the panel in the room creator
  • 0

#47 Jenner

Jenner

    Jenner

  • GMC Member
  • 904 posts

Posted 22 April 2007 - 06:01 PM

Is there anyway you can check if a local variable exists in another object? Without useing the with function?

Something like this:
object0 CREATION:
if object1.variable_local_exists("name") {fhspeed=2}

<{POST_SNAPBACK}>


This is untested but try using with(), for example

with(otherObject) {
  if variable_local_exists("name") {fhspeed=2}
}

<{POST_SNAPBACK}>

hmm.. without with() ^_^
Anyways, I could use with(), but the problem is, that now the otherObject.fhspeed that changes, not mainObject.fhspeed
  • 0

#48 PL productions

PL productions

    GMC Member

  • New Member
  • 80 posts

Posted 22 April 2007 - 10:59 PM

Impressive... I have been in the dark to this function for so long. I never knew it existed. This will be useful in such things as RPG computers, or in room to room transfer.
  • 0
We are all sinners, everyone has sinned (Romans 3:23 and 3:10). Christ the Lord suffered, and died in our place so we wouldn't have to, because He loves us (Romans 5:8, John 3:16). He rose again, defeating Satan, crushing the head of the serpent. We all can live, all we have to do is admit that we are sinners, believe that Jesus Christ died for us, and confess to God that we need Him in our lives and ask him into our hearts and lives (Romans 6:23, John 1:12, 1 Corinthians 15:3-4). Check out Romans 10:13. If you have any questions, contact me. -CoRoSiLe
And on a lighter tone:"Eagles may soar, but weasels don't get sucked into jet engines."; "For Sale: Parachute, only used once, small stain, never opened."
current production: There, Once...
expected production time: 3-4 years

#49 jengajam

jengajam

    GMC Member

  • GMC Member
  • 522 posts

Posted 23 April 2007 - 09:05 PM

Thank you so Much! This will help me so much on my Rpg!
  • 0

#50 Games

Games

    GMC Member

  • Banned Users
  • 2968 posts

Posted 24 April 2007 - 11:47 PM

Very, very cool! This is a great answer to my current dilemma! What a strange coincidence! :P
Click to visit Toydaker.com
For Sale: 3 Editables, 40 dollars (demo on site above). Click to purchase.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users