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

Windows Power Left Mechanic Fnaf

A

ArcaPlaysYT

Guest
so I've been working on a fnaf fangame for a while and have hit a snag when finding how to make a power indicator. I did find one thread that mentioned this, but it wasn't that helpful. all I have is an object with an image that says "POWER:". I need to know how to display the percent of power left and how to make it decrease at a flat rate and decrease more when the cameras, doors, and lights are being used. any help will be good. thanks
 

jo-thijs

Member
so I've been working on a fnaf fangame for a while and have hit a snag when finding how to make a power indicator. I did find one thread that mentioned this, but it wasn't that helpful. all I have is an object with an image that says "POWER:". I need to know how to display the percent of power left and how to make it decrease at a flat rate and decrease more when the cameras, doors, and lights are being used. any help will be good. thanks
Hi and welcome to the GMC!

In your power object or in some controller object, you can use a variable to keep track of the currently remaining amount of power.
For example, in the create event of your power object (let's call it obj_power), you can write:
GML:
power = 100;
This will create the variable power in obj_power and set its value to 100, representing 100% power.
We can then also use a variable to keep track of the current power consumption.
In the create event of obj_power:
GML:
consumption = 0.01;
This will be the amount of power that gets consumed every step (or every frame).
In the door object, when you close the door, you can increase the consumption:
GML:
obj_power.consumption += 0.01;
When you open the door, you can decrease it again:
GML:
obj_power.consumption -= 0.01;
Then, in the step event of obj_power, we need to decrease the consumption from the power:
GML:
power -= consumption;
This subtracts consumption from power every step.
In the draw event, you can then draw the power percentage and power consumption to the screen.
So, in the draw event of obj_power, you can do something like:
GML:
draw_self(); // This will draw the "POWER" text
draw_text(x + 160, y, string(power) + "%"; // This draws the power percentage, but you will need to tweak the exact location this gets drawn to
draw_sprite(spr_power_consumption, 0, x + 160, y + 32); // This draws the first power consumption square to the screen
if (consumption > 0.01) { // If consumption is high enough to draw a second square
    draw_sprite(spr_power_consumption, 0, x + 160 + 32, y + 32);
}
if (consumption > 0.02) { // If consumption is high enough to draw a third square
    draw_sprite(spr_power_consumption, 0, x + 160 + 64, y + 32);
}
// And repeat this for all your squares
You will need to tweak some numbers in this code and it can be optimized a lot, but it may be a good starting point.

I hope this helps!
If you have any questions, don't hesitate to ask them!
 
Last edited:
A

ArcaPlaysYT

Guest
Hi and welcome to the GMC!

In your power object or in some controller object, you can use a variable to keep track of the currently remaining amount of power.
For example, in the create event of your power object (let's call it obj_power), you can write:
GML:
power = 100;
This will create the variable power in obj_power and set its value to 100, representing 100% power.
We can then also use a variable to keep track of the current power consumption.
In the create event of obj_power:
Code:
consumption = 0.01;
This will be the amount of power that gets consumed every step (or every frame).
In the door object, when you close the door, you can increase the consumption:
Code:
obj_power.consumption += 0.01;
When you open the door, you can decrease it again:
Code:
obj_power.consumption -= 0.01;
Then, in the step event of obj_power, we need to decrease the consumption from the power:
Code:
power -= consumption;
This subtracts consumption from power every step.
In the draw event, you can then draw the power percentage and power consumption to the screen.
So, in the draw event of obj_power, you can do something like:
Code:
draw_self(); // This will draw the "POWER" text
draw_text(x + 160, y, string(power) + "%"; // This draws the power percentage, but you will need to tweak the exact location this gets drawn to
draw_sprite(spr_power_consumption, 0, x + 160, y + 32); // This draws the first power consumption square to the screen
if (consumption > 0.01) { // If consumption is high enough to draw a second square
    draw_sprite(spr_power_consumption, 0, x + 160 + 32, y + 32);
}
if (consumption > 0.02) { // If consumption is high enough to draw a third square
    draw_sprite(spr_power_consumption, 0, x + 160 + 64, y + 32);
}
// And repeat this for all your squares
You will need to tweak some numbers in this code and it can be optimized a lot, but it may be a good starting point.

I hope this helps!
If you have any questions, don't hesitate to ask them!
i got some errors:
############################################################################################
ERROR in
action number 1
of Draw Event
for object oPower:

Trying to draw non-existing sprite.
at gml_Object_oPower_DrawEvent_1 (line 1) - draw_self(); // This will draw the "POWER" text
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_oPower_DrawEvent_1 (line 1)
_________________________________________
Unbalanced surface stack. You MUST use surface_reset_target() for each set.
 
A

ArcaPlaysYT

Guest
how would i round the percentage to the nearest whole number? i tried putting "round(string pwr)" but it returned an execution error.
 
A

ArcaPlaysYT

Guest
Round the number and then make it a string.
i tried doing that put nothing happened

GML:
round(pwr)
draw_self(); // This will draw the "POWER" text
draw_set_font(fntEmulogic)
draw_text(x + 300, y, string(pwr) + "%"); // This draws the power percentage, but you will need to tweak the exact location this gets drawn to
draw_sprite(sPower_Use1, 0, x + 300, y + 64); // This draws the first power consumption square to the screen
if (consumption > 0.005) { // If consumption is high enough to draw a second square
    draw_sprite(sPower_Use2, 0, x + 300 + 43, y + 64);
}
if (consumption > 0.01) { // If consumption is high enough to draw a third square
    draw_sprite(sPower_Use3, 0, x + 300 + 86, y + 64);
}
if (consumption > 0.015) {
    draw_sprite(sPower_Use4, 0, x + 300 + 129, y + 64);
}
if (consumption > 0.02) {
    draw_sprite(sPower_Use5, 0, x + 300 + 172, y + 64);
}
if (consumption > 0.025) {
    draw_sprite(sPower_Use6, 0, x + 300 + 215, y + 64);
}
 

Nidoking

Member
round(pwr)
This does nothing. It generates the rounded value and then... throws it away because you didn't do anything with it. You need to either use that value or store it in a variable. Try

Code:
draw_text(x + 300, y, round(string(pwr)) + "%");
or
Code:
var rounded_power = round(pwr)
and then using that variable instead of just pwr.
 
A

ArcaPlaysYT

Guest
This does nothing. It generates the rounded value and then... throws it away because you didn't do anything with it. You need to either use that value or store it in a variable. Try

Code:
draw_text(x + 300, y, round(string(pwr)) + "%");
or
Code:
var rounded_power = round(pwr)
and then using that variable instead of just pwr.
alright thanks that worked.
 

jo-thijs

Member
This does nothing. It generates the rounded value and then... throws it away because you didn't do anything with it. You need to either use that value or store it in a variable. Try

Code:
draw_text(x + 300, y, round(string(pwr)) + "%");
or
Code:
var rounded_power = round(pwr)
and then using that variable instead of just pwr.
In your first suggestion, you swapped string with round.
This:
GML:
draw_text(x + 300, y, round(string(pwr)) + "%");
should be this:
GML:
draw_text(x + 300, y, string(round(pwr)) + "%");
The function "round" expects a number, not a string.

alright thanks that worked.
Glad to hear you got it working!
 
A

ArcaPlaysYT

Guest
sup, I'm back. I've been doing other things on the game, but now I want to ask how to make a power outage?
 

Yal

šŸ§ *penguin noises*
GMC Elder
Code:
if(power <= 0){
  //do special power outage stuff
}
Power outage stuff includes:
  • Decrease a counter until a scripted death
  • Draw a dark overlay over the entire screen so it's clear the power ran out
Things like doors not working when power is zero might be easier to handle by checking if power is above zero instead, and only running their code if it is.
 
Top