martin
18-06-2009, 11:49
Here's a script with 2 very simple functions to load and save (multiple) settings. Maybe others find it usefull too.
uses "file"
function tbmain()
'save some settings to settings.txt
SaveSetting "Player1Name","Martin"
SaveSetting "Player2Name","Richard"
'load the settings from settings.txt
msgbox 0,"Player 1: " & LoadSetting("Player1Name")
msgbox 0,"Player 2: " & LoadSetting("Player2Name")
saveSetting "Player2Name","Deborah" 'change name of player2
msgbox 0,"Player 2: " & LoadSetting("Player2Name")
end function
function Loadsetting(tag as string) as string
if file_exists(APP_ScriptPath & "settings.txt") then function=grab$(file_load(APP_ScriptPath & "settings.txt"),"<" & tag & ">","</" & tag & ">")
end function
function SaveSetting(tag as string,txt as string)
local oldline,Content as string
if file_exists(APP_ScriptPath & "settings.txt") then
Content=file_load(APP_ScriptPath & "settings.txt")
oldline="<" & tag & ">" & grab$(Content,"<" & tag & ">","</" & tag & ">") & "</" & tag & ">"
if grab$(Content,"<" & tag & ">","</" & tag & ">")="" then
Content+=$crlf & "<" & tag & ">" & txt & "</" & tag & ">"
else
Content=replace$(Content,oldline,"<" & tag & ">" & txt & "</" & tag & ">")
end if
end if
if Content="" then Content="<" & tag & ">" & txt & "</" & tag & ">"
file_save(APP_ScriptPath & "settings.txt",Content)
end function
uses "file"
function tbmain()
'save some settings to settings.txt
SaveSetting "Player1Name","Martin"
SaveSetting "Player2Name","Richard"
'load the settings from settings.txt
msgbox 0,"Player 1: " & LoadSetting("Player1Name")
msgbox 0,"Player 2: " & LoadSetting("Player2Name")
saveSetting "Player2Name","Deborah" 'change name of player2
msgbox 0,"Player 2: " & LoadSetting("Player2Name")
end function
function Loadsetting(tag as string) as string
if file_exists(APP_ScriptPath & "settings.txt") then function=grab$(file_load(APP_ScriptPath & "settings.txt"),"<" & tag & ">","</" & tag & ">")
end function
function SaveSetting(tag as string,txt as string)
local oldline,Content as string
if file_exists(APP_ScriptPath & "settings.txt") then
Content=file_load(APP_ScriptPath & "settings.txt")
oldline="<" & tag & ">" & grab$(Content,"<" & tag & ">","</" & tag & ">") & "</" & tag & ">"
if grab$(Content,"<" & tag & ">","</" & tag & ">")="" then
Content+=$crlf & "<" & tag & ">" & txt & "</" & tag & ">"
else
Content=replace$(Content,oldline,"<" & tag & ">" & txt & "</" & tag & ">")
end if
end if
if Content="" then Content="<" & tag & ">" & txt & "</" & tag & ">"
file_save(APP_ScriptPath & "settings.txt",Content)
end function