Jump to content
  • Advertisement
Sign in to follow this  
Thunder Sky

Unity Passing on variable number of arguments...again

This topic is 4525 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

I know a simular problem has already been discussed here but those answers only aplied when passing to libraryfunctions. What if I want to pass on the arguments to one of my own functions? Heres what I meen:

//This one formats the output and returnes the string
char* MyFormatFunction(char *input, ...) {
  va_list valist;
  va_start(valist,input);

  int length=512; //should be enough, havn't come up with a better solution
  char* str=(char*) malloc(length);
  vsnprintf(str,512,input,valist);
  va_end(valist);
  return str;
  };

char* MyPreProcessorFunction(char* input, ...) {
  //
  //I do some stuff with the input string here
  //
  
  char* str=MyFormatFunction(input, ...) // <- how do I pass on the arguments?
  return str;
  };

I know I could make MyFormatFunction take a va_list as the second argument but then I wouldnt be able to call it directly in ordinary printf style. You might also wonder why on earth I would declare a new function like MyFormatFunction when what it does basicly can be sumerized in just two lines of code (malloc and vsnprintf). To tell you the truth I cant remember right now :P. But I know it was necessery. One reason I can think of now is that I wanted to wrap all my output functions with a nice prefix and I wanted MyFormatFunction to be a part of that group. Thanks in advance for any replies. /ThunderSky

Share this post


Link to post
Share on other sites
Advertisement
You need to break MyFormatFunction into two functions, just like printf/vprintf and similar library functions. Then, have the variadic version call the one that takes a va_list parameter.

Hard-coded buffer sizes = evil.

Share this post


Link to post
Share on other sites
Returning new'd string buffers like that is IMO evil as well, unless you have a clear understanding of who owns the string and therefor who is responsible for cleaning it up, or if you return a smart pointer to the buffer.

Share this post


Link to post
Share on other sites
Quote:

You need to break MyFormatFunction into two functions, just like printf/vprintf and similar library functions. Then, have the variadic version call the one that takes a va_list parameter.

Thanks Fruny, quite simple, this shows that I still have a lot to learn...

Quote:

Hard-coded buffer sizes = evil.

I dont like that "should be enough", and whats even wors is that I have no error checking yet either. But as I understand it the implementations of sprintf and related functions seldom comply with the standard so I cant really be sure if will return the buffersize needed if an error occurs.

I read that there is a specific function that doesnt acctually return the formatted buffer, but the size that would be needed to store it. Unfortunatly, it seems to be IDE dependent. I got hold of the VC version on microsofts developer pages but Im using DevC++ (MinGW) so that didnt help much. I later found that there is a g++ version (MinGW is a g++ compiler right?). But when I tried to compile whith the g++ version DevC++ said it was undeclared so...Im cinda lost. If you know how to solv the problem, great! otherwise I think Ill just leav it. Not verry nice, but it'l be enough for what Im using it for.

Quote:

Returning new'd string buffers like that is IMO evil as well, unless you have a clear understanding of who owns the string and therefor who is responsible for cleaning it up, or if you return a smart pointer to the buffer.


yeah I know..guess Im just an evil cinda guy...(whats a smart pointer?)


Edit. Forgot the thank you guys for the comments so..thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by Thunder Sky
I got hold of the VC version on microsofts developer pages but Im using DevC++ (MinGW) so that didnt help much. I later found that there is a g++ version (MinGW is a g++ compiler right?).



Since you're already using C++, you could use boost::format.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Since you're already using C++, you could use boost::format.


Seconded. boost::format is nice and powerful and type safe and all that.

A smart pointer is a class that basically deletes an object when the last reference to it is destroyed. In your code above you return a malloc'd char buffer. If you returned


boost::shared_array<char> MyFunction()
{
boost::shared_array<char> mysmartbuffer(new char[length]);
}
// do stuff
return mysmartbuffer;


This would return a 'smart pointer' to this memory block. When the last smart pointer that references that block goes out of scope or is otherwise delted, the buffer will be deleted automatically, resulting in no memory leak and no need to worry about if the block gets deleted properly or not.

Share this post


Link to post
Share on other sites
Quote:

Since you're already using C++


Well I guess I do, but I have no idea (well some idea) of how to use classes and a lot of the other C++ stuff. Havnt got the time or strength to read up on the subject... so I tend to stick to the more basic C. for now.

besides, Im using this for a small chat program so I prefer not to use any nonstandard libraries.

Share this post


Link to post
Share on other sites
Which brings me to another thing Ive been meaning to ask about. If I include a file, say string.h, how much does the final executable grow in size. would it be better to do a
for(i=0;buffer!='\0';i++);
instead of including the whole string.h file and using strlength?

Mainly Im asking because windows.h is huge when you count in all the other files it inculdes and I like to use Sleep() in my simpler console apps and I read somewhere that that could be acheaved some other way.

