Jump to content
  • Advertisement
Sign in to follow this  
IssamBoughanmi

formating function

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

Hi,

is there any formatting function for angelscript ?

what i want to get is something like that:

[color="#ff0000"]heightmap_05_12.raw where the input of the numbers is : 5 and 12

thanks and good day

Share this post


Link to post
Share on other sites
Advertisement
I haven't provided a pre-implemented add-on for formatting strings, but you can easily do it yourself, for example:

[source]
string FormatInt(const string &fmt, int value)
{
// Make sure the format string is safe
int p = fmt.find("%");
if( p != -1 && fmt.find_first_of("%*sS", p+1) != -1 )
return "{bad format}";

const char buf[256] = {0};
snprintf(buf, 255, fmt.c_str(), value);
return buf;
}

engine->RegisterGlobalFunction("string FormatInt(const string &in, int)", asFUNCTION(FormatInt), asCALL_CDECL);
[/source]


With this (and the standard string add-on) you can write scripts like this:

[source]
string FormatFileName(int a, int b)
{
return "heightmap_" + FormatInt("%02d", a) + "_" + FormatInt("%02d", b) + ".raw";
}
[/source]

Regards,
Andreas

Share this post


Link to post
Share on other sites
hi andreas !,

first thanks for the code, but as you can guess i am complete beginner in angelscript and in scripting languages in general,

this piece of code wont to work



string FormatInt(string &fmt, int value)
{

const char buf[256] = {0};
snprintf(buf, 255, fmt.c_str(), value);
return buf;

}


compiler error
Row: 44, Col: 22 :: No matching signatures to 'string::find_first_of(string&, int)'


any idea please ?



edit : i am reading here that strings are part of an addon, is taht correct ? and if yes how to install this addon

Share this post


Link to post
Share on other sites
The add-ons are located in the folder add_on in the SDK. Include the source code for the addons you want to use in your project so they are compiled together with your code, then you register them with the appropriate registration function. For the string add-on you register it with:

[source]
#include "add_on/scriptstdstring/scriptstdstring.h"

void ConfigureEngine(asIScriptEngine *engine)
{
RegisterStdString(engine);
}
[/source]


The code I provided for FormatInt() was supposed to a C++ global function. After registering the string add-on you would register the FormatInt function with the call I showed as well.



Regards,
Andreas

Share this post


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

  • 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!