Jump to content

Script Review


Recommended Posts

Posted (edited)

Edit: The final version of the script is here:

Spoiler
Scriptname modRunningFatiguePlayerScript extends ReferenceAlias  

Actor PlayerRef
Float currentCarryWeight
Int runningCounter

Function StartFatigue()
  currentCarryWeight = PlayerRef.GetActorValue("CarryWeight")
  PlayerRef.DamageActorValue("CarryWeight", currentCarryWeight)
  GotoState("Fatigued")
EndFunction

Function StopFatigue()
  PlayerRef.RestoreActorValue("CarryWeight", currentCarryWeight)
  runningCounter = 0
  GotoState("Counting")
EndFunction

Function StartWarningFatigue()
  currentCarryWeight = PlayerRef.GetActorValue("CarryWeight")
  PlayerRef.DamageActorValue("CarryWeight", currentCarryWeight)
  Utility.Wait(4)
  PlayerRef.RestoreActorValue("CarryWeight", currentCarryWeight)
EndFunction

Event OnInit()
  PlayerRef = Game.GetPlayer()
  runningCounter = 0
  GotoState("Counting")
  RegisterForSleep()
  RegisterForSingleUpdate(3.0)
EndEvent

State Counting
Event OnBeginState()
  RegisterForSingleUpdate(3.0)
EndEvent

Event OnUpdate()
  if PlayerRef.IsRunning() && !PlayerRef.IsInCombat() && !PlayerRef.IsSneaking()
    runningCounter+=5
  endif

  if runningCounter == 45
    runningCounter = 50
    StartWarningFatigue()
  endif
  
  if runningCounter >= 60
    if !PlayerRef.IsSprinting()
      StartFatigue()
    endif
  else 
    RegisterForSingleUpdate(5.0)    
  endif
EndEvent

Event OnSleepStop(bool abInterrupted)
  StopFatigue()
EndEvent
EndState

State Fatigued
Event OnBeginState()
  RegisterForSingleUpdate(180.0)
EndEvent

Event OnUpdate()
  StopFatigue()
EndEvent

Event OnSleepStop(bool abInterrupted)
  StopFatigue()
EndEvent
EndState

 

I am designing a mod where the player gets tired after running for a certain period of time and can only walk for a certain period of time. I would be very grateful if you could review the script I have written below. I am particularly interested in whether I am using the OnUpdate, RegisterForSingleUpdate and RegisterForSleep commands correctly, which I have read many times that they should be used carefully.

Spoiler
Scriptname PlayerScript extends ReferenceAlias  

int counter
float currentCarryWeight

Event OnInit()
 RegisterForSingleUpdate(1.0)
 RegisterForSleep()
 GotoState("Counting")
EndEvent

State Counting
 Event OnUpdate()
	bool bKeepUpdating = true
	if  bKeepUpdating
		RegisterForSingleUpdate(1.0)
	endif

	if Game.GetPlayer().IsRunning()
		if !Game.GetPlayer().IsInCombat()
			if !Game.GetPlayer().IsSneaking()
				counter = counter+1
			endif
		endif
	endif
	
	if counter == 60
		UnregisterForUpdate()
		counter = 0
		GotoState("Fatigue")
	endif
 EndEvent

 Event OnSleepStop(bool abInterrupted)
	counter = 0
 EndEvent
EndState

State Fatigue
 Event OnBeginState()
	currentCarryWeight = Game.GetPlayer().GetAV("CarryWeight")
	Game.GetPlayer().DamageAV("CarryWeight", -currentCarryWeight)
	Utility.WaitGameTime(1)
	GotoState("Counting")
 EndEvent

 Event OnEndState()
	Game.GetPlayer().RestoreAV("CarryWeight", currentCarryWeight)
	RegisterForSingleUpdate(1.0)
 EndEvent
EndState

;Game.GetPlayer().DamageAV("CarryWeight", -currentCarryWeight) prevents the player from running.

Edited by vn524135
Link to comment
Share on other sites

	bool bKeepUpdating = true
	if  bKeepUpdating
		RegisterForSingleUpdate(1.0)
	endif

