Help with Loading from File
Community Forums/General Help/Help with Loading from File
| ||
I created a soundboard program that works except for one issue: it is either not saving or not loading the last button made and I don't understand why... Here is the code: Window = CreateWindow("Soundboard",0,0,975,690) Global numButtons = 0 Type Button Field x,y Field red,green,blue Field script$ Field audio$ Field audioText$ Field bPage End Type sel.Button = New Button sbCanvas = CreateCanvas(5,5,800,600,Window) bNewButton = CreateButton("New Button",810,5,145,20,Window,1) bEditMode = CreateButton("Edit Mode",810,30,145,20,Window,2) bDeleteButton = CreateButton("Delete Button",810,55,145,20,Window,1) bExitButton = CreateButton("Exit",810,565,145,40,Window,1) pageNeg10 = CreateButton("-10",810,540,20,20,Window,1) pageNeg5 = CreateButton("-5",835,540,20,20,Window,1) pageNeg1 = CreateButton("-1",860,540,20,20,Window,1) pagePlus1 = CreateButton("+1",885,540,20,20,Window,1) pagePlus5 = CreateButton("+5",910,540,20,20,Window,1) pagePlus10 = CreateButton("+10",935,540,20,20,Window,1) lX = CreateLabel("X",810,110,45,20,Window) lY = CreateLabel("Y",860,110,45,20,Window) lPage = CreateLabel("Page#",910,110,45,20,Window) Global txtX = CreateTextField(810,130,45,20,Window) Global txtY = CreateTextField(860,130,45,20,Window) Global txtPage = CreateTextField(910,130,45,20,Window) lRed = CreateLabel("R",810,160,45,20,Window) lGreen = CreateLabel("G",860,160,45,20,Window) lBlue = CreateLabel("B",910,160,45,20,Window) Global txtRed = CreateTextField(810,180,45,20,Window) Global txtGreen = CreateTextField(860,180,45,20,Window) Global txtBlue = CreateTextField(910,180,45,20,Window) lText = CreateLabel("Text",810,210,145,20,Window) lPath = CreateLabel("File Path",810,260,145,20,Window) Global txtScript$ = CreateTextField(810,230,145,20,Window) Global txtAudio$ = CreateTextField(810,280,145,20,Window) Global lSelected = CreateLabel("Selected Button",810,310,145,20,Window) Global txtSelected = CreateTextArea(810,330,145,20,Window) page = 0 SetBuffer CanvasBuffer(sbCanvas) fntTNR = LoadFont("Times New Roman",18) SetFont fntTNR load() Repeat Cls If sel\script = "" Else SetTextAreaText txtSelected,sel\script EndIf For b.Button = Each Button If page = b\bPage Color b\red,b\green,b\blue Rect b\x,b\y,100,30,1 Color 0,0,0 Text b\x + (50), b\y + (15),b\script$,1,1 EndIf Next If KeyHit(203) And page > 1 page = page - 1 Else If KeyHit(205) page = page + 1 EndIf If MouseHit(1) For b.Button = Each Button If MouseX(sbCanvas) > b\x And MouseX(sbCanvas) < (b\x + 100) And MouseY(sbCanvas) > b\y And MouseY(sbCanvas) < (b\y + 30) And page = b\bPage If(ButtonState(bEditMode)) sel.Button = b.Button Else StopChannel(sound) sound = PlaySound(b\audio) EndIf EndIf Next EndIf If ButtonState(bEditMode) EnableGadget bDeleteButton Else DisableGadget bDeleteButton EndIf If WaitEvent() = $401 If EventSource() = bExitButton Exit ElseIf EventSource() = bNewButton newButton() ElseIf EventSource() = bDeleteButton Delete sel.Button sel.Button = New Button ElseIf EventSource() = pageNeg10 If page < 10 page = 0 Else page = page - 10 EndIf ElseIf EventSource() = pageNeg5 If page < 5 page = 0 Else page = page - 5 EndIf ElseIf EventSource() = pageNeg1 If page > 0 page = page - 1 EndIf ElseIf EventSource() = pagePlus1 page = page + 1 ElseIf EventSource() = pagePlus5 page = page + 5 ElseIf EventSource() = pagePlus10 page = page + 10 EndIf EndIf FlipCanvas sbCanvas Forever save() ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;Saves buttons before closing program;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Function save() Delete sel.Button fileOut = WriteFile("buttons.dat") WriteInt(fileOut, numButtons) For b.button = Each button WriteInt(fileOut, b\x) WriteInt(fileOut, b\y) WriteInt(fileOut, b\red) WriteInt(fileOut, b\green) WriteInt(fileOut, b\blue) WriteString(fileOut, b\script) WriteString(fileOut, b\audioText) WriteInt(fileOut, b\bPage) Next End Function ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;Loads buttons at start of program;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Function load() x = -1 y = -1 red = -1 green = -1 blue = -1 script$ = "" audio$ = "" page = -1 If ReadFile("buttons.dat") = 0 temp = WriteFile("buttons.dat") CloseFile(temp) EndIf fileIn = ReadFile("buttons.dat") numButtons = ReadInt(fileIn) For i = 0 To numButtons + 1 x = ReadInt(fileIn) y = ReadInt(fileIn) red = ReadInt(fileIn) green = ReadInt(fileIn) blue = ReadInt(fileIn) script$ = ReadString(fileIn) audioText$ = ReadString(fileIn) page = ReadInt(fileIn) createNewButton(x,y,red,green,blue,script,audioText,page) Next End Function ;;;;;;;;;;;;;;;;;;;;;; ;;Creates new button;; ;;;;;;;;;;;;;;;;;;;;;; Function createNewButton(x,y,red,green,blue,script$,audioText$,bPage) b.Button = New Button b\x = x b\y = y b\red = red b\green = green b\blue = blue b\script$ = script b\audioText = audioText b\audio = LoadSound(audioText) b\bPage = bPage End Function Function newButton() x = TextFieldText(txtX) y = TextFieldText(txtY) red = TextFieldText(txtRed) green = TextFieldText(txtGreen) blue = TextFieldText(txtBlue) script$ = TextFieldText(txtScript) audioText$ = TextFieldText(txtAudio) page = TextFieldText(txtPage) createNewButton(x,y,red,green,blue,script,audioText,page) numButtons = numButtons + 1 End Function |
| ||
hi, first of all, to put your Code into a codebox : read the forum codes under the codebox section. second, i have managed to debug your program, and came this far: the debugging commands are still active. i have changed a bit of your program to isolate that bug. simply said: What is happening is that your save function is saving 1 empty button. Your loading function then loads the right ammount of buttons, but as the first button is empty, it is drawn in black with empty values, and the last button is not loaded. so whenever you start and end the program, you get less and less visibile buttons. i can only guess that it is the Function save() Delete sel.Button which actually empties the object, but doesnt remove it yet. as it is actually late, im going to bed now. edit: to fix this you need to make following change at the top of your program: (the sel.button needs to be set global): Global sel.Button = New Button its not changed in the above code so make sure you do it ;) |
| ||
Thank you very much! |