April 10, 2021, 06:25:00 PM

Author Topic: [Coding Help] How do I permanently change a variable through functions?  (Read 1061 times)

Kernel64

  • Sr. Member
  • ****
  • Posts: 207
    • View Profile
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

  • Resident
  • **
  • Posts: 39
    • View Profile
Unfortunately you can't do either :/


What are you trying to do with this function?

Astazha

  • Member
  • ***
  • Posts: 78
    • View Profile
What about using pointers?

SPmac

  • New Member
  • *
  • Posts: 9
    • View Profile
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
 
Code: [Select]
int[x] IntArr;
pass like this
 
Code: [Select]

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

or for other elements in the array
 
Code: [Select]

//pointer to elements at position x
void func (..., IntArr + x, ...)
« Last Edit: March 15, 2010, 02:31:23 AM by SPmac »

Kernel64

  • Sr. Member
  • ****
  • Posts: 207
    • View Profile
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

  • New Member
  • *
  • Posts: 9
    • View Profile
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

  • Sr. Member
  • ****
  • Posts: 207
    • View Profile
Thanks guys. I'm getting all confused at the moment with pointers.

Astazha

  • Member
  • ***
  • Posts: 78
    • View Profile
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

  • New Member
  • *
  • Posts: 2
    • View Profile
Unfortunately you can't do either :/

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

Kernel64

  • Sr. Member
  • ****
  • Posts: 207
    • View Profile
Sweet!

I'll be working with these in a few.

Chriamon

  • Resident
  • **
  • Posts: 28
    • View Profile
Re: [Coding Help] How do I permanently change a variable through functions?
« Reply #10 on: April 05, 2010, 03:54:29 PM »
Disregard this