Menu Selection Problem

Make games! Discuss those games here.

Moderators: Bob the Hamster, marionline, SDHawk

Post Reply
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

Menu Selection Problem

Post by marionline »

Hello,
I need some scripting help with menus again.

What should happpen:
you choose an option from the menu and it is executed.

What does happen:
The hero imediatly turns right. The running option is always executed, regardless of what is choosen from the menu.

I don't know if I'm either using the menu-handle things worng, or if there's a mistake in the use of the if-clauses.

Thanks for your help in advence! :)

This is the (rather long) script:
[script]
include, Battlesystem.hsi
include, plotscr.hsd

global variable (0, ActiveHero)
#the global var goes from 1 to 4 to loop throug
#the positions of the active fighter

plotscript, Battle, begin

# This is the Instead-of-Battle script named Battle.txt
suspend caterpillar
suspend player

fade screen out
wait
# prepare for going back
variable (LastX)
LastX := hero X (me)
variable (LastY)
LastY := hero Y (me)
variable (LastMap)
LastMap := current map #remembers where the heros came from

teleport to map (2)
wait

# Setting things up

variable (Pos1) #HeroAtPosition1
variable (Pos2)
variable (Pos3)
variable (Pos4)


# Me
#Pos1 := hero by slot (0)
# Here X ans Y for hero:
set hero position (me, 14, 2)
wait for hero (me)
#set hero picture (me, 0, outside battle)
#Wait for hero (me)

set hero direction (me, west)
Wait for hero (me)
wait (8)

# Hero 2
#Pos1 := hero by slot (0)
set hero position (1, 14, 3)
wait for hero (1)
#set hero picture (1, 0, outside battle)
#Wait for hero (1)

set hero direction (1, west)
Wait for hero (1)
wait (8)

# Hero 3
#Pos1 := hero by slot (0)
set hero position (2, 14, 4)
wait for hero (2)
#set hero picture (2, 0, outside battle)
#Wait for hero (2)

set hero direction (2, west)
Wait for hero (2)
wait (8)

# Hero 4
#Pos1 := hero by slot (0)
set hero position (3, 14, 5)
wait for hero (3)
wait for hero (3)
#set hero picture (3, 0, outside battle)
#Wait for hero (3)

set hero direction (3, west)
Wait for hero (3)
wait (8)

put camera (10,3)
wait for camera

# start the Battle
fade screen in
wait



###############################
# Do Stuff here, as long as
# Enemie and you have HP
###############################

variable (id) # wich hero is active?
id := hero by slot(ActiveHero)

# variables for attacks, skills ect. Not in use
variable (Attk1)
variable (Attk2)
variable (Attk3)
variable (Attk4)

# showing the HP (replace later by bar, add MP bar as well)
variable (ActiveHeroHP)
ActiveHeroHP := get hero stat (ActiveHero,0,current stat)
# Show Hero's HP
show value (ActiveHeroHP)

# Menu wich shows the hero's Attacks
variable (HeroM) # Menu variable handle for certain Hero

if (id == 0) then ( HeroM := 1) # Hero 0 (Name) gets Menu 1
else if (id == 1) then ( HeroM := 2)
else if (id == 2) then ( HeroM := 3)

variable (BattleM)
SetVariable (BattleM, OpenMenu (HeroM,0))

# Menu item handle variables depend of the open Menu,
# so actually it depends on the active hero
variable (Choice1)
variable (Choice2)
variable (Waiting)
variable (Running)

Choice1 := first menu item(BattleM)
Choice2 := next menu item(Choice1)
#Choice3 := next menu item(Choice2)
#Choice4 := next menu item(Choice3)
Waiting := next menu item(Choice2) #change Choice3
Running := next menu item(Waiting)


variable (DoDamage) # Damage amount an attack deals
variable (DoMPCost) # MP amount an attack costs