thanks for the other replies

Share this post


Link to post
Share on other sites
Smart compilers don't leave un-used code in the final executable. The actual code for the functions you don't use should not end up in the .exe. It may increase compile time a bit though, but not enough to worry about whether you should use external stuff.

Share this post


Link to post
Share on other sites
Quote:
Original post by Thunder Sky
Well I guess I do, but I have no idea (well some idea) of how to use classes and a lot of the other C++ stuff.


To a large extent, using existing classes should be easier than writing classes. Especially with well-designed libraries.

Quote:
Havnt got the time or strength to read up on the subject... so I tend to stick to the more basic C. for now.

Quote:
Thanks Fruny, quite simple, this shows that I still have a lot to learn...


Do you want to learn or not? [smile] The best way to learn is to actually start using the libraries. You'll probably quickly make up for the "lost" time.

Quote:
besides, Im using this for a small chat program so I prefer not to use any nonstandard libraries.


The C standard library does not include any networking facilities, so you're going to need non-standard libraries anyway. Additionally, quite a few components from the Boost library have been accepted into the upcoming revision of the C++ standard library. You're on shaky grounds to use non-standardization as an argument for rejection. [smile]

Quote:
and I like to use Sleep() in my simpler console apps


Sleep() isn't a standard C function either. [wink]

Quote:
(whats a smart pointer?)


A smart pointer is an object designed to act like a pointer syntactically, but with additional functionality, such as keeping track of the number of references to the block it points to, and automatically releasing the block when that reference count falls to zero.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Aryndon
      Project Redemption is an semi-fantasy RPG with a linear story and an elaborate combat system.
      We are building in Unity and are currently looking animators and artists.
      What we are looking for
      -Someone who is okay with split revenue/profits when finished
      -Collaborate with others in the team. Do you have an idea/thought on what should be included? Tell us!
      -Someone who wants to work with people that are passionate about this project
      If you are interested. Please message me and I will get back to you as soon as possible! Or add me on Discord AJ#6664
    • By Aggrojag
      Hello!
      I'm working on a game that is a narrative driven dark comedy with some small aspects of platforming and puzzle solving. The project is rather small as well. It touches on topics such as suicide, mental illness, family, corruption, free-will, and redemption.
      This project is exercise in polish, this means some experimentation will be needed along with some reworking of assets as they're provided.
      This will be a revshare model.
      First, I'm looking for a 2D sprite artist, not pixelated, that can compliment the style of the attached images, and be similar to the temporary character.
      We are looking to bring on a SFX designer at this time. Full list of required SFX will be available upon request, as well as a build with all elements related to sound implemented in some form (many SFXs pulled from the web for now). Will likely require some field recording, and some pretty strange SFX for when things get weird. I imagine a good portion of these will be quite fun to create.
      Lastly, I'm looking for a male voice actor, English should be your primary language. There will be at minimum two characters that will need to be brought to life through vocals. The first voice is similar to Marvin from Hitchhiker's Guide to the Galaxy. A reference for the second voice would be a mix of Ren (Ren & Stimpy), and Android 21 (DragonBallFighterZ). Due to formatting, I'm not including YouTube links in the post, sorry.
       
      WIP Scene with our custom shaders attached (platforms are lazily placed, as this was an asset test):

      A scene with dynamic lighting and temp character:

       
      Unshaded asset:

      If you made it to the bottom, thank you, and I look forward to hearing from you.
    • By SickTwistGames
      Ok, firstly, Hi.
       
      This is my first post on this forum. I am an Indie Dev making my first game so bear with me when I say dumb stuff, I'm on a huge learning curve.
       
      My first question is about inventory systems for unity. I am trying to make a survival type game with crafting. I have purchased Inventory manager pro by devdog from the unity asset store and it seems like a pretty powerful assett but for an intermediate coder its a little tough to use.  I'm beginning to wonder if it was the right purchase.
      So my question is.... does anyone have any experience of inventory plugins / systems for unity and can anyone reccomend a system to me?
      It needs to have the following: Loot system, crafting system, character sheet, blueprint system,  character stats system. Ideally with as little coding as possible.
       
      Thanks
    • By ethancodes
      I've got a bug with my brick breaker style game. The bricks move down one line at a time ever 1.5 seconds. What appears to be happening is occasionally the ball will be just about to hit the brick when the brick moves down a line, and now the ball is behind it. I'm not sure how to fix this. I have two ideas but I'm not sure of implementation. 1 solution would be to check where they were and where they are going to be before rendering the frame. Then if they crossed paths, then register the brick as hit. Solution 2 would be change how the bricks move. I could maybe slide them down line by line, instead of a jump down. I'm not sure of this will fix the issue or not. Any ideas?
    • By Pixeye
      I wrote an extension for unity inspector that allows to group/fold variables. 
      Available on github  , cheers!

       
  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!