what the point of this? it is always true, your another dude over thinking it  and getting wrong, registering for another update is last and OFC will depend on the result of the function

	if Game.GetPlayer().IsRunning()
		if !Game.GetPlayer().IsInCombat()
			if !Game.GetPlayer().IsSneaking()
				counter = counter+1
			endif
		endif
	endif

I am an anti nester, you have no idea how much that insult me, btw stop using tab indentation, use spaces and please make me happy set it to two space indentation

How nice is this with Spaces Not Tabs and set to two space indentation and NOT nesting any thing???? Let make code pretty to look at, and not offensive to the eyes 😁😁😁👍🏽👍🏽👍🏽🤣🤣🤣🤣

Event OnUpdate()

  if Game.GetPlayer().IsRunning() && !Game.GetPlayer().IsInCombat() && !Game.GetPlayer().IsSneaking()
    counter+=1
  endif

  if counter == 60
    counter=0
    GotoState("Fatigue")
  Else 
    RegisterForSingleUpdate(1.0)
  endif
EndEvent

your logic needs needs work padawan, has for the other calls to update, I am not sure, but your logic gives me a bad feeling.. but not bad for a newbie

Seriously use Spaces not Tabs and set it to your preferred space indentation, but I use 2 a lot to keep it neat tidy and concise..... and other will wonder why your code looks good when you post it.. SPACES not TABS (he whispered) 😉😉😉

Link to comment
Share on other sites

Sometimes PeterMartyr is a bit of a code puritan, wanting to impose his own customs and ethics on others.  Indentation is important; it's part of keeping the code readable and maintainable, but it's the objective that's important and you're doing fine.

His comment on bKeepUpdating should be attended.  If you had a use for it in mind, your code is not doing that.

More important, and this you really MUST address, is the placement of RegisterForSingleUpdate inside the OnUpdate event.  It needs to be one of the very last things in the event function... otherwise you risk having multiple copies of that event code running concurrently which may cause all sorts of difficult to trace or understand logic flows.

You may think that the 1.0 second delay is more than adequate to finish the 1st OnUpdate call before the second kicks in.  That may be right most of the time, but once in a while the 1st instance will be delayed (for a variety of reasons, including a critter spawn event that launches 80 script events at once, which trigger a suspended stack event, which holds your script back 10 seconds, or more).  These problems are hell to debug because they will be rare, random and virtually impossible to replicate.

Don't take chances, properly chain your RegisterForSingleUpdate calls from tail to head.

Link to comment
Share on other sites

@xkkmEl  heheheheh not really those are not my custom ethics)) and the proof is in posting code on the nexus, what you do in the privacy of your own home is no one concern, but once your public or sharing code it is your responsibility to make sure your code readable by others..  if  you work for Google Apple or Microsoft Amazon etc, it  is there way or the highway... I just gave some hints at how to beautify code very importantly make it work on site like the nexus.... I bet also a lot users wonder why their code looks it does on the nexus, and I just told them  why.... they using tab not spaces.. spaces keep it uniform, tab are unpredictable....   your code will not look it does to you with Tabs on another person PC...  Spaces do.

EDIT at uni I was rebel and when told I had to stop using tab, I only stop under the threat they would they fail me, and it years before I realise the error of my ways...  just saying.. so I am aware heaps will disagree with me, cos I was one myself.

Link to comment
Share on other sites

2 hours ago, PeterMartyr said:

EDIT at uni I was rebel and when told I had to stop using tab, I only stop under the threat they would they fail me, and it years before I realise the error of my ways...  just saying.. so I am aware heaps will disagree with me, cos I was one myself.

 

I guess the papyrus coders at Bethesda didn't go to your uni then, cause every vanilla papyrus source script I've seen uses tab to indent. Feel free to go and check for yourself.

Link to comment
Share on other sites

In a coding team I was leading 20+ years ago, we had indentation wars with team members re-indenting other people's code and polluting the source control system with useless diffs making actual changes impossible to locate in the version histories.

