• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Arrays as Parameters

Status
Not open for further replies.
Level 7
Joined
May 13, 2011
Messages
310
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:
JASS:
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):
JASS:
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.
 
Level 11
Joined
Sep 12, 2008
Messages
657
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.
 
Level 7
Joined
May 13, 2011
Messages
310
OK, well I checked out what structs are and I think I got it. So, if I were to declare a struct like this:
JASS:
struct music
    string array musicArray[4]
endstruct

And created my structs like this (inside a function):
JASS:
local music HumanMusic = music.create()
local music OrcMusic = music.create()

Then I could declare my function like this instead:
JASS:
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?
 
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
JASS:
//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
 
Level 7
Joined
May 13, 2011
Messages
310
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.
 
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)
 
Level 7
Joined
May 13, 2011
Messages
310
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.
 
Status
Not open for further replies.
Top