Passing containers to functions

Passing containers to functions

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

Post Reply
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Passing containers to functions

Post by krogothe »

Say i have a simple but big struct like this (pseudo-codish, ignore syntax errors):

Code: Select all

struct santasbag
{
int toy1;
float toy2;
char toy3;
int reindeer1;
float sleigh;
bool claus;
int....
...and so on...
}
basically it has a lot of information in it.

Now say i have a function that will use one or two items from that struct (eg toy1 and sleigh). I could do it two ways:

Code: Select all

void Xmas::SendPresent(int var1, float var2)
{
x = var1;
y += var1 + var2;
}
santabag  gimmeprezzies;
sendpresent (gimmeprezzies.toy1,gimmeprezzies.sleigh)

Code: Select all

void Xmas::SendPresent(santabag my_bag)
{
x = my_bag.toy1;
y += my_bag.toy1 + sleigh;
}
santabag  gimmeprezzies;
sendpresent (gimmeprezzies)
would it be slower to pass the whole struct like shown in the second codebox? Its easier to use and simpler but i have some speed-critical functions and i wouldnt want to use that if it made the function run slower by loading unnecessary data.

Also, in case it IS slower, would it be too bad if santasbag only had say, 6-8 items?
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

Pass a pointer to the struct instead, problem solved. Also sending in specific values might not be the best thing. As long as you send in the global structure you neednt worry about how it works underneath with regards to the rest of the prog as long as it does what ti says it does.

Lok at how JCAI and NTAI do it.
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

right so it would work like this?

Code: Select all

void Xmas::SendPresent(*santabag my_bag) 
{ 
x = my_bag.toy1; 
y += my_bag.toy1 + sleigh; 
} 
santabag  gimmeprezzies; 
SendPresent(&gimmeprezzies);
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

no, you're accessing a pointr so you use -> and not . operator.

Code: Select all

void Xmas::SendPresent(*santabag my_bag){ 
    x = my_bag->toy1; 
    y += my_bag->toy1 + sleigh; 
} 
santabag*  gimmeprezzies = new santabag; 
Xmas* x = new Xmas;
x->SendPresent(gimmeprezzies);
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

I just thought it would work that way (using -> but passing the address of gimmeprezzies the same way)...

Thanks a lot either way!
SoftNum
Posts: 22
Joined: 28 Nov 2005, 22:07

Post by SoftNum »

Actually, technically both ways would work. But A's way is a bit more C++ friendly. Atleast, for the calling of the actual function. You would neet to use -> ... or the ponderous (*var).x -> is much better.
User avatar
mr sharpoblunto
Posts: 24
Joined: 12 Dec 2005, 03:47

Post by mr sharpoblunto »

or another way of passing a pointer to the struct using reference variables

Code: Select all

void Xmas::SendPresent(santabag &my_bag){
    x = my_bag.toy1;
    y += my_bag.toy1 + sleigh;
}

santabag  gimmeprezzies;
SendPresent(gimmeprezzies);
 
this way you don't have to worry about using the -> operator and you don't have to worry about passing the address of the struct to the function. You can pass the struct itself, and the compiler will do all the pointer referencing and dereferencing transparently.

It does exactly the same as the above examples, it just depends on the semantics you prefer. :wink:
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

but would that pass all the data in the struct, therefore making it slower?
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

hmm I couldnt use the this keyword that way
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Post by Tobi »

but would that pass all the data in the struct, therefore making it slower?
No.

it's the same as a pointer, but you use . instead of ->, and you can't make it point to something else..
User avatar
mr sharpoblunto
Posts: 24
Joined: 12 Dec 2005, 03:47

Post by mr sharpoblunto »

hmm I couldnt use the this keyword that way
the method you use to pass parameters to a function shouldn't affect your ability to use the this keyword

Code: Select all

class test
{
  public:
    void testMethod(SomeOtherClass &x);
    void testMethod1(SomeOtherClass *x);
  private:
   int someAttribute;
};

void test::testMethod(SomeOtherClass &x)
{
   this->someAttribute = x.anotherAttribute;
}

void test::testMethod1(SomeOtherClass *x)
{
   this->someAttribute = x->anotherAttribute;
}
both approaches work. and yeah, like Tobi said using an & passes a pointer to the function, but you can use the . operator instead of the -> operator inside the function, and you don't need to use the address of (&) operator when passing the struct to the function.
renrutal
Posts: 84
Joined: 28 Apr 2005, 16:45

Post by renrutal »

Be sure to use containers defined in the Standard Template Library(STL) and Boost library instead of hand-made ones, whenever it is possible:
  • They are very optimized.
  • Portable to all the important platforms.
  • Their manuals have time and space complexity descriptions of the actions you can do on them. It means you can choose which container would be less costly to the computer.
  • Support all kinds of data.
  • Build-in functions and operators you can use for all your actions.
  • Secure if used correctly(always use their operators!), meaning no memory leaks.
  • Containers are easy to access.
  • Iterators! No more problems looking for one data amont thousands in one container.
Really, they aren't a standard for nothing.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

What i meant was that in NTAI int eh globals class I initialize all the agents witht eh this keyword using thgisn such as L = new logger(this);
User avatar
mr sharpoblunto
Posts: 24
Joined: 12 Dec 2005, 03:47

Post by mr sharpoblunto »

ahh right. because to get it to work you don't pass the parameter by reference you pass by value, then a pointer to that value is passed to the function for you.

Code: Select all

L = new logger(*this);
this should work.
Post Reply

Return to “AI”