Hi everyone 🙂

Basically I'm new about scripting and coding and It's been several months that I've been struggling.
I'm trying to make a horror game and I want the character to have a flashlight that can be turned on and off and also uses a system of battery comsumption.
I did succeed to do the flashlight with the directional light and stuffs. But How can I make this system of turning on and off and batterry consumption ?

Even if you have no idea, could you explain to me the system of variables ( they are always "unexpected token" for me) and the system of scripting in general.

Ty 🙁

Took the liberty of making a small sample project you can check out in the zipfile. This is one way to go about it, I'm no master, so there might be smarter ways of going about it.

It evolves around the game tracking the global property "sys_batterytime".

The "startup" script puts the value to 100, shows the widget and give the player 20 batteries.

The batteries activate the script "sys_usebattery", when used it adds 250 to the sys_batterytime global property (which one could call a variable, because its actual number varies).

I changed the keybind "F" to activate the script "sys_turnonflashlight". Which does a lot of things. First it checks if the flashlight is on, and turns it off if it is, by checking the global boolean property IsFlashlightOn. Then checks if sys_batterytime is bigger than 0, and gives a "need more batteries" speech if not. Otherwise it attaches the flashlight, sets IsFlashlightOn to true and continuously drain 0.2 from sys_batterytime every 0.001 seconds in the "while loop" until sys_batterytime reaches zero. When reaching zero it simple detaches the flashlight and puts IsFlashlightOn to False

The only thing a bit complicated is how it "turns off" the flashlight if you push F before the battery runs out. Because the loop that drains the sys_batterytime doesn't stop until it see it has reached zero. So when turning the off the flashlight, it quickly saves the sys_batterytime in a variable, then changes it to zero, thus stopping the loop draining, then uses the variable to set the sys_batterytime back to what it was when you stopped using it.

(Which is why the widget flicker for one frame, when I quickly turn the flashlight on and off in the video, because the value is actually zero for a millisecond. I'm not smart enough yet to end the while loops without crashing the game otherwise.)

Good luck!

flashlight.zip
2MB

Just wow. Thank you so much for your time and answer !

With the sample project and your explanations I kind of understand more of all those stuffs.
Thanks again !

    Greetings, skumleren's system looks great!

    @Skumleren, to help with the loop issue you were explaining, something that worked for me:
    You can have the loop check if both IsFlashlightOn and the sys_batterytime variable at the same time, and this will also allow you to escape the while loop.
    Here's an example that I came up with just now, you can try it to see if it's more efficient or yields better results:

    while global.property["IsFlashlightOn"] == true and global.property["sys_batterytime"] > 0 do
    wait(0.2); //waits 0.2 seconds to avoid calling the loop every game tic or too frequently.
    if global.property["IsFlashlightOn"] == false then //first before subtracting time, lets see if the player disabled the flashlight during the wait.
    unequip_item(player, "right_hand"); //if he did, unequip the flashlight, then break out of the while loop.
    break;
    elseif global.property["sys_batterytime"] > 0 then //checks if the battery time is greater than 0, we don't want this to turn negative.
    global.property["sys_batterytime"] -= 0.2; //subtracts 0.2 from the battery time. Since we wait 0.2 before checking and subtracting, the value of the number in ["sys_batterytime"] is equivalent to seconds for balance purposes.
    else
    global.property["sys_batterytime"] = 0; //sets the time to zero, incase we got a negative number.
    global.property["IsFlashlightOn"] = false; //sets the flashlight to being off, since batterytime is zero, player should not be able to reactivate it.
    unequip_item(player, "right_hand");
    break;
    end;
    end;

    Bear in mind I typed this out just now, I did not run this in RPGIAB to check for errors or unintended side effects.

      MadManMoody

      Was my first thought to add second condition as well, but seeing as the loop note didn't have an add condition like the evaluate node, I didn't give it much though. Tried adding an evaluate condition in the loop, which promptly crashed game or made it lock up for several seconds, so that is why I did the save variable thing. Manually putting in the "and" and IsFlashlightOn in the loop node would make it work as initially intentioned and stop the flickering and stuff.

      Will quickly give it a go and update when home. Maybe also add proper stat functionality, I could see several people wanting to use such a system in their game.