Legacy GM Instance seeking variable in other instance?

B

Brickyoyo

Guest
I'm having trouble coming up with a solution to this problem.

So I have multiple instances of the same object (obj_switch) and in the instance creation code, I have a variable set called "channel" which is just a number. I want switches with the same channel number to act together, so if they are all on, then they all set their "powering" variable to true and when just one is off, they are all off.

I came up with something like this (it's in the obj_switch step event)
Code:
with (obj_switch) {
    if (other.channel == channel) {
        if (other.selfpower == true) && (selfpower== true) {
            powering = true;
            other.powering = true;
        }
        else {
            powering = false;
            other.powering = false;
        }
    }
}
The idea is if they all run it, they all check each other, but it isn't working.

Sorry if this is confusing.
 

jazzzar

Member
Ok that's can be done using a simple controller object, that's how i would go about it, in the controller create event add global.array[0]=noone;
And global.i=0;
Now in obj_switch create event add this :
Code:
global.[global.i]=id;
global.i++;
Now in the step event of controller :
Code:
For (var j=0;i<global.i;j++)
{
    for (var k=1;k<global.i;k++)
    {
        if global.array[k].channel==global.array[j].channel
{
if global.array[j].powering==true
    global.array[k].powering=true;
else global.array[k].powering=false;

}
    }
}
There is definitely an easier way for this, but you can't blame me now it's 2:35AM am already dead, hope this helps
 
M

maartenvt

Guest
Well, if a switch gets turned off/on, you know the channel of that particular switch right? Than you can run a width construct from that switch, and only turn the switches on/off if their channel is equal to other.channel.

Alternatively, you could regulate this centrally in a controller object, like @jazzzar suggested, which could hold an array (i.e. called 'channels'). Then you could make channels[N] hold a list of all switches that have channel N. This way you dont have to loop over all switches everytime you turn a switch, but simply consult the list of switches that have the same channel :)
 
B

Brickyoyo

Guest
Well, if a switch gets turned off/on, you know the channel of that particular switch right?
Well no, the way I have it, when the player presses a button and they're in a certain range, the powering variable of that instance turns true, which is detected by wire objects to carry the power down the line.

Then you could make channels[N] hold a list of all switches that have channel N.
You mean have a separate array for each channel? I.e. channel[1], channel[2]...
 
M

maartenvt

Guest
Ok now I'm a bit confused as to how your switch system works. Could you give some more details? You have switches that can be powered and then what? (some actual code might help too)
 

jazzzar

Member
Well no, the way I have it, when the player presses a button and they're in a certain range, the powering variable of that instance turns true, which is detected by wire objects to carry the power down the line.


You mean have a separate array for each channel? I.e. channel[1], channel[2]...
Did you try what i suggested? And yeah as @maartenvt said, after your last reply i'm confused :/
 
B

Brickyoyo

Guest
I'm not home right now so I can't give you any code, so I'll try to explain.

I have two switch objects, the one that will have channels, and one that ignores channels. That one is done and working. With all the switches, when a player is colliding with them and presses 'Z', the switch toggles on or off. In the non-channel switch, the powering variable is set to true and wire objects detect this so they set their powered vars to true so the signal gets passed down the line.

In the switch with channels, I want every instances' variable powered set true when they are all turned on, which I can calculate using another var (let's call it myPower) so it doesn't interfere with the powered var the wires detect.

If all switches with say channel = 1 have myPower = true, then I want all their powering vars also set to true. And if myPower isn't all true in the channel, then all powering in that channel = false

Also @jazzzar, in your first for statement, did you mean j<global.i?
 

jazzzar

Member
I'm not home right now so I can't give you any code, so I'll try to explain.

I have two switch objects, the one that will have channels, and one that ignores channels. That one is done and working. With all the switches, when a player is colliding with them and presses 'Z', the switch toggles on or off. In the non-channel switch, the powering variable is set to true and wire objects detect this so they set their powered vars to true so the signal gets passed down the line.

In the switch with channels, I want every instances' variable powered set true when they are all turned on, which I can calculate using another var (let's call it myPower) so it doesn't interfere with the powered var the wires detect.

If all switches with say channel = 1 have myPower = true, then I want all their powering vars also set to true. And if myPower isn't all true in the channel, then all powering in that channel = false

Also @jazzzar, in your first for statement, did you mean j<global.i?
yes i did min j < global.i , i'll wait for you to get home so maybe you can provide some code
 
B

Brickyoyo

Guest
Ok, so @jazzzar, I played with your code and got somewhere.
Code:
event_inherited();
if (keyboard_check_pressed(global.select)) && (active = true) {    // Active var is from parent, ignore
    powering = !powering;
}

