Weird problem after ELSE

Just starting out? Need help? Post your questions and find answers here.
nicalejo
Posts: 15
Joined: Thu May 18, 2017 4:06 pm

Weird problem after ELSE

Post by nicalejo »

Hi, I am completely new to spiderbasic and I have a limited experience in web programming, so please be patient ;)

I am going to paste a piece of code and trying to make you understand the problem:

Code: Select all

!$("head").append("<link rel='stylesheet' type='text/css' href='Data/js/messi.css' />");

!$.getScript("Data/js/messi.js", loadok);
!function loadok()
!{
Debug "OK"
!}
  
  
Structure Player
  name.s
  finalres.l
EndStructure

Global Dim players.Player(4)
Global currentHand = 0
Global txtP1, txtP2, txtP3, txtP4

Procedure CloseWindowEvent()
  CloseWindow(EventWindow())
EndProcedure

Procedure GadgetEvents()
  Select EventGadget()
      
  EndSelect
EndProcedure

Procedure Alert(title.s,message.s)
  !new Messi(v_message, {title: v_title, width: '200px', titleClass: 'anim error', modal: true , buttons: [{id: 0, label: 'Close', val: 'X'}]});
EndProcedure 

Runtime Procedure doNewDlg()
  If GetGadgetText(DialogGadget(0, "player1")) = "" Or
     GetGadgetText(DialogGadget(0, "player2")) = "" Or
     GetGadgetText(DialogGadget(0, "player3")) = "" Or
     GetGadgetText(DialogGadget(0, "player4")) = ""
    Alert("New","Names cannot be empty")
  Else
    ; Set names and initialize scores
    players(0)\name =  UCase(GetGadgetText(DialogGadget(0, "player1")))
    players(0)\finalres = 0
    players(1)\name =  UCase(GetGadgetText(DialogGadget(0, "player2")))
    players(1)\finalres = 0
    players(2)\name =  UCase(GetGadgetText(DialogGadget(0, "player3")))
    players(2)\finalres = 0
    players(3)\name =  UCase(GetGadgetText(DialogGadget(0, "player4")))
    players(3)\finalres = 0
    currentHand = 1
    FreeDialog(0)
  EndIf
EndProcedure

Runtime Procedure cancelNewDlg()
  FreeDialog(0)
EndProcedure

