Jump to content

[SA] Declare an array?


EAZYJ

Recommended Posts

Hello guys,

 

Here's what I'm trying to do, but I'm not very familiar with arrays yet. I wanted to make various spawn locations where enemies would come out of randomly:

 

Quote

 

$ENEMY_SPAWN_X[1] = 2498.4067 
$ENEMY_SPAWN_Y[1] = -1642.39
$ENEMY_SPAWN_Z[1] = 15.8359
$ENEMY_SPAWN_A[1] = 175.0

 

$ENEMY_SPAWN_X[2] = 2513.626 
$ENEMY_SPAWN_Y[2] = -1650.3113
$ENEMY_SPAWN_Z[2] = 13.3557
$ENEMY_SPAWN_A[2] = 135.0

 

$ENEMY_SPAWN_X[3] = 2524.5352
$ENEMY_SPAWN_Y[3] = -1658.4711
$ENEMY_SPAWN_Z[3] = 14.4935
$ENEMY_SPAWN_A[3] = 90.0

 

etc...

 

 

I have made the script to make them spawn already, now how do I declare that as an array because I get this message from Sanny Builder whenever I compile:

Quote

"Variable $ENEMY_SPAWN_X is not declared as an array"

 

The error is at this line:

Quote

80C2:  not sphere_onscreen $ENEMY_SPAWN_X[3@] $ENEMY_SPAWN_Y[3@] $ENEMY_SPAWN_Z[3@] radius 2.0

 


Thanks for any help.


 

Link to comment
Share on other sites

The format shown below is required when using variables in the array index. In this example 8 is the number of elements in the array, and f denotes a float. 

80C2:  not sphere_onscreen $ENEMY_SPAWN_X(3@,8f) $ENEMY_SPAWN_Y(3@,8f) $ENEMY_SPAWN_Z(3@,8f) radius 2.0

 

If you do not allocate these global variables carefully a cleo script using these arrays will severely corrupt your global variables. The array elements will bypass the protection Sanny usually offers to critical named CustomVariables. 

 

Arrays are tough to code in cleo scripts. Main.scm "owns" all global variables, and the 32 local variables available to cleo scripts doesn't leave much room for arrays of any size. Can you code this as a custom mission?

 

Added: The usual solution to conserving variable usage in a situation like yours is to use the random number with a jumptable/switch case format to assign the values to only 4 variables as needed.

 

Note: Local variables can be assigned custom names using the CONST..END construct. Inventing custom global variable names is very likely to cause corruption to saved data.

Edited by OrionSR
Link to comment
Share on other sites

I'm sorry for the intrusion. 
We could create arrays in cleo differently (without the use of global variables). It's quite safe way and pretty much popular by now:

Spoiler
0AC6: 31@ = label @coordArray pointer

while true
    wait 0
    0209: 1@ = random_int_in_ranges 0 3 //  0, 1, 2
    1@ *= 4 //  X, Y, Z, A ---> 4 elements 
    1@ *= 4 //  float ---> size 4
    0085: 0@ = 31@  //  coordArray
    005A: 1@ += 0@
    1@ += 0
    0A8D: 2@ = read_memory 1@ size 4 virtual_protect 0
    1@ += 4
    0A8D: 3@ = read_memory 1@ size 4 virtual_protect 0
    1@ += 4
    0A8D: 4@ = read_memory 1@ size 4 virtual_protect 0
    1@ += 4
    0A8D: 5@ = read_memory 1@ size 4 virtual_protect 0
    //  example
    if 0248:   model #MALE01 available
    then
        if 856D: NOT  actor 6@ defined
        then
            009A: 6@ = create_actor_pedtype 4 model #MALE01 at 2@ 3@ 4@
            0173: set_actor 6@ Z_angle_to 5@
            01C2: remove_references_to_actor 6@
        end
    else
        0247: load_model #MALE01
        //  038B: load_requested_models
    end
end


:coordArray
hex
  82 26 1C 45     //    0x451C2682  //  2498.4067   //  $ENEMY_SPAWN_X[0]   0   
  7B 4C CD C4     //    0xC4CD4C7B  //  -1642.39    //  $ENEMY_SPAWN_Y[0]   1   
  D9 5F 7D 41     //    0x417D5FD9  //  15.8359     //  $ENEMY_SPAWN_Z[0]   2   
  00 00 2F 43     //    0x432F0000  //  175.0       //  $ENEMY_SPAWN_A[0]   3   
                                                                    
  04 1A 1D 45     //    0x451D1A04  //  2513.626    //  $ENEMY_SPAWN_X[1]   4   
  F6 49 CE C4     //    0xC4CE49F6  //  -1650.3113  //  $ENEMY_SPAWN_Y[1]   5   
  F2 B0 55 41     //    0x4155B0F2  //  13.3557     //  $ENEMY_SPAWN_Z[1]   6   
  00 00 07 43     //    0x43070000  //  135.0       //  $ENEMY_SPAWN_A[1]   7   
                                                                    
  90 C8 1D 45     //    0x451DC890  //  2524.5352   //  $ENEMY_SPAWN_X[2]   8   
  13 4F CF C4     //    0xC4CF4F13  //  -1658.4711  //  $ENEMY_SPAWN_Y[2]   9   
  60 E5 67 41     //    0x4167E560  //  14.4935     //  $ENEMY_SPAWN_Z[2]   10  
  00 00 B4 42     //    0x42B40000  //  90.0        //  $ENEMY_SPAWN_A[2]   11  
