• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Creating unique objects

A

Arrogant

Guest
I have a problem with wanting the same objects to be separate from one another. I have a button which when activated it powers a object called wire, although it will power all the wires if only one of them is touching the button (GIF of the problem: https://gyazo.com/c6d769055e9aba089b9a1af6da0d31f7).

Step event code of the wire:
if(place_meeting(x,y, obj_wallbutton) && obj_wallbutton.powered == 1)
{
image_index = 1;
}
else
{
image_index = 0;
}

Step event code of the button:
if collision_rectangle(x+7,y+7,x+14,y+14,obj_clight, false, true)
{
image_speed = 0.5;
if(image_index == 15)
{
powered = 1;
image_speed = 0;
image_index = 15;

}
}
else
{
image_speed = -0.5;
if(image_index == 0)
{
powered = 0;
image_speed = 0;
image_index = 0;
}
}

So how would I go about only changing the state of the wire which is touching the button instead of all the wire objects? Also as shown in the GIF, only one of the buttons powers the wire, how could I change this?
Please excuse me if the problem is obvious, i'm very tired :D
Thanks in advance.
 

jo-thijs

Member
Hi and welcome to the GMC!

Can wires be connected to each other?
Looking at your code, I'd guess not.

In that case, you can just simply change the step event of the wire object to:
Code:
image_index = 0;
with obj_wallbutton
    if powered && place_meeting(x, y, other) {
        other.image_index = 1;
        break;
    }
Otherwise, try this:
1) Delete the step event of the wire object
2) Put in the begin step event of the wire object: image_index = 0;
3) Put in the event user 0 event of the wire object:
Code:
if image_index == 0 {
    image_index = 1;
    for(var i = 0; i < 360; i += 90)
        with instance_place(x + lengthdir_x(1, i), y + lengthdir_y(1, i), obj_wire)
            event_user(0);
}
(You might need to change this a bit to get the correct distances and directions)

4) Add this to the step event of obj_wallbutton:
Code:
for(var i = 0; i < 360; i += 90)
    with instance_place(x + lengthdir_x(1, i), y + lengthdir_y(1, i), obj_wire)
        event_user(0);
 
A

Arrogant

Guest
Not sure if I have done it wrong, but doing this gives all the wire objects a constant power?
 

jo-thijs

Member
No, I made a mistake.
4 should have been:
Code:
if powered
    for(var i = 0; i < 360; i += 90)
        with instance_place(x + lengthdir_x(1, i), y + lengthdir_y(1, i), obj_wire)
            event_user(0);
 
A

Arrogant

Guest
Thanks so much it worked well and I was able to implement it to work with other objects, but it powers only 2 of the wires and the second wire in that sequence is powered determined by when they were created.
(e.g.
https://gyazo.com/efbb672a6bd674ec2dc5078636a1a9aa
In that gif the two powered wires were put into the editor before the top one.)
Anyway to fix this?
 

jo-thijs

Member
Hm, it should not be based on which wire is created first.
Are you sure it depends on this?

I also wonder, what is the size and shape of a wire?
How are they created?
 
A

Arrogant

Guest
Well I tested putting the wires in, in different orders and it always powered the wire I put in first.
(gif of what happens when I put the top wire into the editor first: https://gyazo.com/668fb07ff1794f2506c5a05b7a450a97)

The size of the initial wire is 4*4 pixels, but in the editor I stretch them but keep either 4 pixel width or 4 pixel height.
 

jo-thijs

Member
Oh, I see.
Your wires overlap and are quite long.
I should have mentioned that I based my code on a tile mechanism.

Anyway, try this as new user defined event of the wire objct:
Code:
if image_index == 0 {
    image_index = 1;
    for(var i = 0; i < 360; i += 90)
        with obj_wire
            if place_meeting(x + lengthdir_x(1, i), y + lengthdir_y(1, i), other)
                event_user(0);
}
and this as the code in the step event of obj_wallbutton:
Code:
if powered
    for(var i = 0; i < 360; i += 90)
        with obj_wire
            if place_meeting(x + lengthdir_x(1, i), y + lengthdir_y(1, i), other)
                event_user(0);
 
Top