DarkBlizz

Game On => Land of AI => STARCRAFT II: WINGS OF LIBERTY => AI Development => Topic started by: Kernel64 on March 14, 2010, 10:13:58 PM

Title: [Coding Help] How do I permanently change a variable through functions?
Post by: Kernel64 on March 14, 2010, 10:13:58 PM
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.
Title: Re: [Coding Help] How do I permanently change a variable through functions?
Post by: Aeg1s on March 14, 2010, 10:55:43 PM
Unfortunately you can't do either :/


What are you trying to do with this function?
Title: Re: [Coding Help] How do I permanently change a variable through functions?
Post by: Astazha on March 15, 2010, 12:41:16 AM
What about using pointers?
Title: Re: [Coding Help] How do I permanently change a variable through functions?
Post by: SPmac on March 15, 2010, 01:29:30 AM
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, ...)
Title: Re: [Coding Help] How do I permanently change a variable through functions?
Post by: Kernel64 on March 15, 2010, 05:45:00 AM
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.
Title: Re: [Coding Help] How do I permanently change a variable through functions?
Post by: SPmac on March 15, 2010, 06:21:47 AM
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.
Title: Re: [Coding Help] How do I permanently change a variable through functions?
Post by: Kernel64 on March 16, 2010, 06:07:50 AM
Thanks guys. I'm getting all confused at the moment with pointers.
Title: Re: [Coding Help] How do I permanently change a variable through functions?
Post by: Astazha on March 19, 2010, 03:11:55 PM
These might help:


http://www.sc2mapster.com/api-docs/galaxy-language/arrays/ (http://www.sc2mapster.com/api-docs/galaxy-language/arrays/)
http://www.sc2mapster.com/api-docs/galaxy-language/structs/ (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.
Title: Re: [Coding Help] How do I permanently change a variable through functions?
Post by: Xwoa on March 19, 2010, 04:40:06 PM
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.
Title: Re: [Coding Help] How do I permanently change a variable through functions?
Post by: Kernel64 on March 19, 2010, 06:03:36 PM
Sweet!

I'll be working with these in a few.
Title: Re: [Coding Help] How do I permanently change a variable through functions?
Post by: Chriamon on April 05, 2010, 02:54:29 PM
Disregard this