Complications with GetSecondsPassed

Discussions and questions about the CS and mods for Morrowind
Post Reply
Tea.EarlGrey.Hot.
Layman
Layman
Posts: 5
Joined: Sat Apr 26, 2014 10:48 pm
ES Games: Arena, Daggerfall, Redguard, Battlespire, Morrowind, Oblivion, Skyrim
Platform: PC, Xbox, Xbox 360

Complications with GetSecondsPassed

Post by Tea.EarlGrey.Hot. »

I'm not new to scripting, but I can not for the life of me get this timer to work. I have tried it about a dozen different ways. Right now here's how I have it :

Code: Select all

begin EG_SpinWheel

short button
short messageOn
short state
float timer

set state to 0

if ( MenuMode == 1 )
	return
endif

set timer to ( timer + GetSecondsPassed )


if ( OnActivate == 1 )
        MessageBox, "Spin thread", "Marshmerrow", "Kresh", "Exit"
	set messageOn to 1
endif

if ( messageOn == 1 )
	set button to GetButtonPressed

	if ( button >= 0 )
		set messageOn to 0
	endif	

	if ( button == 0 ) 
		if ( player->GetItemCount "ingred_marshmerrow_01" >= 5 )
			player->RemoveItem "ingred_marshmerrow_01", 5
			player->AddItem "EG_MarshmerrowThread", 1
			MessageBox "You begin to spin thread from marshmerrow."
			PlaySound "Item Misc Up"
			set state to 1

		else 
			MessageBox "You don't have the resources."
		endif
	
	elseif ( button == 1  ) 
		 if ( player->GetItemCount "ingred_kresh_fiber_01" >= 5 )
			player->RemoveItem "ingred_kresh_fiber_01", 5
			player->AddItem "EG_KreshThread", 1
			MessageBox "You begin to spin thread from kresh fiber"
			PlaySound "Item Misc Up"
			set state to 1			

		else 
			MessageBox "You don't have the resources."
	endif

	else
		return

	endif
endif

if ( state == 1 )
	if ( timer > 10 )
		PlaySound "Item Misc Up"
		set state to 0
	endif
endif

end EG_SpinWheel
I created the base script and it worked perfectly just as expected. Then I decided I would try adding a delay before the player receives the spool. At first I had the timer logic inside the first button with the sound and additem in the timer check. It executed all the code except what came after the timer check (so it took the marshmerrow but never played sound and added spool to inv)

Since then I've tried shifting things around adding things solving it differently. I changed it to rely on the state variable a few tries ago... I checked the the other scripts with timers in game a million times, checked wiki documentation, googled around. None of it worked!

I'm starting to wonder if maybe the MessageBox or buttons create problems with GetSecondsPassed method? Or maybe I overlooked something and can't see it because I'm too tangled up. >.>
User avatar
Wolfborn
Apprentice
Apprentice
Posts: 158
Joined: Sat Oct 17, 2015 5:10 pm
ES Games: Morrowind, Oblivion, Skyrim (wip)
Platform: PC
Status: Subject to change without notice
UESPoints: 16

Re: Complications with GetSecondsPassed

Post by Wolfborn »

The problem is the "set state to 0" line at the top, right after the variable declarations. The timer code only executes when state = 1, but you're resetting state to 0 every frame, so the timer code only executes for one frame and then stops because state has been reset to 0. Take that line out and it should work.

Mind you, all this will do is take the player's ingredients, add the thread spool, and then play a sound 10 seconds later (and maybe not even that long--the way it's written the timer is incrementing while waiting for a button to be pressed). It doesn't delay the player getting the spool. Given what you've written, here's how I would do it (just off the top of my head--there might be a more elegant way to write this):
Hidden:

Code: Select all

begin EG_SpinWheel

short button
short messageOn
short state
short ingred  ; 1=marshmerrow, 2=kresh fiber
float timer

if ( MenuMode == 1 )
	return
endif

if ( OnActivate == 1 )
        MessageBox, "Spin thread", "Marshmerrow", "Kresh", "Exit"
	set messageOn to 1
endif

