Advertisement

Dynamic Parameter Count Support

Started by October 21, 2023 01:05 PM
6 comments, last by WitchLord 1 year ago

Hey,

Autodesk 3ds Max has a scripting language known as MaxScript, In MaxScript every value in script is driven from Value base :

class Value : public Collectable
{
	...
}

To expose a function we simply define it as following :

// Maxscript Exposed API
def_visible_primitive(FunctionName, "FunctionNameInScript");
Value* FunctionName_cf(Value** arg_list, int count)
{
	if (count == 3)
	{
		const wchar_t* str = arg_list[0]->to_string();
		INT64 str = arg_list[1]->to_int64();
		INT_PTR str = arg_list[2]->to_intptr();
		return &ok;
	}
	else
	{
		throw RuntimeError(L"Invalid parameters"); return &false_value;
	}
}

So we can have a function with unlimited and dynamic parameters (arguments), Is this possible in AngelScript? I've seen usage of Generic value but it needs to define function definition still with limited parameters, Also return value is dynamic as well, I guess return value can be set by generic API dynamically but I'm not sure, This can be very useful.

If it's not possible is there a hope for implementing such feature?

Note : I am aware of getting close result using dictionary but I want something like what MaxScript offers like any function(param1,param2,param3,param4,param5,…)

  • The universe is basically an animal. It grazes on the ordinary.

Variadic parameters is not yet supported in AngelScript, though it is on the to-do list.

In the meantime you might want to take a look at how Patrick Jeeves did something similar: https://github.com/SpehleonLP/Zodiac/tree/main/example

Perhaps you can do the same?

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

@WitchLord I did something simpler for printf implementation :

static int asFormatMaxArgCount = 9999;

inline string asFormatGetFunctionDef()
{
	string generatedFuncDef = "void asFormat(string &in, ";
	for (int i = 0; i < asFormatMaxArgCount; i++)
	{
		generatedFuncDef += "?&in var" + to_string(i + 1) + " = 0";
		if (i != asFormatMaxArgCount - 1)  generatedFuncDef += ", "; else generatedFuncDef += ")";
	}
	return generatedFuncDef;
}

void asFormat(asIScriptGeneric* gen) { ... }
if (engine->RegisterGlobalFunction(asFormatGetFunctionDef().c_str(), asFUNCTION(asFormat), asCALL_GENERIC) <= 0) return false;

I would love to personally meet someone who needs more than 9999 arguments :D I also implemented some custom printing symbols like %vec3 %vec2 %ptr, Works pretty good.

But overall, Same implementation like MaxScript can be extremely helpful I really like to use AngelScript in all of my projects, I'm making all my friends switching from LUA to AngleScript xD

  • The universe is basically an animal. It grazes on the ordinary.

I hope you're not registering 9999 functions with the engine just for this 🙂.

If you feel like implementing the support for this in the script engine, I would be grateful. It is something I want to add, but I really don't have much time to work on new features since my son was born 5 years ago. I thought I would get more time available as he grew older, but that was clearly a very wrong expectation 😉

I'm pleased to hear you like AngelScript anyway and are convincing your friends to switch over from Lua. 🙂

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

@WitchLord

hope you're not registering 9999 functions with the engine just for this 🙂.

I just register a single one as Generic, in asFormat I check types of passed arguments for example if I use asFormat("hello %d %s", 1, “world”) other 9997 will be null, But now you said it I'm not sure, Does it mean if I'm calling this :

if (engine->RegisterGlobalFunction("void asFormat(string &in,?&in var1,?&in var2,?&in var3,?&in var4,?&in var5)", asFUNCTION(asFormat), asCALL_GENERIC) <= 0) return false;

It does create 5 function variation in back-end and register them to engine?

I feel like implementing the support for this in the script engine, I would be grateful. It is something I want to add, but I really don't have much time to work on new features since my son was born 5 years ago. I thought I would get more time available as he grew older, but that was clearly a very wrong expectation 😉

I feel you man but we all know that life often has different plans especially with the responsibilities that come with being a parent 🙂

Wish the best for you and your son!

  • The universe is basically an animal. It grazes on the ordinary.

Ah, no, in your case it will be only a single function. However, it will be extremely expensive run-time wise, because the script will have to push all 9999 arguments on the stack in order to call the function even though they are just the default values.

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

By the way, adding simple support for variadic arg lists on registered functions can probably be done quite easily.

  1. add support for the … token in the tokenizer
  2. add support for the … token in the parser for function signatures
  3. add support in the compiler to allow matching function calls to functions taking variadic arg lists
  4. have the compiler push the arg count on the stack and then treat all the args as ?&in
  5. change the asIGeneric interface to allow it to iterate over the args in the variadic arg list

To keep it simple in the beginning only support the asCALL_GENERIC calling convention, and don't allow it for script declared functions.

At a later stage it would also be necessary to allow the script writer to inform whether an arg is supposed to be treated as &out or &inout in the call, so functions like scanf can be supported.

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