Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jritts

does ANYONE who knows how to do this???

This topic is 6926 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This post is about functions with optional arguments (ANSI compatible). I have a function that looks something like this: void AddWaypoints(int speed, int numWayPts, int x1, int y1, ...); And I want to make this function: void AddWalkingWaypoints(int numWayPts, int x1, int y1, ...); It''s the same thing, only you don''t have to give it a travelling speed (it''ll use some predefined constant instead.) I just want to be able to pass the list of points sent to AddWalkingWaypoints to AddWaypoints. How do I do that? I think printf passes its optional args to vprintf in a similar way, but i''m not sure. I tried to do this within AddWalkingWaypoints - { va_list params; va_start(params, y1); AddWaypoints(CH_MOVEMENT_WALKINGSPEED, numPts, x1, y1, params); va_end( params ); return 0; } and tried dereferencing params in the call to AddWaypoints, but it doesn''t work. Can someone help me? Thanks in advance! -James

Share this post


Link to post
Share on other sites
Advertisement
First a dumb question. Are you needing to do this in ANSI C, not C++? In C++ there are actually four solutions, but one of the is WAY harder than the others, and another one is not as clean as the others, and THOSE two are the ONLY ones availible to C programmers. Here''s the list:

(recommended C++)
1. A function with a default parameter.
(just as good C++)
2. An overloaded function.

(not clean but works in C)
3. Simply make two functions (they need different names, but you can implement the one that takes one less parameter as simply a call to the larger function that uses the predefined constant.
(hard and inefficient, but functional, for C)
4. Variable argument list (like printf).

Good luck, and feel free to write back with questions for more details about any of the above, or other ideas.

Share this post


Link to post
Share on other sites
i''m not sure how you could use either a default argument or an overloaded function, since your types are integer and there would be no way for the compiler to know whether the parameters were actually part of the ... of one function or not.. hope that makes sense

you might pass an array of waypoints, that would actually be more useful because you could change the size at runtime.. instead of everything being pre-specified in the call.

or like Xai said, you could use a variable list parameter,
but it wouldnt be as flexible as an array.
like this..


void AddWayPoints(int speed, int num, int x, int y, ...);
void AddWalkingWaypoints(int num, int x, int y, ...);
void AddWayPoints_(int speed, int num, int x, int y, va_list list);

void AddWayPoints(int speed, int num, int x, int y, ...)
{
va_list list;
va_start(list, y);
AddWayPoints_(speed, num, x, y, list);
va_end(list);
}

void AddWalkingWaypoints(int num, int x, int y, ...)
{
va_list list;
va_start(list, y);
AddWayPoints_(100, num, x, y, list);
va_end(list);
}

then implement AddWayPoints_() however you do that..

well i hope this helps.. good luck!


adamm@san.rr.com

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!