GameMaker How Does the Instance Creation Code Work?

I have objects like, Sign Posts, Chests, NPC's etc, that can all have different actions.

Instead of having multiple objects of the same type (Heart Chest, 1up Chest, etc.). Can I use the Instance Creation Code (when you double click on an instance inside a room) to specify what I want in the chest or what I want the sign or NPC to say?

Also, the Instance Creation Code area doesn't specify what kind it is (like, the chest's item code is in a User Event). I tried pasting in the User Event's code into the Instance Creation Code but it still gave a heart instead of a different item.

Does the Instance Creation Code area work like a script?

GMS2_screenshot.png
 
  • Wow
Reactions: Mut

TheouAegis

Member
The creation code is just code that is run after the instances are all created but before the room has started and if it's the first room the game, before the game has started.

You either have code that is overriding whatever you did in the creation code, or you are trying to do a random drop and you forgot to randomize the seed.
 

Sam04

Member
The instance creation code is a piece of code that your instance will run just after completing its create event (if your question was more if you could find it in the event list of the object, then no, it is not an event that will appear there and you shouldn't consider it an event at all).

It should work with just the line: "item_inside = item_you_want;". However, it seems you already tried this approach and it didn't change the item inside. If that's the case then check the other code in the object in case there is something setting it back to the default object.

As a side note, you can also define variables in the object and you can set them individually for each instance in the "Variables" button that is in your screenshot just below the "Edit Object" button.
 

Sam04

Member
or you are trying to do a random drop and you forgot to randomize the seed.
This is important. If you are just compiling the game to test in the editor then all the "random" functions will always return the same randomnes unless you use the function "randomize()".
 
Thanks for the replies, this is the current code in the User Event of the chest that gives a heart
Code:
event_inherited();

if !instance_exists(o_player) exit;

image_index = 1;
set_player_found_item(s_heart);
global.player_HP += 1;
activatable_ = false;
add_to_destroyed_list(id);
So are you saying that because this code is used as a User Event, it can't work in the Instance Creation Code?
 
I

immortalx

Guest
As said above, variable definitions is one of the nicest features of GMS2 for things like this. Here's an example that was used in my game:


This gate object is used to spawn enemies in the various rooms. First, you define some variables and their default values in the object itself. Then in every instance of this object you place in a room, you can override these values to whatever you want. And finally, you handle what to do with those variables in your scripts and events.
 
Thanks @immortalx for the reply, I'll have to look into the variables.

I actually just did what @Sam04 said about "item_inside = item_you_want;" and I put "chest_item_ = s_heart" in The Create Event of the chest. Then added "chest_item" to the "set_player_found_item(s_heart);" in place of "s_heart" then added "chest_item_ = s_gem" to the Instance Creation Code area and it seems to be working.

Saves me lots of trouble making a millions chests and signs lol.
 
Ok, most of it is working now except I ran into a small problem. I set these values in the chest object's Create Event
Code:
chest_item_ = s_heart;
amount_to_add_ = 1;
item_to_add_to_ = global.player_HP;
So I added the values in the Instance Creation Code for the chest in the Room

Code:
chest_item_ = s_gem;
amount_to_add_ = 50;
item_to_add_to_ = global.player_gems;
and they all work except for the "item_to_add_to_ = global.player_gems". I tested it out and it only works if "global.player_gems" is hard coded in the original chest object. Anyone know what's going on?
Can global variables like "global.player_gems" not be stored in variables?
 
...How are you using item_to_add_to_? Where else in code?
This is the only place.
Here's exactly what's in the Create Event for the main Chest Object:
Code:
event_inherited();

chest_item_ = s_heart;
amount_to_add_ = 1;
item_to_add_to_ = global.player_HP;

image_speed = 0;
image_index = 0;
//depth = -bbox_bottom;

if is_in_destroyed_list(id) {
    image_index = 0;
    activatable_ = false;
}
The colored values are the ones I want to replace.
All other values work, except for the "item_to_add_to_"
Code:
event_inherited();

if !instance_exists(o_player) exit;

image_index = 1;

set_player_found_item(chest_item_);
item_to_add_to_ += amount_to_add_;

activatable_ = false;
add_to_destroyed_list(id);
But if I hard code like this, the item amount is added. It's like the "item_to_add_to_" is ignoring that the "global.player_gems" is stored in it.
Code:
set_player_found_item(chest_item_);
global.player_gems += amount_to_add_;
Btw, the Chest Object does have a Parent "o_interactable" with the following Create Event code:
Code:
activatable_ = true;

enum interactable {
    activate
}
 

TheouAegis

Member
You cannot use a variable as a pointer to another variable in GM. You could replace all the global variables with a single array in which case item_to_add_to_ can hold the index of the array to add to.

Edit: In GMS2 you can pass variable names as strings, but that is poor, slow coding.
 
You cannot use a variable as a pointer to another variable in GM. You could replace all the global variables with a single array in which case item_to_add_to_ can hold the index of the array to add to.

Edit: In GMS2 you can pass variable names as strings, but that is poor, slow coding.
It seemed to work for everything else. Such as (during player movement states, state_ = starting_state_ when state_ already has a value and is stored in another.)
Tried it again with an object reference like, item_to_add_to_ = o_arrow_item; and still item_to_add_to_ ignores the value stored.
 
Last edited:

TheouAegis

Member
What I mean is

item_to_add_to_ += amount_to_add

is only modifying the variable item_to_add_to_, not the variable you stored in it. So like if you had

item_to_add_to_ = score

and score was 0 at the time, then item_to_add_to_ will also be 0. So then

item_to_add_to_ += amount_to_add

will just affect item_to_add_to_, NOT score.
 
Then how can I change what get's added to if it won't let me specify it using item_to_add_to_ = global.player_HP;?

Since it only works the way I want it to this way:
Code:
set_player_found_item(chest_item_);

global.player_gems += amount_to_add_;
But this way would mean I'd have to create a Gem Chest, Heart Chest, Lives Chest, etc.

So far, this is the only code I have in the Creation Code of the chest object in the room:
Code:
chest_item_ = s_gem;
amount_to_add_ = 50;
It let's me specify what's in the chest and the amount. But how do I easily change where to add it too? (global.player_gems, global.player_HP, global.player_lives, etc.)
 

TheouAegis

Member
I told you. Instead of using individual variables like global.hp and global.gems, you can use one single array for all of those and then item_to_add_to would be which index in that array to add to.

Or you can inappropriately try to use variable_set_global().

Or you store some arbitrary value like 1 for hp, 2 for gems, etc. Then you have a switch statement checking the value of the variable when deciding where to add to.

Can I ask why you are not simply adding the values to those variables directly? Why are you trying to do a roundabout method of changing variables outside the instance?
 
D

Danei

Guest
You could give every chest a variable for each different item to add: added_health = 0; added_gems = 0; etc.
Then when a chest is created, set the appropriate variable(s) to a non-zero number whenever you set which item is in it. Then each chest can have the same code and doesn't have to worry about linking up with a specific variable.
 
Can I ask why you are not simply adding the values to those variables directly? Why are you trying to do a roundabout method of changing variables outside the instance?
For one thing, that's how the person in a online course I took did the player stats and I think it makes it (almost) easy to manage. I tend to stay away from arrays since I find them harder to work with.

You could give every chest a variable for each different item to add: added_health = 0; added_gems = 0; etc.
That's basically what I'm trying to do, but it's not working right.

But I guess if it's not going to work out right, I can just make separate chests for the player stats and items. I'll just have to make a couple more objects.
 
Top