1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still haven't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Lead your forces to battle in the 15th Techtree Contest. The call is yours, commander!
    Dismiss Notice
  4. The reforging of the races is complete. Come see the 14th Techtree Contest Results.
    Dismiss Notice
  5. It's time to choose your horse in the race - the 32nd Modeling Contest Poll is up!
    Dismiss Notice
  6. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[JASS] Arrays as Parameters

Discussion in 'Triggers & Scripts' started by puggsoy, May 17, 2011.

  1. puggsoy

    puggsoy

    Joined:
    May 13, 2011
    Messages:
    213
    Resources:
    0
    Resources:
    0
    Well, I don't know exactly what the problem is, I've got a function that doesn't work. I want to compare two loops, but since I only want to run one each time I test, I put each of them in a function so that I simply turn the call to a function into a comment (since JASS doesn't support multi-line comments).

    Anyway, here's my function:
    Code (vJASS):

    function PlayMidis takes string h string o returns nothing
        local integer i = 0
        loop
            exitwhen i > 1

            if GetLocalPlayer() == Player(i) then
                if GetPlayerRace(Player(i)) == RACE_HUMAN then
                    call PlayMusic(h[GetRandomInt(1, 4)])
                elseif GetPlayerRace(Player(i)) == RACE_ORC then
                    call PlayMusic(o[GetRandomInt(1, 4)])
                endif
            endif
            set i = i + 1
        endloop
    endfunction
     


    And here's my call to the function (which is inside the main trigger function):
    Code (vJASS):

    call PlayMidis(HumanMusic, OrcMusic)
     


    HumanMusic
    and
    OrcMusic
    are both valid Arrays, I've used them before.

    I'm using JASS NewGen, and I'm getting a heap of errors, but I think most of them are based on the parameters in my function. So I want to know, can functions in JASS handle arrays as arguments? Or do I have to just write the function declaration differently?

    If this isn't the problem, I'd be glad to give a full list of errors (both from the WE and NewGen) and my full trigger code, if you need it.
     
  2. dardas

    dardas

    Joined:
    Sep 12, 2008
    Messages:
    649
    Resources:
    0
    Resources:
    0
    well, it cannot receive arrays, but it can receive structs.
    a struct is actually an array of integers, just make string values in the struct, and use then create them before giving the function the data, then using the struct data inside the function it-self.
     
  3. puggsoy

    puggsoy

    Joined:
    May 13, 2011
    Messages:
    213
    Resources:
    0
    Resources:
    0
    OK, well I checked out what structs are and I think I got it. So, if I were to declare a struct like this:
    Code (vJASS):

    struct music
        string array musicArray[4]
    endstruct
     


    And created my structs like this (inside a function):
    Code (vJASS):

    local music HumanMusic = music.create()
    local music OrcMusic = music.create()
     


    Then I could declare my function like this instead:
    Code (vJASS):

    function PlayMidis takes music h music o returns nothing
     


    Of course, I would set the values of
    musicArray
    in each of my structs. So would this work?
     
  4. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    why not just use four string members for the array than using an array as a member of the struct? and its still an array so you might still not be able to use it...

    anyway, you could just do this
    Code (vJASS):

    //and oh, you also forgot the , between the two strings
    function PlayMidis takes string h, string o returns nothing
        local integer i = 0
        local string index
        loop
            exitwhen i > 1

            if GetLocalPlayer() == Player(i) then
                if GetPlayerRace(Player(i)) == RACE_HUMAN then
                    set index = h[GetRandomInt(1, 4)]
                    call PlayMusic(index)
                elseif GetPlayerRace(Player(i)) == RACE_ORC then
                    set index = o[GetRandomInt(1, 4)]
                    call PlayMusic(index)
                endif
            endif
            set i = i + 1
        endloop
    endfunction
     
     
  5. puggsoy

    puggsoy

    Joined:
    May 13, 2011
    Messages:
    213
    Resources:
    0
    Resources:
    0
    Holy moly, I bet that missing comma is a major factor in this scenario. I'm so used to the weirdness of JASS (compared to other languages) that I totally overlooked the fact of having to put a comma to separate parameters.
    I'll put that in for sure, but let me see now... are you saying that I can still use my arrays, but just use
    string
    instead of
    array
    in my function declaration? Hmm... I'll see if it works.

    Again, thanks for that comma thing, +rep for that alone.
     
  6. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    On my example:
    I mean since you cannot seem to pass arrays as a parameter, just retrieve the value into a variable and use the variable (instead of retrieving it directly inside the function call)... it might work...

    on the struct:
    just separate the strings since its still an array, you won't still probably be able to use it directly... but using a struct is unneeded for this (if my other example works)
     
  7. puggsoy

    puggsoy

    Joined:
    May 13, 2011
    Messages:
    213
    Resources:
    0
    Resources:
    0
    Oh, I get it. Well, I'll give it a go, and if it doesn't work I'll just use structs. I'll be sure to get it to work one way or another.

    Anyway, I'm just using functions to test loops. Once I decide which one to use I'll just put everything in one function, where I can use arrays like normal.