end

 

or with the direct input of float data into the hex block (same method with a slightly different approach):

Spoiler
0AC6: 31@ = label @coordArray pointer

while true
    wait 0
    0085: 0@ = 31@
    0@ += 0
    0A8C: write_memory 0@ size 4 value 2498.4067 virtual_protect 0
    0@ += 4                                      
    0A8C: write_memory 0@ size 4 value -1642.39 virtual_protect 0
    0@ += 4                                      
    0A8C: write_memory 0@ size 4 value 15.8359 virtual_protect 0
    0@ += 4                                      
    0A8C: write_memory 0@ size 4 value 175.0 virtual_protect 0
    0@ += 4                                      
    0A8C: write_memory 0@ size 4 value 2513.626 virtual_protect 0
    0@ += 4                                      
    0A8C: write_memory 0@ size 4 value -1650.3113 virtual_protect 0
    0@ += 4                                      
    0A8C: write_memory 0@ size 4 value 13.3557 virtual_protect 0
    0@ += 4                                      
    0A8C: write_memory 0@ size 4 value 135.0 virtual_protect 0
    0@ += 4                                      
    0A8C: write_memory 0@ size 4 value 2524.5352 virtual_protect 0
    0@ += 4                                      
    0A8C: write_memory 0@ size 4 value -1658.4711 virtual_protect 0
    0@ += 4                                      
    0A8C: write_memory 0@ size 4 value 14.4935 virtual_protect 0
    0@ += 4                                      
    0A8C: write_memory 0@ size 4 value 90.0 virtual_protect 0
    
    
    0209: 1@ = random_int_in_ranges 0 3 //  0, 1, 2
    1@ *= 4 //  X, Y, Z, A ---> 4 elements
    1@ *= 4 //  float ---> size 4         
    0085: 0@ = 31@  //  coordArray
    005A: 1@ += 0@
    1@ += 0
    0A8D: 2@ = read_memory 1@ size 4 virtual_protect 0
    1@ += 4
    0A8D: 3@ = read_memory 1@ size 4 virtual_protect 0
    1@ += 4
    0A8D: 4@ = read_memory 1@ size 4 virtual_protect 0
    1@ += 4
    0A8D: 5@ = read_memory 1@ size 4 virtual_protect 0
    //  example:
    if 0248:   model #MALE01 available
    then
        if 856D: NOT  actor 6@ defined
        then
            009A: 6@ = create_actor_pedtype 4 model #MALE01 at 2@ 3@ 4@
            0173: set_actor 6@ Z_angle_to 5@
            01C2: remove_references_to_actor 6@
        end
    else
        0247: load_model #MALE01
        //  038B: load_requested_models
    end
end


:coordArray
hex
    //  user inputed data
end

 

This way we could use arrays for storing any type of data including handles of peds, vehicles, objects... all kinds of possibilities.
... just a thought. 
 

Link to comment
Share on other sites

Thanks a lot for the detailed explanations guys! Although I don't really have a high level in coding to get a hold of all of it but I can see the points you're making. Now I should mention that I am scripting my mission on the main.scm and I don't think I'll be having any cleo using an array but it's always good to know.

 

So would it be safer to switch to local variables then? By the way, another question: can I use those variables for the actors as well?

 

Here's what I'm trying to achieve (this is sketchy but it's mostly to give you an idea)

Quote

:SPAWNING_ON
wait 0
if
$SPAWNING == 1
jf @SPAWNING_ADJUST     
if
not $TOTAL_ENEMIES > 10
jf @SPAWNING_ADJUST
0209: 3@ = random_int_in_ranges 1 11
if
80C2:  not sphere_onscreen $ENEMY_SPAWN_X(3@,11f) $ENEMY_SPAWN_Y(3@,11f) $ENEMY_SPAWN_Z(3@,11f) radius 2.0
jf @SPAWNING_ADJUST                    
      
$ENEMY(4@,15i) = Actor.Create(Mission1, #SWAT, $ENEMY_SPAWN_X(3@,11f), $ENEMY_SPAWN_Y(3@,11f), $ENEMY_SPAWN_Z(3@,11f))
Actor.Angle($ENEMY(4@,15i)) = $ENEMY_SPAWN_A(3@,11f)
05E2: AS_actor $ENEMY(4@,15i) kill_actor $PLAYER_ACTOR
04C4: store_coords_to $TEMPVAR_FLOAT_1 $TEMPVAR_FLOAT_2 $TEMPVAR_FLOAT_3 from_actor $PLAYER_ACTOR with_offset 0.0 0.0 0.0
0603: AS_actor $ENEMY(4@,15i) goto_point_any_means $TEMPVAR_FLOAT_1 $TEMPVAR_FLOAT_2 $TEMPVAR_FLOAT_3 mode 6 use_car -1

 

4@ += 1

$TOTAL_ENEMIES += 1
 


:SPAWNING_ADJUST
wait 0
if
Actor.Dead($ENEMY(4@,15i))
jf @SPAWNING_ON
Actor.RemoveReferences($ENEMY(4@,15i))
4@ += -1
$TOTAL_ENEMIES += -1  
jump @SPAWNING_ON

 

Link to comment
Share on other sites

1 hour ago, EAZYJ said:

Now I should mention that I am scripting my mission on the main.scm

Well, mentioning it a bit sooner would have saved you warning, but objections withdrawn. You own the globals in your custom main.

 

Link to comment
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
  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

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