Legacy GM Some of my code has stopped working?

A

Arrogant

Guest
So yesterday to add to my wiring system in my game, I created logic gates for it (NOT, AND, XOR and OR). These worked perfectly up until I opened gms today and found randomly that some of them just didn't work.

Full adder I put into my game not working anymore(when all inputs are 1, the outputs should both be 1):
https://gyazo.com/e360722406d19c855e403c8219eac222

2 of these 3 AND gates work, yet I see no reason the third one doesn't work:
https://gyazo.com/39d1efd9121aa10a457a8d9ba8642575

Example of a gate working (XOR):
https://gyazo.com/f15be6e1bfe0b59be04e67c73f27f6d6

The adder, AND gates and others were working yesterday, thus i'm extremely confused why they are not working today. I'll post as much as I think you would need to about the AND gate to save time and because gates such as OR are very similar.

Wire object:
Wire object create:
Code:
image_speed = 0;
powered = 0;
Wire object begin step:
Code:
image_index = 0;
Wire object step:
Code:
powered = 0;
Wire object event user 0:
Code:
powered = 1;

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);
                }


for(var i = 0; i < 360; i += 90)
        with instance_place(x + lengthdir_x(1, i), y + lengthdir_y(1, i), obj_notGate)
        {
            event_user(0);
        }
    
}

AND gate:
AND gate create:
Code:
image_speed = 0;
right = 0;
left = 0;
And gate step:
Code:
with instance_place(x+1, y, obj_wire)
{
    if(powered == 1)
    {
        other.right = 1;
    }
    else
    {
        other.right = 0;
    }
}
with instance_place(x-1, y, obj_wire)
{
    if(powered == 1)
    {
        other.left = 1;
    }
    else
    {
        other.left = 0;
    }
}


if(left == 1 and right == 1)
{
    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_connector)
        {
            event_user(0);
        }
}
AND gate begin step:
Code:
image_index = 0;

Connector object:
Connector object create:
Code:
image_speed = 0;
Connector object begin step:
Code:
image_index = 0;
Connector user event 0:
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);
        }
As for the "buttons" they just activate the event user 0 of the wire object.
The XOR gate which is seen working is completely the same apart from this:
if(left == 1 or right == 1)
{
if(left = 0 or right = 0)
{}
}
(Note that even though the XOR in the gyazo is working, there are other instances of it not working

The way it works is it looks for two wire objects on either side of it, then sees if their "powered" variable = 1, then if both wires on either side of it are powered, it will output to the event user of the connector obj which outputs to any wire object around it. (The reason I created the connector object was so the signal didn't back track (couldn't think of another way to do it))

If you need any more information please ask.
Sorry for the long post and thanks in advance.
 

TheouAegis

Member
You can simplify your code a little. For example:

with instance_place(x+1,y, obj_wire) other.right = powered;
with instance_place(x-1, y, obj_wire) other.left = powered;
if left & right //see what I did there?
{


Why is your wire's user event so different from the connector's user event? You essentially check just 4 wires in the connector's user event, but you check all wires in the wire's user event.
 
Last edited:
A

Arrogant

Guest
Ty, still new to game maker and always learning of ways to become more efficient :)
Also I didn't because I don't think I need to. All the connector object does is pass a signal to the wire object, so nothing needs to check if it is powered. I think?


*EDIT: Umm, i'm not sure, but I don't think it matters because the wires never come in from any side that isn't up,down,left or right
 
Last edited by a moderator:

TheouAegis

Member
@Arrogant If you're referring to my first edit, I removed that because I didn't see the wire's code. So i put a different question at the end of my post.

Update:
Going on with the optimized code I gave you, that's really all you'd need for the gates' codes, just replacing the operator. I mean, all of your gates are basically just binary values, so you can treat each wire as single bits (if you want to really optimize down the road). So with that said:

Code:
//AND gate
with instance_place(x+1,y, obj_wire) other.right = powered;
with instance_place(x-1, y, obj_wire) other.left = powered;
if left & right
Code:
//OR gate
with instance_place(x+1,y, obj_wire) other.right = powered;
with instance_place(x-1, y, obj_wire) other.left = powered;
if left | right
Code:
//XOR gate
with instance_place(x+1,y, obj_wire) other.right = powered;
with instance_place(x-1, y, obj_wire) other.left = powered;
if left ^ right
Edit: I misread the XOR code; I just learned it as only one value is set, not as one is set and the other not.
 
Last edited:
A

Arrogant

Guest
Btw even by optimising my code, the randomness of whether a gate works or not is still there
 
A

Arrogant

Guest
I think I've fixed it. It was something to do with the position within the step event I checked if the wire was powered. Due to me setting the wires power = 0 in its step event so once its event user 0 isn't being activated it turns off, I had made it so the AND gate (and others) checked the wires power after it had set its power to 0, thus some of the gates seemingly randomly weren't being powered. I think? Seems to fix it.
 
Top