• 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!

Trying To Get Power Cables And Batterys To Work

L

Logan Bevans

Guest
So im trying to get power cables to work like transfer power from a generator to lets say a battery iv gotten most of it working but how i have it set up its triggering all the cables so if i have a cable connected to a generator to a battery and another separate cable (not connected to a generator) connected to a battery once i turn on the generator both cables are activated and both charge the battery im not quite sure how to fix this any help would be appreciated :)

Conduit Creation Code://////////////////
image_index = 0;
image_speed = 0;

breaking = false;
hardness = 1;
index = 0;
item = obj_conduit_item;

transferRate = 10;
outputPower = 20;

Generator Step Even: //////////////////////
if place_meeting(x+16,y,obj_conduit)
or place_meeting(x-16,y,obj_conduit)
or place_meeting(x,y+16,obj_conduit)
or place_meeting(x,y-16,obj_conduit){

if place_meeting(x,y-32,obj_cobble){
image_index = 1;
obj_conduit.outputPower = 20;
}
if !place_meeting(x,y-32,obj_cobble){
image_index = 0;
obj_conduit.outputPower = 0;
}
}

Battery Creation Code://///////////////
image_index = 0;
image_speed = 0;

breaking = false;
hardness = 0.75;
index = 0;
item = obj_leadstonecell_item;

batteryCapacity = 0;
powerTransfer = 10;
Timer = false;
maxCapacity = 1000;
transferRate = 10;

Battery Step Event:////////////////
///Charging
if instance_exists(obj_conduit){
if place_meeting(x+16,y,obj_conduit)
or place_meeting(x-16,y,obj_conduit)
or place_meeting(x,y+16,obj_conduit)
or place_meeting(x,y-16,obj_conduit){
if obj_conduit.outputPower > 0 {
Timer = true;
}
}

if Timer = true{
powerTransfer -=1;
}
if powerTransfer < 0 {
powerTransfer = transferRate;
batteryCapacity += obj_conduit.outputPower;
}

if batteryCapacity > maxCapacity {
Timer = false
batteryCapacity = maxCapacity;
powerTransfer = 0;
}
if obj_conduit.outputPower = 0{
Timer = false
powerTransfer = 0;
}

if batteryCapacity = 10{
image_index = 1;
}
if batteryCapacity = 160{
image_index = 2;
}
if batteryCapacity = 320{
image_index = 3;
}
if batteryCapacity = 480{
image_index = 4;
}
if batteryCapacity = 640{
image_index = 5;
}
if batteryCapacity = 800{
image_index = 6;
}
if batteryCapacity = 1000{
image_index = 7;




}
}

Just A visual of what i mean the red boxes are the batterys and they all charged up even though the once to the left arnt connected to a generator, im assuming its something to do with how i call the variable obj_conduit.outputpower from the battery or generator thats doing it.
 
S

spoonsinbunnies