if (powering = false) {
    sprite_index = spr_light_switch_off; 
}
if (powering = true) {
    sprite_index = spr_light_switch_on;
}

for (var j = 0; j < global.i; j++) {
    for (var k = 1; k < global.i; k++) {
        if global.channels[k].channel == global.channels[j].channel {
            if global.channels[j].powering == true {
                global.channels[k].powering = true;
            }
            else {
                global.channels[k].powering = false;
            }
        }
    }
}
this is in the switch step event

And I did the other stuff you mentioned before. The problem is now I have three switches one on channel 2 and two on channel 1. channel 2 works great, but with channel 1, one switch activates both, so the channel seems to be telling something to the other switch, but the other switch doesn't do anything.

Switch 1 and Switch 2 are on Channel 1.
Switch 1 turns on Switch 2 and itself.
Switch 2 doesn't affect anything, whether it's on or off, Switch 1 will turn it on or off
 
B

Brickyoyo

Guest
Okay, quick update. I changed the code around.
Code:
var j;
for (j = 0; j < instance_number(obj_switch); j += 1) {
    global.channels[j] = instance_find(obj_switch, j);
    if (global.channels[j].channel == channel) {
        if (global.channels[j].myPower == true) && (myPower == true) {
            global.channels[j].powering = true;
            powering = true;
        } else {
            global.channels[j].powering = false;
            powering = false;
        }
    }
}
(In step event of obj_switch)

With one switch on a channel, It turns on and off just as it should because it's the only switch on a channel.

With two or more on a channel, the last instance with that channel to be initialized (closest to bottom in instance order,) will turn on and off independently from the others. But the others in the channel that were initialized before the last, are dependent of the last instance. They will only powering = true if both their and the last instance's myPower vars = true.

Basically, it sort of works but I need it so they are all dependent of each other, or so the last instance won't powering = true unless it and the other's myPower all = true.
 
B

Brickyoyo

Guest
@jazzzar
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_switch:

Variable <unknown_object>.<unknown variable>(100054, -2147483648) not set before reading it.
at gml_Object_obj_switch_StepNormalEvent_1 (line 31) - if (global.channels[j].channel == channel) {
############################################################################################

Just adding that = gives this error. I think it may have something to do with every instance trying to run it on each other? I didn't touch anything else btw.
 

jazzzar

Member
@jazzzar
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_switch:

Variable <unknown_object>.<unknown variable>(100054, -2147483648) not set before reading it.
at gml_Object_obj_switch_StepNormalEvent_1 (line 31) - if (global.channels[j].channel == channel) {
############################################################################################

Just adding that = gives this error. I think it may have something to do with every instance trying to run it on each other?
forgive my stupidness ofcourse it's an error, i don't have anything in mind right now sorry :p
 

jazzzar

Member
Ok... Well do you know why this gives an error?
yep, because when j reaches instance_number(obj_switch) it is getting beyond the limit of the second argument that instance_find takes, which is instance_number(obj_switch)-1, you should use j < instance_number(obj_switch)
 
B

Brickyoyo

Guest
yep, because when j reaches instance_number(obj_switch) it is getting beyond the limit of the second argument that instance_find takes, which is instance_number(obj_switch)-1, you should use j < instance_number(obj_switch)
That makes sense...Yeah, I don't really see how you could fix that. I'll keep trying though
 
B

Brickyoyo

Guest
Well, I solved it. But my solution is kind of exclusive to my case. As mentioned previously, I have wire objects that basically transport power signals from sources to things like light bulbs. I have different kinds of switches, one which just outputs a signal when activated, and one which acts more like a real world switch, where it can be open or closed and doesn't give off a signal, it just allows current to flow or not. This was basically what I was trying to achieve so...yay?
1.PNG
Nothing is on
2.PNG
The switch with power is on (right corner)
3.PNG
Switch on left corner sees power going in and is activated by player so it is closed, turning on the light
4.PNG
Left switch is still on, but since there's no power, light is off

Also, just wanted to say thank you @jazzzar for all your help
 

jazzzar

Member
Well, I solved it. But my solution is kind of exclusive to my case. As mentioned previously, I have wire objects that basically transport power signals from sources to things like light bulbs. I have different kinds of switches, one which just outputs a signal when activated, and one which acts more like a real world switch, where it can be open or closed and doesn't give off a signal, it just allows current to flow or not. This was basically what I was trying to achieve so...yay?
View attachment 1848
Nothing is on
View attachment 1849
The switch with power is on (right corner)
View attachment 1850
Switch on left corner sees power going in and is activated by player so it is closed, turning on the light
View attachment 1852
Left switch is still on, but since there's no power, light is off

Also, just wanted to say thank you @jazzzar for all your help
yep actually yay, congrats, and welcome, the best of luck for you
 
Top