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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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