[Snippet] Additional Vars

I just stumped upon Deji's SuperVars and this was mentioned in readme

I originally created the script 'ExtraVars' which demonstrated
a way to extend the limitations on variable usage by directly  
accessing memory using var arrays. A little example:
   1@(31@,1i)

...

However, this method wasn't without fault. I started noticing  
crashes caused by the index falling to certain values and      
causing the game to write to a bad section of memory.

I don't know what exactly it's means 

but i make a quick test by trying make 1024 additional vars and found no problem. So, i've decided to share this. Might come in handy for coders. Removing limitations of vars

Code:
0A90: 28@ = 4 * 1024 // int
0AC7: 29@ = var 0@ offset            // get the pointer to the last usable variable
0AC8: 30@ = allocate_memory_size 28@
0A8F: 31@ = 30@ - 29@                // get the offset from variable 0@ to the memory
0016: 31@ /= 4                      // divide by array size to get the index
0085: 28@ = 31@ // (int)
0006: 1@ = 0
0006: 2@ = 0    
//fill extra var 0@(31@,4i) - 1023@(31@,4i)
for 1@ = 0 to 1023 step 1
    0A90: 2@ = 1@ * 2 // int
    0085: 0@(28@,4i) = 2@ // (int)   
    0AD1: show_formatted_text_highpriority "[Offset: %d]~n~[%d@: %d]" time 500 31@ 1@ 0@(28@,4i)
    wait 0
    000A: 28@ += 1
end
//compare variable and value
while true
    wait 0
    0AD1: show_formatted_text_highpriority "[Offset: %d]~n~[0@: %d][193@: %d][298@: %d]~n~[518@: %d][763@: %d][888@: %d]~n~[954@: %d][1023@: %d]" time 2000 31@ 0@(31@,4i) 193@(31@,4i) 298@(31@,4i) 518@(31@,4i) 763@(31@,4i) 888@(31@,4i) 954@(31@,4i) 1023@(31@,4i)
end
 

Ninja FTW

Active member
Joined
Aug 11, 2017
Messages
61
Reaction score
0
Location
Saturn
You can also try to use CLEO global variables (0AB3, 0AB4) by DK
 

Ninja FTW

Active member
Joined
Aug 11, 2017
Messages
61
Reaction score
0
Location
Saturn
Problem is:

if the same variable found in a script then i think they might crash the game by colliding with each other. The best solution is to simulate two FUNC like 0AB3 and 0AB4 but remember for our MEmoryThread


Code:
{ something like in programming languages , the comment at the end of the function label
will be printed on the list of labels that 
Sanny generates when you begin typing one. It will also be placed
in the editor when you select it from that list. 
Just a simple little thing to remind you what the function does 
and how many parameters it uses.}

:set{passed_value,var_number,var_size}  
0AC6: 3@ = get_label_pointer @memory
int 1@ *= 4   //you can put a typedef in some cases before a variable instead of typing the opcode of that operation.
int 3@ += 1@
Memory.Write(3@,2@,0@,0)
ret 0

:get{var_number,var_size}
0AC6: 3@ = get_label_pointer @memory
int 0@ *= 4
int 3@ += 0@
2@ = Memory.Read(3@,1@,0)
ret 1 2@

:memory  // extra vars , whatever number of vars you like... the only limit is your RAM.
hex
00 00 00 00  // var 0
00 00 00 00  // var 1
00 00 00 00  // var 2
00 00 00 00  // var 3
00 00 00 00  // var 4
00 00 00 00  // var 5
00 00 00 00  // var 6
00 00 00 00  // var 7
00 00 00 00  // var 8
00 00 00 00  // var 9
00 00 00 00  // var 10
00 00 00 00  // var 11
00 00 00 00  // var 12
00 00 00 00  // var 13
00 00 00 00  // var 14
00 00 00 00  // var 15
00 00 00 00  // var 16
00 00 00 00  // var 17
00 00 00 00  // var 18
00 00 00 00  // var 19
00 00 00 00  // var 20
00 00 00 00  // var 21
00 00 00 00  // var 22
00 00 00 00  // var 23
00 00 00 00  // var 24
00 00 00 00  // var 25
00 00 00 00  // var 26
00 00 00 00  // var 27
00 00 00 00  // var 28
00 00 00 00  // var 29
00 00 00 00  // var 30
end
Here's an example of using:


Code:
{$CLEO}

0000:

// some examples

0@ = 5
0AB1: @set 3 passed_value 0@ var_number 0 var_size 4  // value of 0@ is stored in var 0 

0AB1: @get 2 var_number 3 var_size 4 receiver 1@ // value of  var 3 is received in 1@

1@ = 4
0AB1: @set 3 passed_value 1@ var_number 2 var_size 4  // value of 1@ is stored in var 2

0AB1: @get 2 var_number 30 var_size 4 receiver 0@ // value of  var 30 is received in 0@

{ in case of passing integer variables like 0@ then var_size must be always 4 bytes  
, @s  strings are 8 bytes ( 2 vars)
, @v  long strings are 16 bytes (4 vars )
so it's just like Cleo variables when you use 0@v , 
you know that 1@,2@,3@ will be used to store the 16 bytes value}



for 1@ = 0 to 30
    0A90: 2@ = 1@ * 2 // int // the value is variable index * 2
    0AB1: @set 3 passed_value 2@ var_number 1@ var_size 4 // storing value of 2@ to variables starting from var0 to var30   
end


while not key.Pressed(0x54)
wait 0
end

for 1@ = 0 to 30
    wait 500
    0AB1: @get 2 var_number 1@ var_size 4 receiver 0@
    0ACE: print_help_formatted "Value of var %i is %i" 1@ 0@     
end               

end_custom_thread
 

Husnain

Well-known member
Joined
May 20, 2016
Messages
228
Reaction score
9
Location
Mars
So, basically i can add more variables? Holy!
 

Ninja FTW

Active member
Joined
Aug 11, 2017
Messages
61
Reaction score
0
Location
Saturn
@0B36 - Keep PMing, little boy. I'm not stupid and idiot like you who is going to fight with a baby. Go and observe at the rules, once again.


Husnain said:
So, basically i can add more variables? Holy!

Yes, if you understand them clearly.
 

Husnain

Well-known member
Joined
May 20, 2016
Messages
228
Reaction score
9
Location
Mars
@Nin FTW - He's mentally disordered and racist. Just ignore him. Someone did not told him that how appreciation is a real thing.

Edit: This shit really rocks with upto 31@ variables xd.
 

HowEnCokkien

Well-known member
Joined
Jul 9, 2017
Messages
307
Reaction score
9
Husnain said:
@Nin FTW - He's mentally disordered and racist. Just ignore him. Someone did not told him that how  appreciation is a real thing.

Edit: This shit really rocks with upto 31@ variables xd.

ninja is your alt acc btw haha lmao
 

Husnain

Well-known member
Joined
May 20, 2016
Messages
228
Reaction score
9
Location
Mars
HowEnCokkien said:
Husnain said:
@Nin FTW - He's mentally disordered and racist. Just ignore him. Someone did not told him that how  appreciation is a real thing.

Edit: This shit really rocks with upto 31@ variables xd.

ninja is your alt acc btw haha lmao
lol rlly?
 

Parazitas

God
Joined
Jan 2, 2017
Messages
3,116
Solutions
5
Reaction score
881
Location
Lithuania
PHP:
:Read // call @Read 1 Var 0@ ret 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

        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@

:Write // call @Write 2 Var 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
 
Top