#Special = get menu item extra(Choice1, extra 2)
#Ideas:
# 0 = target one;
#1= target all;
#2= usable from above;
#3= enviroment effect;


if (selected menu item (BattleM) == Choice1) then
(
DoDamage := get menu item extra(Choice1, extra 0)
DoMPCost := get menu item extra(Choice1, extra 1)

walk hero (ActiveHero, 1, west)
wait for hero

# Number depends on Hero!
set hero picture (ActiveHero,4,outside battle)
wait for hero
set hero direction (ActiveHero, north)
wait for hero
set hero direction (ActiveHero, east)
wait for hero
set hero direction (ActiveHero, south)
wait for hero
set hero direction (ActiveHero, west)
wait for hero


#close menu(BattleM)
)

else if (selected menu item (BattleM) == Choice2) then
(
DoDamage := get menu item extra(Choice2, extra 0)
DoMPCost := get menu item extra(Choice2, extra 1)

walk hero (ActiveHero, 1, west)
wait for hero

# Number depends on Hero!
set hero picture (ActiveHero,5,outside battle)
wait for hero
set hero direction (ActiveHero, north)
wait for hero
set hero direction (ActiveHero, east)
wait for hero
set hero direction (ActiveHero, south)
wait for hero
set hero direction (ActiveHero, west)
wait for hero

# Number depends on Hero!
set hero picture (ActiveHero,0,outside battle)
wait for hero

walk hero (ActiveHero, 1, east)
wait for hero

set hero direction(ActiveHero, west)
wait for hero

#close menu(BattleM)
)

else if (selected menu item (BattleM) == Waiting) then
(
# Number depends on Hero!
set hero picture (ActiveHero,8,outside battle)
wait for hero
set hero direction (ActiveHero, north)
wait for hero
set hero direction (ActiveHero, east)
wait for hero
set hero direction (ActiveHero, south)
wait for hero
set hero direction (ActiveHero, west)
wait for hero

)
else if (selected menu item (BattleM) == Running) then
(
walk hero (ActiveHero, east ,2)
wait for hero
)


else ()


##################################
# End of Battle, you've won:
# Enemie Hp are 0 or below
#################################

wait(16)

#Reset the scene
fade screen out
wait

camera follows hero (me)
wait for camera

# Change the hero pictures back to
# map appropriate ones

# If you rearrange the party,
# the party slot numbers do not change.

set hero picture (me,0,outsidebattle)

if (hero by slot(1) == 1) then (
reset hero picture (1,outsidebattle)
)

if (hero by slot(2) == 2) then (
reset hero picture (1,outsidebattle)
)

if (hero by slot(3) == 3) then (
reset hero picture (1,outsidebattle)
)

# go back to last map
teleport to map (lastMap)
set hero position (me, LastX,LastY)
Wait for hero (me)

fade screen in
wait

resume player
resume caterpillar

###################################
# End of Battle, you lost
# Your HP are 0 -> Game Over Script
####################################

end
[/script]
User avatar
RMSephy
Metal Slime
Posts: 356
Joined: Mon Dec 21, 2009 5:56 pm

Post by RMSephy »

I think the problem is that you're not waiting for the player to make any choices. selectedMenuItem just gives you the menu item that is currently selected; it doesn't wait for the player to press enter or space.

You could try adding a waitForKey(useKey) before the if clauses.
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

Post by marionline »

Thanks RMSephy!
I added the wait for key (usekey) in front of the if-clauses block.

The first time, nothing happed. I was suck in battle after the menu closed.
The second time, I made sure to use enter when choosing an option and I got an error:
Script Error!
selectedmenuitem: invalid menu handle: -1
Call chain (current script last): battle
User avatar
RMSephy
Metal Slime
Posts: 356
Joined: Mon Dec 21, 2009 5:56 pm

Post by RMSephy »

For the second error, could you check if the "Close menu if selected" is set on each of your menu items? It sounds like the menu is closing right after you press the Enter key, so by the time the script reaches the if clauses it doesn't know which menu item you're looking at.
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