I put an end to it by forcing a system re-indent as part of the commit operation, and a per-programmer configurable indent on checkout.

Link to comment
Share on other sites

I said spaces not tab with your preferred indentation, do you two guys think tabs are indentations, 🤣🤣🤣🤣🤣🤣🤣🤣🤣 and spaces cannot have indentation   

EDIT it is Tab or Spaces with your preferred indentation.. what are you guys talking about, explain to me like talking to a baby until I get it 

Edit #2 if you look at my code you will see I am using spaces with indentation, I just use a 2 indent instead of a four indent ... @xkkmEl I stand by I am anti nester.. but I never told anyone not to use indentation, I clearly said spaces with your preferred indentation... 

Never thought explaining why  code posted on the Nexus looks like it does or how to fix it would lead to this

@xkkmEl  @dylbill  sorry guys get a life you both need one 😁😁😁😁😁😂😆🙏🏽

Link to comment
Share on other sites

1 hour ago, PeterMartyr said:

 

@xkkmEl  @dylbill  sorry guys get a life you both need one 😁😁😁😁😁😂😆🙏🏽

Look in the mirror my guy, you’re one of the most arrogant people I’ve ever come across. Every Bethesda source script i’ve seen uses 4 space (tab) indentations, but sure tell the op he’s “wrong” for doing the exact same thing. 🤣🤡

Link to comment
Share on other sites

my comment was generic, If you post code for the Witcher, any game the nexus hosts, and you find that it does not look like the code you wrote, ? it is because your using tabs not spaces, say this is false any one I dare you?

at @dylbill are you still sore I wrote code you been working on for what days weeks months maybe in few hours and where you used 200 lines of code to create 4 objects I created over 50 objects with 70 lines of code?, it show your still bitter.. 😂😂😂😂 you had to come here uninvited to have a shot at me for stating a simple truth, cos no you look down on could write code 100 fold better than you 😅😅😅😅 that eats you up does it not?

Edit now I am taking the PISS out of you... see the difference 

Edit @dylbill these forum are like an audit trial,  it clear as day I never speak to you until you have cheap shot at me.. I am gonna repeat get a f*#@ing life, and at any nexus mod check to see what said is true ))

Link to comment
Share on other sites

Thank you for your comments. I'm using translation because my English is not good. Please forgive me if I miss anything you say. If I understand correctly, what you said is that the bKeepUpdating is unnecessary because it is always true. I didn't notice this detail because I'm new to coding. I replaced my code with the code you suggested and it works without problems.

Thank you for your warnings about using RegisterForSingleUpdate, and other information and suggestions.

The final version of the script is as follows;

Scriptname PlayerScript extends ReferenceAlias  

int counter
float currentCarryWeight

Event OnInit()
  RegisterForSingleUpdate(1.0)
  RegisterForSleep()
  GotoState("Counting")
EndEvent

State Counting
Event OnUpdate()
  if Game.GetPlayer().IsRunning() && !Game.GetPlayer().IsInCombat() && !Game.GetPlayer().IsSneaking()
    counter+=1
  endif

  if counter == 60
    counter=0
    GotoState("Fatigue")
  Else 
    RegisterForSingleUpdate(1.0)
  endif
EndEvent

Event OnSleepStop(bool abInterrupted)
  counter = 0
EndEvent
EndState

State Fatigue
Event OnBeginState()
  currentCarryWeight = Game.GetPlayer().GetAV("CarryWeight")
  Game.GetPlayer().DamageAV("CarryWeight", -currentCarryWeight)
  Utility.WaitGameTime(1)
  GotoState("Counting")
EndEvent

Event OnEndState()
  Game.GetPlayer().RestoreAV("CarryWeight", currentCarryWeight)
  RegisterForSingleUpdate(1.0)
EndEvent
EndState

Here's how the code works in my mind: RegisterForSingleUpdate runs until the player completes the total running time of 60 seconds. When the counter reaches 60, the player enters a fatigue state, and RegisterForSingleUpdate doesn't run until the end of this state.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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