Jump to content

Advanced Scripting Help


GeneralGred

Recommended Posts

I'm currently making a player home mod with a emphases on survival and one of these aspects is the use of a generator to power the home. I do not consider myself a master in the scripting department but I am quite adept at it, I have used my knowledge to make a script that allows the generator to have dynamic real-time fuel and it works fundamentally. I am however unable to keep it working through any form of time transition (ex: Sleep/Wait/Fast Travel). I made a separate system in the script to handle these transitions but it works rarely and usually only once in a single play session, it usually simply ignores the transition as if it never happened and so fuel is not consumed properly. Despite my best efforts I cannot seem to work this out, I'm using both NVSE and JIP Plugin and would appreciate it if anyone knows a way to solve this issue.

 

Current version of my script below:

 

short FuelConsumption
int FuelLevel
short PowerTier
float Timer
float GeneratorRuntime

 

if (GMGeneratorPanelTier1Ref.GetDisabled == 0)
if (GMGeneratorPanelTier1Ref.Running == 1)
set PowerTier to 1
if (FuelLevel >= 1) && (FuelConsumption != 1)
Set GeneratorRuntime to GameHour
Set FuelConsumption to 1
elseif (FuelLevel <= 0)
set GMGeneratorPanelTier1Ref.Running to 0
endif
elseif (GMGeneratorPanelTier1Ref.Running == 0)
set PowerTier to 0
set FuelConsumption to 0
endif
endif

if (GMGeneratorPanelTier2Ref.GetDisabled == 0)
if (GMGeneratorPanelTier2Ref.Running == 1)
set PowerTier to 2
if (FuelLevel >= 1) && (FuelConsumption != 1)
Set GeneratorRuntime to GameHour
Set FuelConsumption to 1
elseif (FuelLevel <= 0)
set GMGeneratorPanelTier1Ref.Running to 0
endif
elseif (GMGeneratorPanelTier1Ref.Running == 0)
set PowerTier to 0
set FuelConsumption to 0
endif
endif


if (FuelConsumption == 1)
if (GameHour - GeneratorRuntime >= 0.01666667)
set FuelLevel to (FuelLevel - 1)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 1) && (GameHour - GeneratorRuntime < 2)
set FuelLevel to (FuelLevel - 60)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 2) && (GameHour - GeneratorRuntime < 3)
set FuelLevel to (FuelLevel - 120)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 3) && (GameHour - GeneratorRuntime < 4)
set FuelLevel to (FuelLevel - 180)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 4) && (GameHour - GeneratorRuntime < 5)
set FuelLevel to (FuelLevel - 240)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 5) && (GameHour - GeneratorRuntime < 6)
set FuelLevel to (FuelLevel - 300)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 6) && (GameHour - GeneratorRuntime < 7)
set FuelLevel to (FuelLevel - 360)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 7) && (GameHour - GeneratorRuntime < :cool:
set FuelLevel to (FuelLevel - 420)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= :cool: && (GameHour - GeneratorRuntime < 9)
set FuelLevel to (FuelLevel - 480)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 9) && (GameHour - GeneratorRuntime < 10)
set FuelLevel to (FuelLevel - 540)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 10) && (GameHour - GeneratorRuntime < 11)
set FuelLevel to (FuelLevel - 600)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 11) && (GameHour - GeneratorRuntime < 12)
set FuelLevel to (FuelLevel - 660)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 12) && (GameHour - GeneratorRuntime < 13)
set FuelLevel to (FuelLevel - 720)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 13) && (GameHour - GeneratorRuntime < 14)
set FuelLevel to (FuelLevel - 780)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 14) && (GameHour - GeneratorRuntime < 15)
set FuelLevel to (FuelLevel - 840)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 15) && (GameHour - GeneratorRuntime < 16)
set FuelLevel to (FuelLevel - 900)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 16) && (GameHour - GeneratorRuntime < 17)
set FuelLevel to (FuelLevel - 960)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 17) && (GameHour - GeneratorRuntime < 18)
set FuelLevel to (FuelLevel - 1020)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 18) && (GameHour - GeneratorRuntime < 19)
set FuelLevel to (FuelLevel - 1080)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 19) && (GameHour - GeneratorRuntime < 20)
set FuelLevel to (FuelLevel - 1140)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 20) && (GameHour - GeneratorRuntime < 21)
set FuelLevel to (FuelLevel - 1200)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 21) && (GameHour - GeneratorRuntime < 22)
set FuelLevel to (FuelLevel - 1260)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 22) && (GameHour - GeneratorRuntime < 23)
set FuelLevel to (FuelLevel - 1320)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 23) && (GameHour - GeneratorRuntime < 24)
set FuelLevel to (FuelLevel - 1380)
set FuelConsumption to 0
endif
if (GameHour - GeneratorRuntime >= 24)
set FuelLevel to (FuelLevel - 1440)
set FuelConsumption to 0
endif
endif

Edited by General Gred
Link to comment
Share on other sites

I will try to have a proper look at this later because, quite frankly, the concept interests me. I did however, notice a few things that may (or may not) be causing you problems:

 

First, I'll assume your actual script runs in a gamemode block.

 

Second, you probably shouldn't have it do math at the top of your if statement:

 

if (GameHour - GeneratorRuntime >= 22)

 

