[Coding Help] How do I permanently change a variable through functions?

Started by Kernel64, March 14, 2010, 10:13:58 PM

Previous topic - Next topic

Kernel64

How do I change a variable that's been passed as a parameter? Say, I have:

void func (string OrderList, int OrderCount[j], int OrderPrio[k]);

how do I actually change the values of those arrays? I don't know how to make them change in that function and have the changes happen after the function is called.

Also, is that a valid function? Assigning arrays as arguments like that? Is that how to do it?

Thanks in advance.

Aeg1s

Unfortunately you can't do either :/


What are you trying to do with this function?


SPmac

IF pointers are supported ...I don't know if they are, yet to play with them

void func (string OrderList, int *OrderCount, int *OrderPrio)

Say you want to pass this array

int[x] IntArr;

pass like this


//pointer to first element in the array
void func (..., IntArr, ...)


or for other elements in the array


//pointer to elements at position x
void func (..., IntArr + x, ...)

Kernel64

Oh, nice.

So if, say, I have a function:

void ShuffleOrders(string *Orderlist, int *OrderCount)

and I pass: string[25] Orders, int Count[25]...

then access them in the function and swap things around,

I can use something in the function like:
OrderlistTemp = Orderlist[0];
Orderlist[0] = Orderlist[1];
Orderlist[1] = OrderlistTemp;

and have the values actually swap when I access string[25] Orderlist in another function.

Is this correct?

Aeg1s, I'm trying to see if I can make running orders exactly as I lay them out within the blocks. Say, I want:
+4 Drones,
+1 Ovie,
+1 Pool

I want to have them in a queue, and get the first one executed, then the next, then the next without keeping an eye on them so closely like what is happening now.

Even so, I'm still not sure if this will be applicable for every order, since some train or build orders can be queued in another building and the other in another and so forth.

SPmac

So pointers do work...?

and... Yes, it will hold the value across function calls. But if you're not too keen on pointers, I would suggest getting more familiar w/ them outside of your AI project. Cuz as we all know, constantly loading and reloading SC and moving files in and out of MPQ editors is a pain in the ass, and pointers can be hairy to work w/ at first. I'd suggest playing around w/ em in C w/ a compiler.

..but what you got there works. In short.. just make sure your not assigning to or dereferencing values out of your arrays range.

Kernel64

Thanks guys. I'm getting all confused at the moment with pointers.

Astazha

These might help:


http://www.sc2mapster.com/api-docs/galaxy-language/arrays/
http://www.sc2mapster.com/api-docs/galaxy-language/structs/


They're not specifically about pointers, but pointers are addressed within them with examples.  The structures one is really good - pay close attention to the example of what not to do.

Xwoa

Quote from: Aeg1s on March 14, 2010, 10:55:43 PM
Unfortunately you can't do either :/

You can certainly do so, the parameter just needs to be passed in by reference.

Kernel64