Post by marionline »

That's right. I'm closing the menu after the choosing an item.
So, I'd have to manually close the menu in the script?
User avatar
RMSephy
Metal Slime
Posts: 356
Joined: Mon Dec 21, 2009 5:56 pm

Post by RMSephy »

Yep. You already have the close menu commands commented out in the if branches, so you can just put those back in.
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

Post by marionline »

The error is gone :).
Still, the game is stuck after the mrnu disappears, also the hero does not perform the action for the attack.

Edit: Propably I forgot to turn off the close menu thing in the menu :-/
I'll check.

So, had forgotten to set off the automatic trun off, I enabled the bitset "allow gameplay and scripts", I am not stuck in the battle anymore. And now we're back to the beginning, where attack 4 (running away) is choosen and the battle ends.

I'm not sure if that is good or not. :???:
Last edited by marionline on Sat Jan 24, 2015 5:55 pm, edited 3 times in total.
User avatar
RMSephy
Metal Slime
Posts: 356
Joined: Mon Dec 21, 2009 5:56 pm

Post by RMSephy »

Oh! Do you have a textbox right before bringing up the battle menu? One possibility is that a player would press Enter/Space to close the textbox before the menu, and the game thinks that keypress was used to make a choice on the menu too. Try adding something like this before the waitForKeypress and see if that changes anything.

Code: Select all

while(keyispressed(57),or,keyispressed(28),or,keyispressed(29)) do(wait(1))
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

Post by marionline »

I have no textbox before the battle. I start the battle via NPC.
And the menu does show up, just the

Code: Select all

 if (selected menu item (BattleM) == Choice1) then 
etc. does not seem work.
Also i could just add a longer wait before the battle or maybe some animation with sprites, so the menu takes longer to show up and the player does not immediatly close the menu.

Maybe...is there a wait for menu function I should add somewhere?

Edit: I didn't include the wait yet, but I saw that the 1st and 2nd choice are nor preformed. The last two - waiting and running - do work. :)
Last edited by marionline on Sat Jan 24, 2015 6:59 pm, edited 2 times in total.
User avatar
RMSephy
Metal Slime
Posts: 356
Joined: Mon Dec 21, 2009 5:56 pm

Post by RMSephy »

Hmm... I'm kind of out of ideas here. What exactly is happening when you pick each of the choices, and how are you checking which action is being performed?
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

Post by marionline »

I made some screenshots:


This is before the battle script. The fight is triggered by the NPC running the script.
Image