Should probably be something like:

 

If ( gamehour > generatorRuntime )

Set newvariable to ( GameHour - GeneratorRuntime )

Endif

 

If newvariable > 0

Set anothernewvariable to ( newvariable * 60 )

Set Fuellevel to ( fuellevel - anothernewvariable )

Set fuelconsumption to 0

Endif

 

And that should also make you only need one if statement to subtract fuel instead of 24.

Edited by uhmattbravo
Link to comment
Share on other sites

Thank you for your input, sorry I forgot but yes it is a Gamemode block running out of a quest script. I made the modifications you recommended to the script while assuming your new vars are Short, but unfortunately to no success. The replacements to my script for handling the hours still broke if waiting for more then a couple hours, this happens with my script too, it works fine if only waiting 1, 2, sometimes 3 hours at a time, but will brake if I wait more then that. Also your script suggestions doubled the amount of fuel consumption. I would like to clarify as well that the reason I have a separate piece script for handling fuel on a in-game minute basis is to prevent the player from turning off the generator and restarting the script to prevent the fuel consumption. So no matter what I do I must maintain a script that handles the consumption on a in-game minute basis. Also I'd like to know more about your use of the * in your function, I haven't encountered that before.

Link to comment
Share on other sites

It may be the wrong star. I typed it from my phone. It's multiplication.

 

That's odd that it doubled. I was assuming you were going for a rate of one unit per game minute (60 an hour)... I could have typed something wrong.

 

Hopefully I'll get the chance to experiment a little with it later on tonight and let you know if I can get it to work because, like I said, I really like the concept.

Link to comment
Share on other sites

Ya definitely need the per minute basis, but as far as I can tell the script simply doesn't acknowledge the passage of time during Wait/Sleep/ETC. Whats frustrating is that its nearly perfect as any tampering I do usually breaks it outright. This is I believe the 3rd or 4th version of this script and by far works the best. I don't think it will be much help but as you might have guessed the FuelLevel variable is set externally from another script.

Link to comment
Share on other sites

Thanks for your efforts, if you need anything to help just ask, I've taken a small break from the script to work on other systems and parts of the mod while I try to figure it out as well. It would be nice to reverse engineer the hunger/thirst system but it seems hard coded into the engine. I thought about maybe using Global variable but haven't got around to it.

Link to comment
Share on other sites

I don't see much harm in trying a global, but personally, I've been avoiding using them since quest variables became a thing. If it doesn't offend you, I may try rewriting it from scratch. I just tend to get better results that way sometimes when a script is being this picky.

 

Another thing though, am I correct in assuming your generator is upgradeable? Or is it two running off the same fuel tank?

Link to comment
Share on other sites

Disclaimer: I haven't fully studied the script in the OP because it seems needlessly complex for the task. I'm with mattbravo on a re-implementation, and I have a suggestion for how to go about it.

But as an aside, (one of the) reason(s) you're having problems could be due to the way GameHour "flips" when it crosses into a new day. It's 23.9 right before midnight and then back to 0 afterwards. Needless to say, this can cause unexpected behaviour if you don't account for it. I find that using GameDaysPassed for tracking considerably simplifies things as it only ever grows in value.

Back to the suggestion. Having a variable constantly incremented (or decremented) as an effect of time passing doesn't take much at all. Below is the core variable control from a discarded mod project of mine that would've added primary needs to FO3. The comments take up almost as much space as the code:

short initialized
float last_tick
float time_passed
float current_thirst
float current_hunger
float current_sleep
float old_thirst
float old_hunger
float old_sleep

begin gameMode

    if initialized != 1
        set initialized to 1

        ; The last tick represents the last time the script executed.
        set last_tick to GameDaysPassed
    else

        ; Old values can be compared to current values in-between ticks
        ; to check if value is growing or falling and by how much.
        set old_thirst to current_thirst
        set old_hunger to current_hunger
        set old_sleep to current_sleep

        ; Compute how much time has passed as a fraction of a day.
        set time_passed to GameDaysPassed - last_tick

        ; Multiply that fraction by thirst/hunger/sleep rates and add
        ; to current value. The rates are saved as global variables.
        set current_thirst to current_thirst + time_passed * ThirstRate
        set current_hunger to current_hunger + time_passed * HungerRate
        set current_sleep to current_sleep + time_passed * SleepRate

        ; Last tick happened right now.
        set last_tick to GameDaysPassed
    endif

end

I've cut out the feature specific stuff, like stages with negative effects, UI stuff, sounds, etc. and this is what I was left with. Feel free to adapt this to your generator/fuel use case, or any other stuff you wish to use it for. The increment is constant and it'll account for any transition where time passes.

I had this running in a quest with a script delay of somewhere below 0.5.

Edited by Ladez
Link to comment
Share on other sites

@uhmattbravo

Ya I figured a Global would be redundant, and yes it is upgradable. There are 3 tiers of generators, the last one isn't in the script yet, and they do use the same fuellevel variable. During the upgrade process the "fuel tank" is emptied and to simulate more efficient fuel the adding of each fuel canister simply adds more to the fuellevel variable.

 

@Ladez

I will definitely give this a shot tomorrow as for now I have to go to work, I think I can safely assume which variable types each of your variables are, but if you get a chance maybe add in which ones are which (short/int/float/etc).

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...