[SNIPPET] Expanding variable

Piece of code which allows you to save more numbers without storing them in external files.

Edit: Tested and changed into simpler and actually working code
Code:
alloc 0@ 512 //array pointer
1@ = 0 //buffer

0AB1: @IntArray_Write 3 arrayPointer 0@ index 0 val 123
0AB1: @IntArray_Write 3 arrayPointer 0@ index 1 val 9919
0AB1: @IntArray_Write 3 arrayPointer 0@ index 2 val 77
0AB1: @IntArray_Write 3 arrayPointer 0@ index 3 val 5
0AB1: @IntArray_Write 3 arrayPointer 0@ index 4 val 3661
0AB1: @IntArray_Write 3 arrayPointer 0@ index 5 val 91
0AB1: @IntArray_Write 3 arrayPointer 0@ index 6 val 7255
0AB1: @IntArray_Write 3 arrayPointer 0@ index 7 val 525


0AB1: @IntArray_Read arrayPointer 0@ index 0 _returned 1@
//do something with 1@ which becomes 123
0AB1: @IntArray_Read arrayPointer 0@ index 1 _returned 1@
//do something with 1@ which becomes 9919
0AB1: @IntArray_Read arrayPointer 0@ index 2 _returned 1@
//do something with 1@ which becomes 77
0AB1: @IntArray_Read arrayPointer 0@ index 3 _returned 1@
//do something with 1@
0AB1: @IntArray_Read arrayPointer 0@ index 4 _returned 1@
//do something with 1@
0AB1: @IntArray_Read arrayPointer 0@ index 5 _returned 1@
//do something with 1@
0AB1: @IntArray_Read arrayPointer 0@ index 6 _returned 1@
//do something with 1@
0AB1: @IntArray_Read arrayPointer 0@ index 7 _returned 1@
//do something with 1@


//0AB1: @IntArray_Write 3 arrayPointer 0@ index 1@ val 2@
:IntArray_Write
0012: 1@ *= 4
005A: 1@ += 0@  // (int)
0A8C: write_memory 1@ size 4 value 2@ virtual_protect 1
0AB2: ret 0

//0AB1: @IntArray_Read arrayPointer 0@ index 1@ _returned 2@
:IntArray_Read
0012: 1@ *= 4
005A: 1@ += 0@  // (int)
0A8D: 2@ = read_memory 1@ size 4 virtual_protect 1
0AB2: ret 1 2@
This way by using only 2 variables it's possible to store a lot of data
 

0x32789

Expert
Joined
May 26, 2014
Messages
849
Reaction score
51
Location
LongForgotten <-> 0x32789
RE: Expanding variable

just save the variables and use them wisely, because they are one of the most important parts of the code if you are making something big, change variables on snippets so they dont use your global variables
0-31 (32) variables are always enough
 

Parazitas

God
Joined
Jan 2, 2017
Messages
3,115
Solutions
5
Reaction score
879
Location
Lithuania
Bump.
Highly recommended use it more wildly, it allows you break limits of max usable variables.
 

ajom

Well-known member
Joined
Apr 14, 2020
Messages
389
Solutions
2
Reaction score
268
Location
Pluto
Self Memory Expanding Variable Read/Write:
  1. Can store unlimited number of custom variables(just don't overdo it to avoid memory fragmentation fault due to large allocated memory size). I already tested this snippet on 550 custom variables.
  2. No need to allocate memory with fixed size.
  3. Built-In Self Memory Expansion System
    • Dynamically Allocate memory to accomodate all custom variables
    • Smarter Memory Allocation with size as small as possible
  4. Built-in Variable Index Validity Checker
  5. Bug Free
PHP:
:SelfExpandingVariableRead // 0AB1: @SelfExpandingVariableRead 1 _VariableIndex 0@ _ReturnValue 31@
    if 0@ >= 0 // valid index
    then
        0AC6: 31@ = label @SelfExpandingVariableRegion pointer
        0A8E: 30@ = 31@ + 4
        0A8D: 29@ = read_memory 30@ size 4 virtual_protect 0 // Get Allocated Memory Address

        0A8E: 28@ = 0@ + 1
        0A8D: 27@ = read_memory 31@ size 4 virtual_protect 0 // Get Total number of variable index
        if 002D: 27@ >= 28@ // index is in bounds
        then
            0@ *= 4 // 4 bytes per variable index
            005A: 29@ += 0@  // custom variable index pointer
            0A8D: 29@ = read_memory 29@ size 4 virtual_protect 0 // read custom variable's value
        else 29@ = 0 // default value when out of bounds. change it in case you want a different default value
        end
    end
0AB2: ret 1 29@

:SelfExpandingVariableWrite // 0AB1: @SelfExpandingVariableWrite 2 _VariableIndex 0@ _Value 1@
    if 0@ >= 0 // valid index
    then
        0AC6: 31@ = label @SelfExpandingVariableRegion pointer
        0A8E: 30@ = 31@ + 4
        0A8D: 29@ = read_memory 30@ size 4 virtual_protect 0 // Get Allocated Memory Address

            // self memory expansion system
        0A8E: 28@ = 0@ + 1
        0A8D: 27@ = read_memory 31@ size 4 virtual_protect 0 // Get Total number of variable index
        if 001D: 28@ > 27@ // index is out of bounds
        then // expand memory region
            0A8C: write_memory 31@ size 4 value 28@ virtual_protect 0 // Save New Total number of variable index

            0085: 26@ = 29@ // old allocated memory address
            0A90: 29@ = 28@ * 4
            0AC8: 29@ = allocate_memory_size 29@
            0A8C: write_memory 30@ size 4 value 29@ virtual_protect 0 // Save New Allocated Memory Address

            if 26@ <> 0 // has existing allocated memory
            then
                if 27@ > 0 // valid old index
                then // migrate old memory data to the new memory region
                    0085: 25@ = 29@
                    0085: 24@ = 26@
                    for 23@ = 1 to 27@
                        0A8D: 22@ = read_memory 24@ size 4 virtual_protect 0
                        0A8C: write_memory 25@ size 4 value 22@ virtual_protect 0
                        25@ += 4
                        24@ += 4
                    end
                end

                0AC9: free_allocated_memory 26@
            end
        end
            //

        0@ *= 4 // 4 bytes per variable index
        005A: 29@ += 0@  // custom variable index pointer
        0A8C: write_memory 29@ size 4 value 1@ virtual_protect 0 // save
    end
0AB2: ret 0

:SelfExpandingVariableRegion
hex
    00 00 00 00 // Total number of variable index
    00 00 00 00 // Allocated Memory Address
end

Custom Variable Debugger:
  1. Run the game with this script
  2. Lets this script log all the variable contents for 10 seconds
  3. exit the game
  4. View the chatlog and inspect the results
PHP:
{$CLEO}
0000:

repeat
    wait 0
until 0AFA:  is_samp_available

while true
    wait 100

    0209: 1@ = random_int_in_ranges 35 823923
    0AB1: @SelfExpandingVariableWrite 2 _VariableIndex 0@ _Value 1@
    chatmsg "updated %d into %d" -1 0@ 1@
    for 1@ = 0 to 49
        0AB1: @SelfExpandingVariableRead 1 _VariableIndex 1@ _ReturnValue 2@
        chatmsg "index:%d value:%d" -1 1@ 2@
    end
    0@++
    0B1B: 0@ %= 50
end
 
Top