Array question

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
I have a function...
JASS:

globals
     integer array myInteger
endglobals

function myFunct takes integer i returns nothing
     set myInteger[0] = i
endfunction

function myFunct2 takes nothing returns nothing
     call myFunct(737) // Random numbers
endfunction

Okay, so technically it's two functions and a global, but that's beside the point.

My problem is I'm using this in a system I'm making, but I can't have it so I need to declare the array each time.
This function (myFunct) is part of the core of the system. It's part of how it works (it's a secret project, so don't ask what it is please).

What I need is a way to set the array number (I think it's called an array index) from the function calling it.
Is it possible? If so, how would I do it?
(I have several global arrays which "myFunct" sets values to.)
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
The problem is I call myFunct a lot.
Nearly every time I call it, it will be storing in a different index.
I just need a way to define the array index from the function calling myFunct, if it's possible.
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
Yeah.... Still a little new to JASS...
Haven't quite gotten what returns are yet...

Mind sharing some details?
 

cleeezzz

The Undead Ranger.
Reaction score
268
well i still dont really get what your trying to do but

(assuming the variable arraynumber is an integer variable that increases by one each time function called)

JASS:
function myFunct takes integer i returns integer
     set arraynumber = arraynumber + 1
     set myInteger[arraynumber] = i
     return arraynumber
endfunction


then if your code, you can do

local integer b = myFunct(737)

b will now equal the array of myInteger that you set 737 to.

i dont really know why you would need this
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
That part of the system (this isn't all of it, just a small part) stores values into globals.
I have this (complete example):
JASS:

globals
       integer array myInteger
       string array myString
       string array myString2
       boolean array myBool
endglobals

function myFunct takes integer i, string s, string a, boolean b, returns nothing
       set myInteger = i.     // I realize none of these an array index
       set myString = s
       set myString2 = a
       set myBool = b
endfunction

function myFunct2 takes nothing returns nothing
       call myFunct(184, "Hello world", "Hello again", false)
       call DisplayTextToForce(GetPlayersAll(), "Variables set")
endfunction


Now, the problem is myFunct (this compiles properly when I add array indexes).
Like I said, it's going to be called a lot, so it can store data.
The problem is nearly each time it's called, it's going to use a different array index.
I just need a way to set that array index from outside of the function.
 

cleeezzz

The Undead Ranger.
Reaction score
268
JASS:
globals
       integer array myInteger
       string array myString
       string array myString2
       boolean array myBool
endglobals

function myFunct takes integer i, string s, string a, boolean b, integer arrayvalue returns nothing
       set myInteger[arrayvalue] = i.     // I realize none of these an array index
       set myString[arrayvalue] = s
       set myString2[arrayvalue] = a
       set myBool[arrayvalue] = b
endfunction

function myFunct2 takes nothing returns nothing
       call myFunct(184, "Hello world", "Hello again", false,1)
       call DisplayTextToForce(GetPlayersAll(), "Variables set")
endfunction


like that?

it would set those variables to array value 1
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
That might work... I'll need to run a syntax check and test it though, which will have to wait till tomorrow.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
It sounds like you're reinventing the wheel of structs in vJASS.

JASS:
globals
       integer array myInteger
       string array myString
       string array myString2
       boolean array myBool
endglobals

function myFunct takes integer i, string s, string a, boolean b, integer arrayvalue returns nothing
       set myInteger[arrayvalue] = i.     // I realize none of these an array index
       set myString[arrayvalue] = s
       set myString2[arrayvalue] = a
       set myBool[arrayvalue] = b
endfunction

function myFunct2 takes nothing returns nothing
       call myFunct(184, "Hello world", "Hello again", false,1)
       call DisplayTextToForce(GetPlayersAll(), "Variables set")
endfunction

If you were writing this in vJASS, the struct instance itself is the common index for all the arrayed values. Here's what the vJASS struct syntax would look like in your case:

JASS:
struct Data
    integer myInteger //notice that because these are members of the Data struct,
    string myString   //that they do not have to be declared as arrays.
    string myString2
    boolean myBool

    method myFunct takes integer i, string s, string a, boolean b returns nothing
        set this.myInteger = i
        set this.myString = s
        set this.myString2 = a
        set this.myBool = b
    endmethod
endstruct

function myFunct2 takes nothing returns nothing
    local Data d = Data.create() //d becomes 1, the common array index to all the arrays.
                               //This makes it easier to get a new index, as the indexes are recycled.
    call d.myFunct(184, "Hello world", "Hello again", false) //setting d's members
    call DisplayTextToPlayer(GetLocalPlayer(), "Variables set")
endfunction


In this case, it is perfectly acceptable to write your code the first way, but I personally prefer the second, as it is cleaner, and easier (for me) to write/understand.
 

cleeezzz

The Undead Ranger.
Reaction score
268
lol i have no idea what he needs them for so i just wrote it that way, making them into structs would just confuse him if he doesn't know it

and i was worried if something in his map depended on those arrays.

(meaning he would have to take time into learning structs and then recode his old codes to work with it)

like lets say some outside function wanted to get myInteger from the 25th instance of the struct, how would he do that?

it would be much more difficult than myInteger[25]
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
lol i have no idea what he needs them for so i just wrote it that way, making them into structs would just confuse him if he doesn't know it

He was essentially trying to group all the instances of each variable together. Exactly what a struct does. It might be confusing at first, but then again, all concepts are. I was simply trying to show him an example and perhaps enlighten him.

like lets say some outside function wanted to get myInteger from the 25th instance of the struct, how would he do that?

it would be much more difficult than myInteger[25]

If you have to ask this, you're probably not writing the code correctly (In this case, as you are not extending arrays or anything of the sort). With structs, you don't necessarily know (and it doesn't necessarily matter) what instance of the object you're working with.

However, you could easily do Data(25).myInteger. Same thing.

I wasn't trying to say anything bad about your way of doing it. I'm simply showing the OP another, possibly better way of writing the code.
 

cleeezzz

The Undead Ranger.
Reaction score
268
yep i didn't say yours was bad either xd

(ive never really found a need to extend array, so ive never done it o_O)

i mainly only use it for timer related things (so i only know how to attach)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top