if ( messageOn == 1 )
	set ingred to 0
	set button to GetButtonPressed

	if ( button >= 0 )
		set messageOn to 0
	endif	

	if ( button == 0 ) 
		if ( player->GetItemCount "ingred_marshmerrow_01" >= 5 )
			set ingred to 1
			player->RemoveItem "ingred_marshmerrow_01", 5
			MessageBox "You begin to spin thread from marshmerrow."
			set state to 1

		else 
			MessageBox "You don't have the resources."
		endif
	
	elseif ( button == 1  ) 
		 if ( player->GetItemCount "ingred_kresh_fiber_01" >= 5 )
			set ingred to 2
			player->RemoveItem "ingred_kresh_fiber_01", 5
			MessageBox "You begin to spin thread from kresh fiber"
			set state to 1			

		else 
			MessageBox "You don't have the resources."
	endif

	else
		return

	endif
endif

if ( state == 1 )
	set timer to ( timer + GetSecondsPassed )
	if ( timer > 10 )
		if ( ingred == 1 )
			player->AddItem "EG_MarshmerrowThread", 1
		elseif ( ingred == 2 )
			player->AddItem "EG_KreshThread", 1
		endif
		PlaySound "Item Misc Up"
		set state to 0
		set timer to 0
	endif
endif

end EG_SpinWheel
I think that should work and do what you want it to do.

Edit: tightened up the timer code
CLIFF RACER BODY COUNT: 1059

And I ain't in it for the power
And I ain't in it for my health
I ain't in it for the glory of anything at all
And I sure ain't in it for the wealth
Tea.EarlGrey.Hot.
Layman
Layman
Posts: 5
Joined: Sat Apr 26, 2014 10:48 pm
ES Games: Arena, Daggerfall, Redguard, Battlespire, Morrowind, Oblivion, Skyrim
Platform: PC, Xbox, Xbox 360

Re: Complications with GetSecondsPassed

Post by Tea.EarlGrey.Hot. »

but you're resetting state to 0 every frame
Oh, thanks! I didn't know that it resets every frame, thought it was an initial assignment type thing!

and I know that it would only play the sound 10 seconds later, I had it the way I had wanted the first times I wrote it, but got tired of rearranging all that code just for it not to work, so I only did the sound as a test :P

EDIT:

I got too used to regular programming! Reading your above quote made everything click instantly. Here's the script now, working perfectly. Now just to come up with more things to make thread from... Thanks again :D

Code: Select all

begin EG_SpinWheel

short button
short messageOn
short state
float timer


if ( MenuMode == 1 )
	return
endif

if ( OnActivate == 1 )
	MessageBox, "Spin thread", "Marshmerrow", "Kresh", "Exit"
	set messageOn to 1
endif

if ( messageOn == 1 )
	set button to GetButtonPressed

	if ( button >= 0 )
		set messageOn to 0
	endif	

	if ( button == 0 ) 
		if ( player->GetItemCount "ingred_marshmerrow_01" >= 5 )
			player->RemoveItem "ingred_marshmerrow_01", 5
			MessageBox "You begin to spin thread from marshmerrow."
			DisablePlayerControls
			set state to 1

		else 
			MessageBox "You don't have the resources."
		endif
	
	elseif ( button == 1  ) 
		 if ( player->GetItemCount "ingred_kresh_fiber_01" >= 5 )
			player->RemoveItem "ingred_kresh_fiber_01", 5
			MessageBox "You begin to spin thread from kresh fiber"
			DisablePlayerControls
			set state to 1			

		else 
			MessageBox "You don't have the resources."
	endif

	else
		return

	endif
endif

if ( state == 1 )
	set timer to ( timer + GetSecondsPassed )
	
	if ( timer > 3 )
		if ( button == 0 )
			player->AddItem "EG_MarshmerrowThread", 1
		elseif ( button == 1 )
			player->AddItem "EG_KreshThread", 1
		endif

		PlaySound "Item Misc Up"
		EnablePlayerControls
		set state to 0
                set timer to 0
	endif
endif

end EG_SpinWheel
Post Reply