I worked on a neat way to organize functions by how they are used. Setup function are called before the do loop. Loop functions are called inside the loop. Close function are called when the app closes. Simple but very helpful!
Please read everything because there is a lot to this function system.
How To Use:
If you need to setup something before entering the loop then add "Setup_" or "BeforeLoop_" in the Function's Name
Setup Functions:
Function Setup_()
Or
Function BeforeLoop_()
Some Functions have a setup order. If you need to have certain functions setup first then add:
` "SetupFirst_" Or "SetupSecond_" Or "SetupThird_" Or "SetupForth_" Or ... Or "SetupNineth_"
If you have a setup function that does some thing only once then use this code:
Function Setup_SetupFunctionName()
If IsSetup = False
` Run Inital Task
EndIf
EndFunction
Loop Functions:
If you have a function to run within the loop then add "Loop_" Or "Step_" in the function's name
Function Loop_()
Or
Function Step_()
Close Functions:
If you need to save or delete something before closing then add "Save_" Or "OnClose_" Or "Closing_" in the function's name
Function Save_()
Or
Function OnClose_()
Or
Function Closing_()
Full Example:
How it works: Calls Setup(). If File Exists "File.dat" then it has been setup already and IsSetup = 1. SetupFirst_DoStuff is called first and creates a file if IsSetup = false. SetupNinth_DoStuff is called because it is the next function to be called. (I didn't add 2-8) Loop_PrintStuff will print a random number repeativly. OnClose_DoSomething is called when you press the escape key.
CallSetupFunctions() ` Will setup all the libraries automatically!
Disable EscapeKey
Do
If EscapeKey() = 1
CallCloseFunctions()
End
EndIf
` Functions are automatically called here!
Sync
Loop
Function SetupFirst_DoStuff()
Message "Setup First"
If IsSetup = False
Open To Write 1, "File.dat" : Close File 1
Message "File.dat Created"
Else
Message "File.dat Already Created"
EndIf
EndFunction
Function SetupNinth_DoStuff()
Message "Setup Ninth"
EndFunction
Function Loop_PrintStuff()
Print RND(2524)
EndFunction
Function OnClose_DoSomething()
Message "Closed App"
EndFunction
`--------------------------------------------------------------------------------------
Function Setup() ` AUTOMATICALLY CALLED FIRST
` If it is already setup then there is no need to re-setup everything
Global IsSetup
` Setup checks: If it is already setup then there is no need to re-setup everything.
` CHECK IF INITIAL SETUP HAS BEEN SETUP HERE
If File Exist("File.dat") = 1 Then IsSetup = 1
If IsSetup = 1 Then Message "IsSetup = 1"
` All Loop Functions are automatically called.
Set PreSync CallBack "CallLoopFunctions"
` Setup Plugins:
`D3D_Init ` If D3D Commands are used then uncomment this
`Phy Start ` If Dark Physics Commands are used then uncomment this
`AI Init ` If Dark AI commands are going to be used then uncomment this
AutoCam Off ` Normally this is turned off to prevent the camera to change position every time an object is created
Sync On : Sync Rate 60 ` Standard FPS Rate
EndFunction
Function CallSetupFunctions()
Count = Get Function Count()
` Special case for functions that need to be called before the rest
For Index = 1 To Count
Name$ = Get Function Name$(Index)
If Name$ = "Setup" Then Setup$ = Name$
If Mid$(Name$, 1, 11) = "SetupFirst_" Then First$ = Name$
If Mid$(Name$, 1, 12) = "SetupSecond_" Then Second$ = Name$
If Mid$(Name$, 1, 11) = "SetupThird_" Then Third$ = Name$
If Mid$(Name$, 1, 11) = "SetupForth_" Then Forth$ = Name$
If Mid$(Name$, 1, 11) = "SetupFifth_" Then Fifth$ = Name$
If Mid$(Name$, 1, 11) = "SetupSixth_" Then Sixth$ = Name$
If Mid$(Name$, 1, 13) = "SetupSeventh_" Then Seventh$ = Name$
If Mid$(Name$, 1, 12) = "SetupEighth_" Then Eighth$ = Name$
If Mid$(Name$, 1, 12) = "SetupNinth_" Then Ninth$ = Name$
Next Index
If Setup$ <> "" Then Call Function Name Setup$
If First$ <> "" Then Call Function Name First$
If Second$ <> "" Then Call Function Name Second$
If Third$ <> "" Then Call Function Name Third$
If Forth$ <> "" Then Call Function Name Forth$
If Fifth$ <> "" Then Call Function Name Fifth$
If Sixth$ <> "" Then Call Function Name Sixth$
If Seventh$ <> "" Then Call Function Name Seventh$
If Eighth$ <> "" Then Call Function Name Eighth$
If Nineth$ <> "" Then Call Function Name Ninth$
` Execute the rest of the setup functions
For Index = 1 To Count
Name$ = Get Function Name$(Index)
If Mid$(Name$, 1, 6) = "Setup_" Or Mid$(Name$, 1, 11) = "BeforeLoop_"
Call Function Name Name$
EndIf
Next Index
EndFunction
Function CallLoopFunctions() ` CALLED AUTOMATICALLY BY PRESYNC
Count = Get Function Count()
For Index = 1 To Count
Name$ = Get Function Name$(Index)
If Mid$(Name$, 1, 5) = "Loop_" Or Mid$(Name$, 1, 5) = "Step_"
Call Function Name Name$
EndIf
Next Index
EndFunction
Function CallCloseFunctions()
Count = Get Function Count()
For Index = 1 To Count
Name$ = Get Function Name$(Index)
If Mid$(Name$, 1, 5) = "Save_" Or Mid$(Name$, 1, 8) = "OnClose_" Or Mid$(Name$, 1, 8) = "Closing_"
Call Function Name Name$
EndIf
Next Index
EndFunction
Example:
Call Setup functions will get everything going
If escape key = 1 then run all the close/save functions
CallSetupFunctions() ` Will setup all the libraries automatically!
Disable EscapeKey
Do
If EscapeKey() = 1
CallCloseFunctions()
End
EndIf
` Functions are automatically called here!
Sync
Loop
Setup:
IsSetup: This is a variable you can use to test if everything is initially setup. This could be making a few files or something like that. Perform a quick check like for example if a those file exist and set to IsSetup = true if they exist.
Function Setup() ` AUTOMATICALLY CALLED FIRST
` If it is already setup then there is no need to re-setup everything
Global IsSetup
` Setup checks: If it is already setup then there is no need to re-setup everything.
` CHECK IF INITIAL SETUP HAS BEEN SETUP HERE
` All Loop Functions are automatically called.
Set PreSync CallBack "CallLoopFunctions"
` Setup Plugins:
`D3D_Init ` If D3D Commands are used then uncomment this
`Phy Start ` If Dark Physics Commands are used then uncomment this
`AI Init ` If Dark AI commands are going to be used then uncomment this
AutoCam Off ` Normally this is turned off to prevent the camera to change position every time an object is created
Sync On : Sync Rate 60 ` Standard FPS Rate
EndFunction
Call Functions:
I don't really want to get into explaining how this works. It is kinda straight forward how it works. An algorithm to check if the first part of the function name is equal to some preset name. If so then call the function. Not much too it. Just a bit of a lengthy algorithm.
CallSetupFunctions()
Function CallSetupFunctions()
Count = Get Function Count()
` Special case for functions that need to be called before the rest
For Index = 1 To Count
Name$ = Get Function Name$(Index)
If Name$ = "Setup" Then Setup$ = Name$
If Mid$(Name$, 1, 11) = "SetupFirst_" Then First$ = Name$
If Mid$(Name$, 1, 12) = "SetupSecond_" Then Second$ = Name$
If Mid$(Name$, 1, 11) = "SetupThird_" Then Third$ = Name$
If Mid$(Name$, 1, 11) = "SetupForth_" Then Forth$ = Name$
If Mid$(Name$, 1, 11) = "SetupFifth_" Then Fifth$ = Name$
If Mid$(Name$, 1, 11) = "SetupSixth_" Then Sixth$ = Name$
If Mid$(Name$, 1, 13) = "SetupSeventh_" Then Seventh$ = Name$
If Mid$(Name$, 1, 12) = "SetupEighth_" Then Eighth$ = Name$
If Mid$(Name$, 1, 12) = "SetupNinth_" Then Ninth$ = Name$
Next Index
If Setup$ <> "" Then Call Function Name Setup$
If First$ <> "" Then Call Function Name First$
If Second$ <> "" Then Call Function Name Second$
If Third$ <> "" Then Call Function Name Third$
If Forth$ <> "" Then Call Function Name Forth$
If Fifth$ <> "" Then Call Function Name Fifth$
If Sixth$ <> "" Then Call Function Name Sixth$
If Seventh$ <> "" Then Call Function Name Seventh$
If Eighth$ <> "" Then Call Function Name Eighth$
If Ninth$ <> "" Then Call Function Name Ninth$
` Execute the rest of the setup functions
For Index = 1 To Count
Name$ = Get Function Name$(Index)
If Mid$(Name$, 1, 6) = "Setup_" Or Mid$(Name$, 1, 11) = "BeforeLoop_"
Call Function Name Name$
EndIf
Next Index
EndFunction
CallLoopFunctions()
Function CallLoopFunctions() ` CALLED AUTOMATICALLY BY PRESYNC
Count = Get Function Count()
For Index = 1 To Count
Name$ = Get Function Name$(Index)
If Mid$(Name$, 1, 5) = "Loop_" Or Mid$(Name$, 1, 5) = "Step_"
Call Function Name Name$
EndIf
Next Index
EndFunction
CallCloseFunctions()
Function CallCloseFunctions()
Count = Get Function Count()
For Index = 1 To Count
Name$ = Get Function Name$(Index)
If Mid$(Name$, 1, 5) = "Save_" Or Mid$(Name$, 1, 8) = "OnClose_" Or Mid$(Name$, 1, 8) = "Closing_"
Call Function Name Name$
EndIf
Next Index
EndFunction
This snippet is used in my project
Zombie Project: Early WIP.
Any comments, questions, or bugs let me know. Don't forget to credit me if you use any of my code.
DigitalFury