lolol
lolol

Reputation: 69

Game Maker - Not all objects work

This is my code (Placed in a step event)

if (place_meeting(x,y,Obj_Player))
{
    time = 50
}
if (time > 0)
{
    Obj_Player.jsp = 17;
}
else
{
    Obj_Player.jsp = 12;
}
if (time > 0) time -= 1;

Placed in a create event:

image_speed = 0.4;
time = 0;
new = 17

For some reason, one object with this code will work, but the rest will not. I've no idea why and I need multiple items in the room.

Also, the items in the create event, I would like to move into the creation code of each individual item. I only want to edit new so will I have to move all the code or just new?

Upvotes: 0

Views: 421

Answers (1)

Dmi7ry
Dmi7ry

Reputation: 1777

When you use Obj_Player.blablabla, GMS will get first created instance of the object. You need use function which will return id of instance. For example, instance_place(). I'm not sure what exactly you doing, so code can be unusable:

Example with one global timer for all instances:

var obj = instance_place(x, y, Obj_Player);
if obj != noone
{
    with Obj_Player
    {
        time = 50;
    }
}

with Obj_Player
{
    if (time > 0) time--;
}

Next is Obj_Player:

// Create
time = 0;

// Step
if time > 0
    jsp = 17;
else
    jsp = 12;


Example with an independent local timer for each instance:

var obj = instance_place(x, y, Obj_Player);
if obj != noone
    obj.time = 50;

Next is Obj_Player:

// Create
time = 0;

// Step
if time > 0
    jsp = 17;
else
    jsp = 12;

if (time > 0) time--;

upd:

Just to clarify, I have one Obj_Player and multiple items which contain the code.

I think I understand what you trying to do

Obj_Player, Create:

time = 0;

Obj_Player, Collision event (with your object):

time = 50;

Obj_Player, Step event:

time--;
if time > 0
    jsp = 17;
else
    jsp = 12;

That's all.

As you can see, all code placed at Obj_Player. Because when you do

if time > 0
    Obj_Player.jsp = 17;
else
    Obj_Player.jsp = 12;

and you have multiple instances with this code, jsp will be 17 only when all instances will has time > 0. Actually, jsp will contain result of check of the last instance (as the last check anyway will replace any previous results).




Also, the items in the create event, I would like to move into the creation code of each individual item. I only want to edit new so will I have to move all the code or just new?

Only new. Firstly will be called Create event (if we're talking about GMS, not about GM8) and then Creation code, so using Creation code you can just modify all, what was in Create event.

Upvotes: 1

Related Questions