Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  
123iamking

GTA Sa - Cleo 3 - Convert string to int?

Recommended Posts

123iamking

Hi,

Is there anyway I can convert a string to integer in Cleo 3?

 

I just want to read an integer from a file with Cleo 3 (I want to use Cleo 3 for max compatibility)

0A9A: [email protected] = openfile "./CLEO/config.txt" mode 0x72  //0x72 - opens text file for reading

//The file config.txt contains the string "101"

//How can I get integer 101 from variable [email protected]?

 

Thanks.

Edited by 123iamking

Share this post


Link to post
Share on other sites
In45do

I think I have an alternative to get int/float from a file. Maybe you can use a file with .INI as its extension. For example: script.ini

Inside that file you can write it down like this:

[TEST]
Integer = 101

 

Use opcode 0AF0 to get int from script.ini:

0AF0: [email protected] = get_int_from_ini_file "cleo\script.ini" section "TEST" key "Integer"

I don't know if that opcode can work for cleo 3. Please let me know.

Share this post


Link to post
Share on other sites
ZAZ
7 hours ago, In45do said:

I don't know if that opcode can work for cleo 3. Please let me know.

No, ini codes was made for cleo4

 

opcodes 0AAB:, 0A9A:, 0A9D:, 0A9E:, 0A9B: work for cleo3

{$CLEO .cs}
thread 'READWRT'

:READWRITE_1
wait 0
03F0: enable_text_draw 1
045A: text_draw_1number  50.0  300.0 'NUMBER' 1@
045A: text_draw_1number  50.0  310.0 'NUMBER' 2@
if
0AB0:   key_pressed 8// 	BACKSPACE key
jf @READWRITE_1
if
0AAB:   file_exists "CLEO\READWRITE.txt"
jf @READWRITE_SaveData


:READWRITE_Openfile
wait 0
if 
0A9A: 0@ = openfile "CLEO\READWRITE.txt" mode 0x6272  // binary read
jf @READWRITE_Openfile
0A9D: readfile 0@ size 4 from 1@
0A9B: closefile 0@
wait 1500
jump @READWRITE_1



:READWRITE_SaveData
wait 0
if 
0A9A: 0@ = openfile "CLEO\READWRITE.txt" mode 0x6277  // binary read
jf @READWRITE_SaveData
2@ = 2018
0A9E: writefile 0@ size 4 from 2@
0A9B: closefile 0@
wait 1500
jump @READWRITE_1

 

Share this post


Link to post
Share on other sites
123iamking