Guest
you are correct in thinking this, the easiest fix I can think of is to change your place meeting calls into instance place to save ids like so
Battery Step Event:////////////////
///Charging
if instance_exists(obj_conduit){
a= instance_place(x+16,y,obj_conduit)
if a=noone
a=instance_place(x-16,y,obj_conduit)
if a=noone
a= instance_place(x,y+16,obj_conduit)
if a=noone
a=instance_place(x,y-16,obj_conduit){
if a!=noone{
if a.outputPower > 0
Timer = true;}

now anywhere where you reference obj_conduit after this you can change it to a.
 
L

Logan Bevans

Guest
ohh okay thanks for responding quickly ill go and try it out
 
L

Logan Bevans

Guest
you are correct in thinking this, the easiest fix I can think of is to change your place meeting calls into instance place to save ids like so
Battery Step Event:////////////////
///Charging
if instance_exists(obj_conduit){
a= instance_place(x+16,y,obj_conduit)
if a=noone
a=instance_place(x-16,y,obj_conduit)
if a=noone
a= instance_place(x,y+16,obj_conduit)
if a=noone
a=instance_place(x,y-16,obj_conduit){
if a!=noone{
if a.outputPower > 0
Timer = true;}

now anywhere where you reference obj_conduit after this you can change it to a.
if i was to access that variable from the conduit would i have to do obj_battery.a to do it. i had obj_conduit.outputPower in the battery to get the variable but i think that is were the problem lied
 
L

Logan Bevans

Guest
ok so i have a generator, conduit and a battery i put conduit beside the generator and a battery beside the conduit i put a piece of cobble on top of the generator to turn it on that poweres the battery but if i place a conduit anywere else when one piece is powered and place a battery next to it will power it aswell
obj_conduit creation code: ----------------------------
image_index = 0;
image_speed = 0;

breaking = false;
hardness = 1;
index = 0;
item = obj_conduit_item;

outputPower = 0;
------------------------------------------------------------
obj_leadstonecell(battery) creation code:------------------------------------
///Variables
image_index = 0;
image_speed = 0;

breaking = false;
hardness = 0.75;
index = 0;
item = obj_leadstonecell_item;


batteryCapacity = 0;
powerTimer = 10;
Timer = false;
maxCapacity = 1000;
-------------------------------------------------------------
obj_leadstonecell Step Event: -------------------------------
///Charging
if instance_exists(obj_conduit){
if place_meeting(x+16,y,obj_conduit)
or place_meeting(x-16,y,obj_conduit)
or place_meeting(x,y+16,obj_conduit)
or place_meeting(x,y-16,obj_conduit){
if obj_conduit.outputPower = 10{
Timer = true;
}
}
}
if Timer = true{
powerTimer -=1;
}
if powerTimer < 0 {
batteryCapacity += obj_conduit.outputPower;
powerTimer = 10;
}

if batteryCapacity > maxCapacity {
Timer = false
batteryCapacity = maxCapacity;
powerTimer = 0;
}
if obj_conduit.outputPower = 0{
Timer = false
powerTimer = 0;
}

if batteryCapacity = 10{
image_index = 1;
}
if batteryCapacity = 160{
image_index = 2;
}
if batteryCapacity = 320{
image_index = 3;
}
if batteryCapacity = 480{
image_index = 4;
}
if batteryCapacity = 640{
image_index = 5;
}
if batteryCapacity = 800{
image_index = 6;
}
if batteryCapacity = 1000{
image_index = 7;

if !place_meeting(x-16,y,obj_conduit)
or !place_meeting(x-16,y,obj_conduit)
or !place_meeting(x,y+16,obj_conduit)
or !place_meeting(x,y-16,obj_conduit){
Timer = false
powerTransTime = 0;
}
}
--------------------------------------------------------------
obj_generator_coal Step Event: ------------------------------------
if place_meeting(x+16,y,obj_conduit)
or place_meeting(x-16,y,obj_conduit)
or place_meeting(x,y+16,obj_conduit)
or place_meeting(x,y-16,obj_conduit){

if place_meeting(x,y-32,obj_cobble){
image_index = 1;
obj_conduit.outputPower = 10;
}
if !place_meeting(x,y-32,obj_cobble){
image_index = 0;
obj_conduit.outputPower = 0;
}
}
-------------------------------------------------------
 
L

Logan Bevans

Guest
Ohh Yeah i though it was something to do with how i called the variables how would i do it then something like this?

with (obj_conduit){
outputPower = 0;
}

and

with (obj_conduit){
if outputPower >0{
Timer = true;
}

and

if powerTimer < 0 {
with (obj_conduit){
batteryCapacity += outputPower;
powerTimer = 10;
}
}
 

Relic

Member
That’s not right either:

“With(X)” makes the block of code run inside ALL X active in the room.

With(obj_conduit) will run the code inside every obj_conduit in the room. I don’t think you want all of them to change power, just the one connected?

You need a way to get the instance id of the important obj_conduit. You were close before- instead of place_meeting which returns true if there is a collision, use instance_place which returns the id of the instance in the collsion.

connected_conduit=instance_place(x+16,y,obj_conduit)

//now connected_conduit is the id of the important conduit
connected_conduit.outputPower=0

Or you could also do:
with (connected_conduit) {outputPower=0}

The point is, when more than one instance is on the room you MUST use the instance id to change variables, not the object index.
 
L

Logan Bevans

Guest
That’s not right either:

“With(X)” makes the block of code run inside ALL X active in the room.

With(obj_conduit) will run the code inside every obj_conduit in the room. I don’t think you want all of them to change power, just the one connected?

You need a way to get the instance id of the important obj_conduit. You were close before- instead of place_meeting which returns true if there is a collision, use instance_place which returns the id of the instance in the collsion.

connected_conduit=instance_place(x+16,y,obj_conduit)

//now connected_conduit is the id of the important conduit
connected_conduit.outputPower=0

Or you could also do:
with (connected_conduit) {outputPower=0}

The point is, when more than one instance is on the room you MUST use the instance id to change variables, not the object index.
Ohhh ok i got confused between the two ill test this out thanks im still learning :)
 
L

Logan Bevans

Guest
That’s not right either:

“With(X)” makes the block of code run inside ALL X active in the room.

With(obj_conduit) will run the code inside every obj_conduit in the room. I don’t think you want all of them to change power, just the one connected?

You need a way to get the instance id of the important obj_conduit. You were close before- instead of place_meeting which returns true if there is a collision, use instance_place which returns the id of the instance in the collsion.

connected_conduit=instance_place(x+16,y,obj_conduit)

//now connected_conduit is the id of the important conduit
connected_conduit.outputPower=0

Or you could also do:
with (connected_conduit) {outputPower=0}

The point is, when more than one instance is on the room you MUST use the instance id to change variables, not the object index.
///Charging
if instance_exists(obj_conduit){
connected_conduit = instance_place(x+16,y,obj_conduit)
if connected_conduit = noone
connected_conduit = instance_place(x-16,y,obj_conduit)
if connected_conduit = noone
connected_conduit = instance_place(x,y+16,obj_conduit)
if connected_conduit = noone
connected_conduit = instance_place(x,y-16,obj_conduit){

if connected_conduit.outputPower > 0
Timer = true;}
}


if Timer = true{
powerTimer -=1;
}

if powerTimer < 0 {
batteryCapacity += connected_conduit.outputPower;
powerTimer = 10;
}


if batteryCapacity > maxCapacity {
Timer = false
batteryCapacity = maxCapacity;
powerTimer = 0;
}

if connected_conduit.outputPower = 0{
Timer = false;
powerTimer = 0;
}

This is what i put into the batteries step event everything seems to work exept for one thing i can only place the blocks in a specific order or i get this error:


FATAL ERROR in
action number 1
of Step Event0
for object obj_leadstonecell:

Variable obj_leadstonecell.connected_conduit(100058, -2147483648) not set before reading it.
at gml_Object_obj_leadstonecell_StepNormalEvent_1 (line 32) - if connected_conduit.outputPower = 0{
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_leadstonecell_StepNormalEvent_1 (line 32)

thats if i place a battery by itself than:


FATAL ERROR in
action number 1
of Step Event0
for object obj_leadstonecell:

Variable <unknown_object>.<unknown variable>(100059, -2147483648) not set before reading it.
at gml_Object_obj_leadstonecell_StepNormalEvent_1 (line 11) - if connected_conduit.outputPower > 0
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_leadstonecell_StepNormalEvent_1 (line 11)

once i have already placed a gen a conduit and a battery and i try to place another battery somewhere else im assuming its something to do with addressing the noone
 
L

Logan Bevans

Guest
I figured out the first error just left with the last one ima try to fix it but if you have any suggestions would be nice
 

Relic

Member
Understanding what the error messages say is definitely the next step in improving your game development process- won’t have to come back here each time!

Look at the error message again and see how I know the following has occurred:

In the step event of obj_lead stone cell at line 11 of the code you have tried to access a variable of an instance that does not exist. This line tries to access connected_conduit.outputPower. I reckon connected_conduit does not yet exist. You should set connected_conduit= no one in the create event as a start and make sure you continue to check if instance_exists(connected_conduit) before trying to access its variables.
 
L

Logan Bevans

Guest
Understanding what the error messages say is definitely the next step in improving your game development process- won’t have to come back here each time!

Look at the error message again and see how I know the following has occurred:

In the step event of obj_lead stone cell at line 11 of the code you have tried to access a variable of an instance that does not exist. This line tries to access connected_conduit.outputPower. I reckon connected_conduit does not yet exist. You should set connected_conduit= no one in the create event as a start and make sure you continue to check if instance_exists(connected_conduit) before trying to access its variables.
upload_2019-1-25_19-44-46.png
Thanks for your help sorry i was bothering you :)
got everything working pretty good just gotta figure out how to get it to work with more than one cable to pass on the variable
 

Relic

Member
It’s no bother - this section of the forum is for asking and receiving help! The only thing that can be annoying is when forum members ask for how to do something before they have tried themeselves. You have the right approach.
 
L

Logan Bevans

Guest
It’s no bother - this section of the forum is for asking and receiving help! The only thing that can be annoying is when forum members ask for how to do something before they have tried themeselves. You have the right approach.
iv tried something like this im not sure if im in the right direction with it its not doing anything

Conduit Step Even:
if instance_exists(obj_conduit){
connected_conduit = instance_place(x+16,y,obj_conduit)
if connected_conduit = noone
connected_conduit = instance_place(x-16,y,obj_conduit)
if connected_conduit = noone
connected_conduit = instance_place(x,y+16,obj_conduit)
if connected_conduit = noone
connected_conduit = instance_place(x,y-16,obj_conduit){

if connected_conduit != noone{
outputPower = connected_conduit.outputPower;
}
}
}
 
Top