Procedure newDlg()
  #Dialog = 0
  #Xml = 0
  
  XML$ = "<window id='#PB_Any' name='new' text='New game' minwidth='auto' minheight='auto' maxheight='auto' flags='#PB_Window_ScreenCentered | #PB_Window_SizeGadget'>" +
         "    <vbox expand='no'>" +
         "      <text text='Player #1' />" +
         "      <string name='player1' text='' width='200' />" +
         "      <text text='Player #2' />" +
         "      <string name='player2' text='' width='200' />" +
         "      <text text='Player #3' />" +
         "      <string name='player2' text='' width='200' />" +
         "      <text text='Player #4' />" +
         "      <string name='player3' text='' width='200' />" +
         "      <empty /> " +
         "      <empty /> " +
         "      <hbox  expand='equal'>" +
         "        <button text='Ok' onevent='doNewDlg()' height='30' />" +
         "        <button text='Cancel' onevent='cancelNewDlg()' height='30' />" +
         "      </hbox>" +
         "    </vbox>" +
         "  </window>"
  
  If ParseXML(#Xml, XML$) And XMLStatus(#Xml) = #PB_XML_Success
    
    If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #Xml, "new")
      Debug "Dialog created"      
    Else  
      Debug "Dialog error: " + DialogError(#Dialog)
    EndIf
  Else
    Debug "XML error: " + XMLError(#Xml) + " (Line: " + XMLErrorLine(#Xml) + ")"
  EndIf
EndProcedure

Procedure MenuEvents()
  Select EventMenu()
    Case 0 ; New
     newDlg() 
    Case 1 ; Load
    Case 2 ; Save
    Case 3 ; Undo
    Case 4 ; Draw
    Case 4 ; Show
  EndSelect
EndProcedure
At the end of the code I call, depending on a menu, the newDLG() method. It works and shows the dialog. On it, if I push Cancel, the freedialog(0) call releases an closes the dialog (calling the RunTime cancelNewDlg()). If I push OK (calling the RunTime doNewDlg()), I check four stringgadgets to avoid emptyness. This check is done in an IF statement and shows an alert, but if all four have strings, the ELSE simply hangs the application, even I cannot show Debug statements

Any advise will be appreciated

THX
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Weird problem after ELSE

Post by Peter »

Code: Select all

!$("head").append("<link rel='stylesheet' type='text/css' href='Data/js/messi.css' />");

!$.getScript("Data/js/messi.js", loadok);
!function loadok()
!{
Debug "OK"
!}

i guess, that the path of the css- or js-file is not ok. Try to check the browser-console for a 404-Error (messi.* not found) after running your application.

This one works for me:

Code: Select all

! $("head").append("<link rel='stylesheet' type='text/css' href='https://cdn.rawgit.com/MessiJS/MessiJS/504c130f/dist/messi.min.css' />");

! $.getScript("https://cdn.rawgit.com/MessiJS/MessiJS/504c130f/dist/messi.min.js", loadok);

! function loadok() {
Debug "OK"
! }
Greetings ... Peter
nicalejo
Posts: 15
Joined: Thu May 18, 2017 4:06 pm

Re: Weird problem after ELSE

Post by nicalejo »

This is working, I simply put it on a local directory inside Data (the resources directory). This is the js code that shows pretty alerts and is working ok. I have even replaced my code with yours and the problem is the same.

THX
nicalejo
Posts: 15
Joined: Thu May 18, 2017 4:06 pm

Re: Weird problem after ELSE

Post by nicalejo »

Maybe I am abusing but I paste the link to the drive containing the full source of the app. I am really stuck. Hope someone can debug and find the explanation for the freezing of the application.

https://drive.google.com/file/d/0B3zCPW ... sp=sharing

Thanks in advance
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Weird problem after ELSE

Post by Peter »

nicalejo wrote:Hope someone can debug and find the explanation for the freezing of the application.
no problems here:

Image

Perhaps it is a good idea, to start your application after messi.js is loaded:

Code: Select all

!$("head").append("<link rel='stylesheet' type='text/css' href='Data/js/messi.css' />");

Structure Player
	name.s
	finalres.l
EndStructure

Global Dim players.Player(4)
Global currentHand = 0
Global txtP1, txtP2, txtP3, txtP4

Procedure CloseWindowEvent()
	CloseWindow(EventWindow())
EndProcedure

Procedure GadgetEvents()
	Select EventGadget()
			
	EndSelect
EndProcedure

Procedure Alert(title.s,message.s)
	!new Messi(v_message, {title: v_title, width: '200px', titleClass: 'anim error', modal: true , buttons: [{id: 0, label: 'Close', val: 'X'}]});
EndProcedure 

Procedure RefreshButtons()
	SetGadgetText(txtP1, players(0)\name)
	SetGadgetText(txtP2, players(1)\name)
	SetGadgetText(txtP3, players(2)\name)
	SetGadgetText(txtP4, players(3)\name)
EndProcedure

Runtime Procedure doNewDlg()
	If GetGadgetText(DialogGadget(0, "player1")) = "" Or
	   GetGadgetText(DialogGadget(0, "player2")) = "" Or
	   GetGadgetText(DialogGadget(0, "player3")) = "" Or
	   GetGadgetText(DialogGadget(0, "player4")) = ""
		Alert("New","Names cannot be empty")
	Else
		; Set names and initialize scores
		players(0)\name =  UCase(GetGadgetText(DialogGadget(0, "player1")))
		players(0)\finalres = 0
		players(1)\name =  UCase(GetGadgetText(DialogGadget(0, "player2")))
		players(1)\finalres = 0
		players(2)\name =  UCase(GetGadgetText(DialogGadget(0, "player3")))
		players(2)\finalres = 0
		players(3)\name =  UCase(GetGadgetText(DialogGadget(0, "player4")))
		players(3)\finalres = 0
		currentHand = 1
		FreeDialog(0)
	EndIf
EndProcedure

Runtime Procedure cancelNewDlg()
	FreeDialog(0)
EndProcedure

Procedure newDlg()
	#Dialog = 0
	#Xml = 0
	
	XML$ = "<window id='#PB_Any' name='new' text='New game' minwidth='auto' minheight='auto' maxheight='auto' flags='#PB_Window_ScreenCentered | #PB_Window_SizeGadget'>" +
	       "    <vbox expand='no'>" +
	       "      <text text='Player #1' />" +
	       "      <string name='player1' text='' width='200' />" +
	       "      <text text='Player #2' />" +
	       "      <string name='player2' text='' width='200' />" +
	       "      <text text='Player #3' />" +
	       "      <string name='player2' text='' width='200' />" +
	       "      <text text='Player #4' />" +
	       "      <string name='player3' text='' width='200' />" +
	       "      <empty /> " +
	       "      <empty /> " +
	       "      <hbox  expand='equal'>" +
	       "        <button text='Ok' onevent='doNewDlg()' height='30' />" +
	       "        <button text='Cancel' onevent='cancelNewDlg()' height='30' />" +
	       "      </hbox>" +
	       "    </vbox>" +
	       "  </window>"
	
	If ParseXML(#Xml, XML$) And XMLStatus(#Xml) = #PB_XML_Success
		
		If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #Xml, "new")
			Debug "Dialog created"      
		Else  
			Debug "Dialog error: " + DialogError(#Dialog)
		EndIf
	Else
		Debug "XML error: " + XMLError(#Xml) + " (Line: " + XMLErrorLine(#Xml) + ")"
	EndIf
EndProcedure

Procedure MenuEvents()
	Select EventMenu()
		Case 0 ; New
			newDlg() 
		Case 1 ; Load
		Case 2 ; Save
		Case 3 ; Undo
		Case 4 ; Draw
		Case 4 ; Show
	EndSelect
EndProcedure

Procedure Start()
	
	ExamineDesktops()
	
	WindowWidth  = DesktopWidth(0)
	WindowHeight = DesktopHeight(0)
	
	Top = 10
	GadgetHeight = 24
	
	If OpenWindow(0, 0, 0, WindowWidth, WindowHeight, "Mahjong Score", #PB_Window_Background)
		
		; Toolbar
		If CreateToolBar(0, WindowID(0))
			Global newBtn = ToolBarImageButton(0, ImageID(0))
			ToolBarToolTip(0, newBtn, "New game")
			Global loadBtn = ToolBarImageButton(1, ImageID(1))
			ToolBarToolTip(0, loadBtn, "Load game")
			Global saveBtn = ToolBarImageButton(2, ImageID(2))
			ToolBarToolTip(0, saveBtn, "Save game")
			ToolBarSeparator()
			Global undoBtn = ToolBarImageButton(3, ImageID(3))
			ToolBarToolTip(0, undoBtn, "Undo annotation")
			Global drawBtn = ToolBarImageButton(4, ImageID(4))
			ToolBarToolTip(0, drawBtn, "Draw hand")
			ToolBarSeparator()
			Global showBtn = ToolBarImageButton(5, ImageID(5))
			ToolBarToolTip(0, showBtn, "Show annotations")
			
			BindEvent(#PB_Event_Menu, @MenuEvents())
		EndIf
		
		; Four big buttons
		height = WindowHeight - ToolBarHeight(0)
		If (height > WindowWidth)
			width = (WindowWidth - 20) / 2
		Else
			width = (height - 20) / 2
		EndIf
		
		ContainerGadget(#PB_Any,10, ToolBarHeight +10, width, width, #PB_Container_Raised )     
		txtP1 = TextGadget(#PB_Any, width / 2 - 20, width / 2 - 60, 40, 40, "East", #PB_Text_Center | #PB_Text_VerticalCenter)
		ButtonImageGadget(#PB_Any, width / 2 - 30, width / 2 - 30, 60, 60, ImageID(6))
		CloseGadgetList()
		
		ContainerGadget(#PB_Any, 10 + width, ToolBarHeight +10, width, width, #PB_Container_Raised )     
		txtP2 = TextGadget(#PB_Any, width / 2 - 20, width / 2 - 60, 40, 40, "South", #PB_Text_Center | #PB_Text_VerticalCenter)
		ButtonImageGadget(#PB_Any, width / 2 - 30, width / 2 - 30, 60, 60, ImageID(7))
		CloseGadgetList()
		
		ContainerGadget(#PB_Any, 10, ToolBarHeight + 10 + width, width, width, #PB_Container_Raised )     
		txtP3 = TextGadget(#PB_Any, width / 2 - 20, width / 2 - 60, 40, 40, "West", #PB_Text_Center | #PB_Text_VerticalCenter)
		ButtonImageGadget(#PB_Any, width / 2 - 30, width / 2 - 30, 60, 60, ImageID(8))
		CloseGadgetList()
		
		ContainerGadget(#PB_Any, 10 + width, ToolBarHeight + 10 + width, width, width, #PB_Container_Raised )     
		txtP4 = TextGadget(#PB_Any, width / 2 - 20, width / 2 - 60, 40, 40, "North", #PB_Text_Center | #PB_Text_VerticalCenter)
		ButtonImageGadget(#PB_Any, width / 2 - 30, width / 2 - 30, 60, 60, ImageID(9))
		CloseGadgetList()
		
		; Actions on gadgets
		BindEvent(#PB_Event_Gadget, @GadgetEvents())
		BindEvent(#PB_Event_CloseWindow, @CloseWindowEvent())
		
		
		CompilerIf #PB_Compiler_OS <> #PB_OS_Web
			Repeat
				Event = WaitWindowEvent()
			Until Event = #PB_Event_CloseWindow
		CompilerEndIf
	EndIf 
EndProcedure

Procedure Loading(Type, Filename$)
	Static NbLoadedElements
	
	NbLoadedElements+1
	If NbLoadedElements = 10 ; Finished the loading of all images, we can start the application
		Start()
	EndIf
EndProcedure

Procedure LoadingError(Type, Filename$)
	Debug Filename$ + ": loading error"
EndProcedure

; Resizing or rotating
Procedure SizeWindowHandler()
	CloseWindow(0)
	Start()
EndProcedure

Procedure LoadImages()
	
	;CloseDebugOutput()
	
	; Resizing or rotation
	BindEvent(#PB_Event_SizeWindow, @SizeWindowHandler(),0)
	
	; Load images
	LoadImage(0, "Data/ToolBar/add.png")
	LoadImage(1, "Data/ToolBar/load.png")
	LoadImage(2, "Data/ToolBar/save.png")
	LoadImage(3, "Data/ToolBar/undo.png")
	LoadImage(4, "Data/ToolBar/draw.png")
	LoadImage(5, "Data/ToolBar/table.png")
	LoadImage(6, "Data/Buttons/east.png")
	LoadImage(7, "Data/Buttons/south.png")
	LoadImage(8, "Data/Buttons/west.png")
	LoadImage(9, "Data/Buttons/north.png")
	
EndProcedure

; Register the loading event before calling any resource load command
BindEvent(#PB_Event_Loading, @Loading())
BindEvent(#PB_Event_LoadingError, @LoadingError())

!$.getScript("Data/js/messi.js", loadok);
!function loadok()
!{
LoadImages()
!}
Greetings ... Peter
nicalejo
Posts: 15
Joined: Thu May 18, 2017 4:06 pm

Re: Weird problem after ELSE

Post by nicalejo »

Please, try to put values in the four fields and push OK, then hangs
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Weird problem after ELSE

Post by Peter »

ah, now i see :P

there are 2 'player2' - StringGadgets in your Xml (and no 'player4'):

Code: Select all

          "      <text text='Player #1' />" +
          "      <string name='player1' text='' width='200' />" +
          "      <text text='Player #2' />" +
          "      <string name='player2' text='' width='200' />" +
          "      <text text='Player #3' />" +
          "      <string name='player2' text='' width='200' />" +
          "      <text text='Player #4' />" +
          "      <string name='player3' text='' width='200' />" +
should be:

Code: Select all

          "      <text text='Player #1' />" +
          "      <string name='player1' text='' width='200' />" +
          "      <text text='Player #2' />" +
          "      <string name='player2' text='' width='200' />" +
          "      <text text='Player #3' />" +
          "      <string name='player3' text='' width='200' />" +
          "      <text text='Player #4' />" +
          "      <string name='player4' text='' width='200' />" +
(nevertheless the SpiderBasic-Compiler shouldn't hang here but report an error)

Greetings ... Peter
nicalejo
Posts: 15
Joined: Thu May 18, 2017 4:06 pm

Re: Weird problem after ELSE

Post by nicalejo »

Thanks a lot, problems of cut & paste, my fault. I really apreciate. You have free beers if you come to Valencia, Spain.
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Weird problem after ELSE

Post by Peter »

nicalejo wrote:You have free beers if you come to Valencia, Spain.
Image
Cheers!
Post Reply