@ZAZ Thank ZAZ for showing me how `openfile` opcode work (I misused it :p), but I see that the code write & read binary file (thus can't be easily edited by human), Is there anyway I can get the number in a text file, assume I have a text file with the content: 101

I want to get this number 101 as int.

Edited by 123iamking

Share this post


Link to post
Share on other sites
123iamking

I see you did this before with your AddCar_spawner mod, even though I never use this mod, it crashed on me, maybe because I don't have extra car.

Share this post


Link to post
Share on other sites
ZAZ
15 minutes ago, 123iamking said:

I see you did this before with your AddCar_spawner mod, even though I never use this mod, it crashed on me, maybe because I don't have extra car.

I also did that for this mod

I used an algorithm, posted by Wesser

Spoiler
{$CLEO}

0000: NOP

0@ = 0  // File handle
1@ = 4  // File size
2@ = 0  // Read buffer
3@ = 0  // result

gosub @ReadFromTextFile

while true
wait 0  
03F0: enable_text_draw 1
0341: unknown_text_draw_flag 0
0342: enable_text_draw_centered 1
045A: draw_text_1number 320.0 240.0 GXT 'NUMBER' number 3@ // ~1~
end

:ReadFromTextFile
if
0A9A: 0@ = openfile "cleo\camkey.ini" mode 0x6272 // Binary Text Reading
then
repeat
  wait 0
  0A9D: readfile 0@ size 1 to 2@
  for 4@ = 48 to 57
    if
    003B:  4@ == 2@
    then
      3@ *= 10
      4@ -= 48
      005A: 3@ += 4@
      5@ += 1
      break
    end
  end
until 003B:  5@ == 1@
0A9B: closefile 0@
end
return 

 

hmmm.. or maybe a template from anybody else

 

the read algorithm from FreeMovingCamera:

var
7@:int
end
const
lines_to_read = 7@
end

lines_to_read = 10
0050: gosub @FREECAM_File_start
0006: 0@ = 0        //File handle 
0006: 1@ = 0       //Read buffer
0006: 2@ = 0        //result
0006: 5@ = 0        // array index
0006: 7@ = 0        // array index

[main script]

:FREECAM_File_start  
0006: 0@ = 0        //File handle 
0006: 1@ = 0       //Read buffer
0006: 2@ = 0        //result
0006: 5@ = 0        // array index
0A9A: 0@ = openfile "cleo\freecamkey.ini" mode 0x6272

:FREECAM_FileLoop1                                    //Searching " for starting
0001: wait 0 ms 
0A9D: readfile [email protected] size 1 to [email protected]                     // store 1 byte to the buffer  
00D6: if 0
0039:   [email protected] == 34                                   // '"'
004D: jump_if_false @FREECAM_FileLoop1

:FREECAM_FileLoop2
0001: wait 0 ms
0A9D: readfile [email protected] size 1 to [email protected]                     // store 1 byte to the buffer  
00D6: if 
8039:   not  [email protected] == 34 
004D: jump_if_false @FREECAM_submit 
00D6: if and
0019:   [email protected] > 47 
001B:   58 > [email protected] 
004D: jump_if_false @FREECAM_FileLoop1
0012: [email protected] *= 10 
005A: [email protected] += [email protected] // (int) 
000E: [email protected] -= 48
jump @FREECAM_FileLoop2

:FREECAM_submit
0085: [email protected]([email protected],10i) = [email protected] // (int)
0006: [email protected] = 0        //result
000A: [email protected] += 1
if
002D:   [email protected] >= lines_to_read // (int)
004D: jump_if_false @FREECAM_FileLoop1
0A9B: closefile [email protected]
0051: return

 

 

 

 

Edited by ZAZ

Share this post


Link to post
Share on other sites
ZAZ
var
7@:int
end
const
lines_to_read = 7@
end

lines_to_read = 10
0050: gosub @FREECAM_File_start
0006: 0@ = 0        //File handle 
0006: 1@ = 0       //Read buffer
0006: 2@ = 0        //result
0006: 5@ = 0        // array index
0006: 7@ = 0        // array index

<main script>

:FREECAM_File_start  
0006: 0@ = 0        //File handle 
0006: 1@ = 0       //Read buffer
0006: 2@ = 0        //result
0006: 5@ = 0        // array index
0A9A: 0@ = openfile "cleo\freecamkey.ini" mode 0x6272

:FREECAM_FileLoop1                                    //Searching " for starting
0001: wait 0 ms 
0A9D: readfile [email protected] size 1 to [email protected]                     // store 1 byte to the buffer  
00D6: if 0
0039:   [email protected] == 34                                   // '"'
004D: jump_if_false @FREECAM_FileLoop1

:FREECAM_FileLoop2
0001: wait 0 ms
0A9D: readfile [email protected] size 1 to [email protected]                     // store 1 byte to the buffer  
00D6: if 
8039:   not  [email protected] == 34 
004D: jump_if_false @FREECAM_submit 
00D6: if and
0019:   [email protected] > 47 
001B:   58 > [email protected] 
004D: jump_if_false @FREECAM_FileLoop1
0012: [email protected] *= 10 
005A: [email protected] += [email protected] // (int) 
000E: [email protected] -= 48
jump @FREECAM_FileLoop2

:FREECAM_submit
0085: [email protected]([email protected],10i) = [email protected] // (int)
0006: [email protected] = 0        //result
000A: [email protected] += 1
if
002D:   [email protected] >= lines_to_read // (int)
004D: jump_if_false @FREECAM_FileLoop1
0A9B: closefile [email protected]
0051: return

 

Share this post


Link to post
Share on other sites
123iamking

Thank @ZAZ,

Your file reader code is good, but it would loop forever if the setting file (freecamkey.ini in your case) doesn't match the format "Num". The code is not foolproof.

I'd like to improve it

//----------------Get the value from the file ----------
//1 = variable to store the file handle
//2 = file name. If the path is relative, the file is open in accordance with the current directory (opcode 0A99).
//3 = file open mode:
//0x6272 - opens binary file for reading
//0x72 - opens text file for reading
//0x6277 - opens binary file for writing
//0x77 - opens text file for writing

[email protected] = 0       //File handle
[email protected] = 0       //current file position
[email protected] = 0       //File size 
[email protected] = 0       //current reading letter
[email protected] = 0      //result

if
    0AAB:   file_exists "./CLEO/config.txt"
then
    0A9A: [email protected] = openfile "./CLEO/config.txt" mode 0x6272  //0x6272 - opens binary file for reading
    0A9C: [email protected] = file [email protected] size
    
    for [email protected] = 1 to [email protected]   //for(int i=1; i<=n;++i)  
        wait 0 
        0A9D: readfile [email protected] size 1 to [email protected]                 // Read current letter  
    
        if and
            [email protected] > 47                                   //the value from 48 to 57 is ASCII for numbers (0 - 9), outside this range, it's not number
            58 > [email protected] 
        then                                          //If it's number, then put this number into result
            0012: [email protected] *= 10 
            005A: [email protected] += [email protected] // (int) 
            000E: [email protected] -= 48                            //48 is the ASCII for 0                                 
        end
    end

    0A9B: closefile [email protected]
end 
//----------------Complete getting value from the file ----------

//Check if there is value in the result
if
    [email protected] > 0
then
    //Do anything with the result [email protected] here
end

With this code, the format of the file doesn't require quotation mark, the file `config.txt` just needs to contains the number (for example: 69). The code will read the file base on the file size thus it will never loop forever.

Share this post


Link to post
Share on other sites
ZAZ

Nice job 123iamking 🍪

would be cool if you enhance it, so that you can read more than one line

Edited by ZAZ

Share this post


Link to post
Share on other sites
123iamking
On 11/17/2018 at 10:27 PM, ZAZ said:

would be cool if you enhance it, so that you can read more than one line

Reading multi-line is simple.

for [email protected] = 1 to [email protected]   //for(int i=1; i<=n;++i)  
        wait 0 
        0A9D: readfile [email protected] size 1 to [email protected]                 // Read current letter  
    
        //...
  		
  		if
  			[email protected] == 10								//10 is the ASCII for new line
  		then
  			//This is new line, you know what to do here...
  		end
                                           
        //...
end

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • 1 User Currently Viewing
    0 Members, 0 Anonymous, 1 Guest

×

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.