Hi,
This is the first piece of code I wrote in BMax, it's supposed to read a preference txt file which contains settings:
--------------------------------------------------------------------------------------------
Strict
Global SettingsFile$ = "test.txt"
Global User_Credits%, Base_Points%, Talent_Amount#, ReGen_Cost%, Break_Min#, Break_Max#, Traits_Total% '
Global file:TStream = ReadFile(SettingsFile)
If Not file RuntimeError "could not open file " + SettingsFile
While Not Eof(file)
Local CurrentLine$ = ReadLine(file) 'Reads a line from the file
'Local Label$ 'How come the code didn't work (No error message) if I decleared label here instead of inside the loop?
If CurrentLine [0..1] <> "'" Then
Local BeginPos = CurrentLine.Find(":")+1 'Find the : mark then increase the postition by 1
Local Label$ = CurrentLine [0..CurrentLine.Find(":")] 'Cut the string from the start of the string til the ':' character, not including the ':'
Select Label
Case "User Credits" User_Credits = Int CurrentLine[BeginPos..CurrentLine.Length]
Case "Base Points" Base_Points = Int CurrentLine[BeginPos..CurrentLine.Length]
Case "Talent Amount" Talent_Amount = Float CurrentLine[BeginPos..CurrentLine.Length]
Case "ReGen Cost" ReGen_Cost = Int CurrentLine[BeginPos..CurrentLine.Length]
Case "Break Min" Break_Min = Float CurrentLine[BeginPos..CurrentLine.Length]
Case "Break Max" Break_Max = Float CurrentLine[BeginPos..CurrentLine.Length]
Case "Traits Total" Traits_Total = Int CurrentLine[BeginPos..CurrentLine.Length]
End Select
End If
Wend
Print User_Credits
Print Base_Points
Print Talent_Amount
Print ReGen_Cost
Print Break_Min
Print Break_Max
Print Traits_Total
CloseStream file
--------------------------------------------------------------------------------------------
The settings file is like this:
'Test Settings file
User Credits: 10000
Base Points: 150
Talent Amount: .15
ReGen Cost: 200
Break Min: .45
Break Max: .55
Traits Total: 9
My question is:
1. Why all three float numbers end up having not quite accurate values, for example Break_Min, instead of .45, it got 0.449999988
2. how do I round numbers to two decimals? In VBA I'd use ROUND but I think it's something different in Bmax?
3. There are better ways to do this right?
I'm no programmer at all, have only been doing VBA scripts for 2 weeks and bought Bmax just yesterday, so please go easy on me ;D
I'm a game artist, been doing that for many years, but want to get into actually design some games myself hahaha, dunno if I could ever do that, being able to illustrate my ideas with code would be cool in enough!
Thanks!
This is the first piece of code I wrote in BMax, it's supposed to read a preference txt file which contains settings:
--------------------------------------------------------------------------------------------
Strict
Global SettingsFile$ = "test.txt"
Global User_Credits%, Base_Points%, Talent_Amount#, ReGen_Cost%, Break_Min#, Break_Max#, Traits_Total% '
Global file:TStream = ReadFile(SettingsFile)
If Not file RuntimeError "could not open file " + SettingsFile
While Not Eof(file)
Local CurrentLine$ = ReadLine(file) 'Reads a line from the file
'Local Label$ 'How come the code didn't work (No error message) if I decleared label here instead of inside the loop?
If CurrentLine [0..1] <> "'" Then
Local BeginPos = CurrentLine.Find(":")+1 'Find the : mark then increase the postition by 1
Local Label$ = CurrentLine [0..CurrentLine.Find(":")] 'Cut the string from the start of the string til the ':' character, not including the ':'
Select Label
Case "User Credits" User_Credits = Int CurrentLine[BeginPos..CurrentLine.Length]
Case "Base Points" Base_Points = Int CurrentLine[BeginPos..CurrentLine.Length]
Case "Talent Amount" Talent_Amount = Float CurrentLine[BeginPos..CurrentLine.Length]
Case "ReGen Cost" ReGen_Cost = Int CurrentLine[BeginPos..CurrentLine.Length]
Case "Break Min" Break_Min = Float CurrentLine[BeginPos..CurrentLine.Length]
Case "Break Max" Break_Max = Float CurrentLine[BeginPos..CurrentLine.Length]
Case "Traits Total" Traits_Total = Int CurrentLine[BeginPos..CurrentLine.Length]
End Select
End If
Wend
Print User_Credits
Print Base_Points
Print Talent_Amount
Print ReGen_Cost
Print Break_Min
Print Break_Max
Print Traits_Total
CloseStream file
--------------------------------------------------------------------------------------------
The settings file is like this:
'Test Settings file
User Credits: 10000
Base Points: 150
Talent Amount: .15
ReGen Cost: 200
Break Min: .45
Break Max: .55
Traits Total: 9
My question is:
1. Why all three float numbers end up having not quite accurate values, for example Break_Min, instead of .45, it got 0.449999988
2. how do I round numbers to two decimals? In VBA I'd use ROUND but I think it's something different in Bmax?
3. There are better ways to do this right?
I'm no programmer at all, have only been doing VBA scripts for 2 weeks and bought Bmax just yesterday, so please go easy on me ;D
I'm a game artist, been doing that for many years, but want to get into actually design some games myself hahaha, dunno if I could ever do that, being able to illustrate my ideas with code would be cool in enough!
Thanks!