GetButtonPressed Returning -1

Post » Thu Oct 13, 2016 12:32 pm

I'm having some strange problems with a chargen script that I've been modifying. In essence, the player chooses between three options for their starting area. If the player choose "random" then the script rolls the dice and activates a random region script while setting a global variable "CRELisRandom" to 1. The computer just randomly goes through the region script without any GetButtonPressed input. If the player chooses a specific region, then that specific script is activated and CRELisRandom stays at 0. Both the starting script and a region script are attached below.


When I select the "Random" option for starting area, it runs perfectly: it sets CRELisRandom to 1, runs that portion of CRELvvarden, and then places the player character in a random scenario with the HUD and everything activated.


When I select Vvardenfell for the starting area, CRELvvarden is activated and I proceed through the CRELisRandom == 0 portion until State 5. The problem arises here when I select either Caldera or Gnisis. Either option leads to the message "State 5 button -1" appearing indicating that the script thinks that I'm not selecting an option. After clicking either city, both scripts end and I'm stuck unable to move with no HUD (aka CRELoverallState doesn't get set to 1 in chargenew).


Additionally, selecting Random instead of Caldera or Gnisis ends up working perfectly as intended.


I hope this is an obvious error, but I'm struggling to see why GetButtonPressed is returning -1 in that portion of the script.


The starting script:


Spoiler

begin chargenew


short StartState

short State

short buttonLoc

short button

;short SkipPress


short RandStart

float WindowTimer ;Stops the windows from stacking (all coming up at the same time).


if ( WindowTimer < 1)

set WindowTimer to ( WindowTimer + GetSecondsPassed )

return

endif


if ( menumode == 1)

return

endif


if ( CRELoverallState == 0 )

...

elseif ( State == 60 )

if ( ScriptRunning, "CM_SpawnItem" == 0)

MessageBox "Where does your story begin?", "Random", "Vvardenfell", "Solstheim"

set State to 65

endif

return

elseif ( State == 65 )

set buttonLoc to GetButtonPressed

if ( buttonLoc == -1 )

return

elseif ( buttonLoc == 0 ) ;Random

set CRELisRandom to 1

set RandStart to ( Random, 2 )

if ( RandStart == 0 )

StartScript, CRELvvarden

elseif ( RandStart == 1 )

StartScript, CRELsolstheim

endif

elseif ( buttonLoc == 1 ) ;Vvardenfell

StartScript, CRELvvarden

elseif ( buttonLoc == 2 ) ;Solstheim

...

endif

return

elseif ( CRELoverallState == 1 ) ; activated region script sets CRELoverallState to 1 before ending


endif


end chargenew




The region script activated through the starting script


Spoiler

begin CRELvvarden


short OverallState

short State

short SkipPress ;Used only for Random beginning. Skips Getbuttonpress.

short RandStart1A

short RandStart1B

short RandStart2

short button


if ( menumode == 1 )

return

endif


if ( OverallState == 0 )

if ( CRELisRandom == 0 ) ; vvardenfell chosen in chargenew script

if ( State == 0 )

MessageBox "Under what circumstances does your story begin on Vvardenfell?", "Random", "Caldera", "Gnisis"

set State to 5

elseif ( State == 5 )

if ( SkipPress != 1 )

set button to GetButtonPressed

endif

if ( button == -1 )

MessageBox "State 5 button -1"

return

elseif ( button == 0 )

set RandStart1A to ( Random, 2 )

if ( RandStart1A == 0 )

set RandStart1B to ( ( Random, 2 ) + 1 )

set button to RandStart1B

elseif ( RandStart1A == 1 )

set RandStart1B to ( ( Random, 2 ) + 1 )

set button to RandStart1B

endif

elseif ( button == 1 )

...

set OverallState to 1

elseif ( button == 2 )

...

set OverallState to 1

endif

endif

elseif ( CRELisRandom == 1 ) ; random option chosen in chargenew script

set SkipPress to 1

set CRELisRandom to 0

set RandStart2 to ( Random, 2 )

if ( RandStart2 == 0 )

set State to 5

set button to ( ( Random, 2 ) + 1 )

elseif ( RandStart2 == 1 )

set State to 5

set button to ( ( Random, 2 ) + 1 ) ; same as above for debug purposes

endif

return

endif

elseif ( OverallState == 1 )

StopScript, CRELvvarden

endif


end CRELvvarden


User avatar
Avril Louise
 
Posts: 3408
Joined: Thu Jun 15, 2006 10:37 pm

Post » Thu Oct 13, 2016 6:27 pm

Dunno if this is the problem, but my 2 septims:
always reset state/timers as soon as possible, you will less likely forget to do it e.g.
if ( timer < 2 )
set timer to ( timer + GetSecondsPassed )
return
endif
set timer to 0 ; as soon as possible to not forget it!

; ...
if ( state == blah )
set button to GetButtonPressed
if ( button == -1 )
return
endif
set state to somethingelse ; as soon as possible to not forget it!
if ( button == blah )
; ....
User avatar
Trista Jim
 
Posts: 3308
Joined: Sat Aug 25, 2007 10:39 pm

Post » Thu Oct 13, 2016 10:57 pm

Abot, wouldn't the script skip over "if ( button == blah )" in that setup? It appears that it would process setting the button to GetButtonPressed, would check for -1, and then switch the state regardless of what happened with -1.



Edit: Whoops, the state change wouldn't take effect until the return.

User avatar
Shannon Lockwood
 
Posts: 3373
Joined: Wed Aug 08, 2007 12:38 pm

Post » Thu Oct 13, 2016 2:09 pm

uhm, no, another example

short state
short b1
short b2

if ( state == 0 )
set state to 1
MessageBox "Press a button" "1" "2"
return
elseif ( state == 1 )
set button to GetButtonPressed
if ( button == -1 )
return ; return until a button pressed
endif
set state to 2 ; a button was pressed, ensure to exit code block regardless of what button
if ( button == 0 )
set b1 to ( b1 + 1 ) ; process button 1
else
set b2 to ( b2 + 1 ) ; process button 2
endif
return
elseif ( state == 2 )
set state to 0 ; reset state, ensure to exit code block
MessageBox "button 1 pressed %g times, button 2 pressed %g times" b1 b2 ; give information
endif
User avatar
Killer McCracken
 
Posts: 3456
Joined: Wed Feb 14, 2007 9:57 pm

Post » Thu Oct 13, 2016 2:20 pm

So it's better to "if->endif, if->endif" for the "button == -1" portion rather than "if->elseif"?

User avatar
Sista Sila
 
Posts: 3381
Joined: Fri Mar 30, 2007 12:25 pm

Post » Thu Oct 13, 2016 7:04 pm

Greatness7 solved the problem by adding some StopScript and StartScripts to the scripts. It essentially boiled down to the game having an issue with two scripts running at the same time + two instances of GetButtonPressed.

User avatar
Abi Emily
 
Posts: 3435
Joined: Wed Aug 09, 2006 7:59 am

Post » Thu Oct 13, 2016 1:39 pm

IMO yes. You reset the variable once and as soon as possible, instead of setting it inside each sub-block of next if structure.
I think your posted script part is missing resetting the state variable, you keep repeating the if ( State == 65 ) block if you don't change the state variable value
User avatar
Dark Mogul
 
Posts: 3438
Joined: Tue Feb 20, 2007 11:51 am


Return to III - Morrowind