You are teleported to the battle map. And the menu shows up.
Image
(The Enemy-NPC is facing the wrong direction, but that dosen't matter)

You choose an action. For example wait, and the hero faces you and waits. (Because it changes it's walkabout and then goes throug the directions, you have a little animation.)

Image

Choosing, Attack 1, Attack 2 the menu seems to not disappear immediatly. It can stll be seen on the screenshot. The hero runs away, instead of showing it's scripted attack animation.
Attack 1:
Image
Attack 2:
Image

Running:
Image
Also here, the menu is still visible.

That's what the menu looks like in the editor:
Image

I hope this helps to find the problem. :???:

I'll also post the script again, with the wait for key command added, just to make sureI did not add it the wrong way.

Code: Select all

include, Battlesystem.hsi
include, plotscr.hsd

global variable (0, ActiveHero) 
  #the global  var goes from 1 to 4  to loop throug 
  #the positions of the active fighter

plotscript, Battle, begin

# This is the Instead-of-Battle script named Battle.txt
  suspend caterpillar
  suspend player
  
  fade screen out
  wait
 # prepare for going back
  variable  (LastX)
  LastX := hero X (me)
  variable (LastY)
  LastY := hero Y (me)
  variable (LastMap)
  LastMap := current map #remembers where the heros came from 
  
  teleport to map (2)
  wait
  
  # Setting things up
    
  variable (Pos1) #HeroAtPosition1
  variable (Pos2)
  variable (Pos3)
  variable (Pos4)


  #	Me
  #Pos1 := hero by slot (0)
  # Here X ans Y for hero:
  set hero position (me, 14, 2)
  wait for hero (me)
  #set hero picture (me,  0, outside battle)
  #Wait for hero (me)
  
  set hero direction (me, west)
  Wait for hero (me)
  wait (8)
  
  # Hero 2
  #Pos1 := hero by slot (0)
  set hero position (1, 14, 3)
  wait for hero (1)
  #set hero picture (1,  0, outside battle)
  #Wait for hero (1)
  
  set hero direction (1, west)
  Wait for hero (1)
  wait (8)

  #	Hero 3
  #Pos1 := hero by slot (0)
  set hero position (2, 14, 4)
  wait for hero (2)
  #set hero picture (2,  0, outside battle)
  #Wait for hero (2)
  
  set hero direction (2, west)
  Wait for hero (2)
  wait (8)

  # Hero 4
  #Pos1 := hero by slot (0)
  set hero position (3, 14, 5)
  wait for hero (3)
  wait for hero (3)
  #set hero picture (3,  0, outside battle)
  #Wait for hero (3)
  
  set hero direction (3, west)
  Wait for hero (3)
  wait (8)

  put camera (10,3)
  wait for camera
  
  # start the Battle
  fade screen in
  wait


   
  ###############################
  # Do Stuff here, as long as 
  # Enemie and you have HP
  ###############################
  
  variable (id) # wich hero is active?  
  id := hero by slot(ActiveHero) 
  
  # variables for attacks, skills ect. Not in use 
  variable (Attk1)
  variable (Attk2)
  variable (Attk3)
  variable (Attk4)
  
  # showing the HP (replace later by bar, add MP bar as well)
  variable (ActiveHeroHP) 
  ActiveHeroHP := get hero stat (ActiveHero,0,current stat)
  # Show Hero's HP
  show value (ActiveHeroHP)
  
  # Menu wich shows the hero's Attacks
    variable (HeroM) # Menu variable handle for certain Hero
    
    if (id == 0) then 		( HeroM  := 1) # Hero 0 (Name) gets Menu 1
    else if (id == 1) then  ( HeroM  := 2)
    else if (id == 2) then  ( HeroM  := 3) 
    
    variable (BattleM) 
	SetVariable (BattleM, OpenMenu (HeroM,0))    
 
 if (menu is open(BattleM)) then (
    
 # Menu item handle variables depend of the open Menu, 
 # so actually it depends on the active hero 
    variable (Choice1)
    variable (Choice2)
    variable (Waiting)
    variable (Running) 
    
    Choice1		:= first menu  item(BattleM)
	Choice2		:= next  menu  item(Choice1)
    #Choice3		:= next  menu  item(Choice2)
    #Choice4		:= next  menu  item(Choice3)
    Waiting		:= next  menu  item(Choice2) #change Choice3
    Running		:= next  menu  item(Waiting)
    
     
     variable (DoDamage) # Damage amount an attack deals
     variable (DoMPCost) # MP amount an attack costs 
     
     #Special = get menu item extra(Choice1, extra 2) 
     #Ideas:
     # 0 = target one; 
     #1= target all; 
     #2= usable from above; 
     #3= enviroment effect;
     
    waitForKey(useKey) #waits for a key to be used    
    
    if (selected menu item (BattleM) == Choice1) then
		(
			#DoDamage  := get menu item extra(Choice1, extra 0)
			#DoMPCost  := get menu item extra(Choice1, extra 1)
			
			walk hero (ActiveHero, 1, west)
			wait for hero
			
			# Number depends on Hero!
			set hero picture (ActiveHero,4,outside battle)	
			wait for hero
			set hero direction (ActiveHero, north)
			wait for hero
			set hero direction (ActiveHero, east)
			wait for hero
			set hero direction (ActiveHero, south)
			wait for hero
			set hero direction (ActiveHero, west)
			wait for hero
			
			
			close menu(BattleM)
		)	
    
    else if (selected menu item (BattleM) == Choice2) then
		(
			#DoDamage  := get menu item extra(Choice2, extra 0)
			#DoMPCost  := get menu item extra(Choice2, extra 1)
			
			walk hero (ActiveHero, 1, west)
			wait for hero
			
			# Number depends on Hero!
			set hero picture (ActiveHero,5,outside battle)	
			wait for hero
			set hero direction (ActiveHero, north)
			wait for hero
			set hero direction (ActiveHero, east)
			wait for hero
			set hero direction (ActiveHero, south)
			wait for hero
			set hero direction (ActiveHero, west)
			wait for hero
			
			# Number depends on Hero!
			set hero picture (ActiveHero,0,outside battle)
			wait for hero
		
			walk hero (ActiveHero, 1, east)
			wait for hero
		
			set hero direction(ActiveHero, west)
			wait for hero
			
			close menu(BattleM)
		)
	
    else if (selected menu item (BattleM) == Waiting) then
		(
			# Number depends on Hero!
			set hero picture (ActiveHero,8,outside battle)	
			wait for hero
			set hero direction (ActiveHero, north)
			wait for hero
			set hero direction (ActiveHero, east)
			wait for hero
			set hero direction (ActiveHero, south)
			wait for hero
			set hero direction (ActiveHero, west)
			wait for hero
			
			close menu(BattleM)
						
		)  
   else if (selected menu item (BattleM) == Running) then
		(
			walk hero (ActiveHero, east ,2)
			wait for hero		
			
			close menu(BattleM)
		)
		
		
    else () # nothing choosen ...
    )
   
  ##################################
  # End of Battle, you've won:
  # Enemie Hp are 0 or below 
  #################################  
  
  wait(16)
  
  #Reset the scene
   fade screen out
   wait
    
  camera follows hero (me)
  wait for camera
    
  # Change the hero pictures back to  
  # map appropriate ones
  
  # If you rearrange the party, 
  # the party slot numbers do not change. 
  
  set hero picture (me,0,outsidebattle)
    
  if (hero by slot(1) == 1) then (
    reset hero picture (1,outsidebattle) 
  )
  
  if (hero by slot(2) == 2) then (
   reset hero picture (1,outsidebattle) 
  )
  
  if (hero by slot(3) == 3) then (
   reset hero picture (1,outsidebattle) 
  )
  
  # go back to last map 
  teleport to map (lastMap)
  set hero position (me, LastX,LastY)
  Wait for hero (me)
  
  fade screen in
  wait
  
  resume player
  resume caterpillar
  
  ###################################
  # End of Battle, you lost
  # Your HP are 0 -> Game Over Script 
  ####################################
  
 end   
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

Post by marionline »

Image

I added a textbox for each attack.
Apparently the right textboxes are shown, but the hero is leaving the screen and the animation that is supposed to be shown does not well, show up.
Also, I fail to find what part of the script makes the hero move east.

Is it getting more confusing? :???:


Problem solved!

walk hero (who, direction, distance)
I swapped direction and distance. xD

Last edited by marionline on Sun Jan 25, 2015 3:07 pm, edited 1 time in total.
User avatar
Urkelbot666
Slime Knight
Posts: 188
Joined: Sat Oct 18, 2014 5:29 pm

Post by Urkelbot666 »

So this is a pretty intriguing system you're working on. I assume that you're trying to make more non-linear battles, or incorporate a form of battle scripting.

I was wondering if you would mind if I took inspiration from the scripts you've posted, and tried to do something similar with them. It seems like there is the possibility of doing some really cool battles with the system you're working on!
Post Reply