Advertisement

formating function

Started by November 16, 2011 07:23 PM
2 comments, last by WitchLord 13 years, 1 month ago
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
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

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
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
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

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement