Advertisement

How would you approach adding an interface to c++ variant to AngelScript?

Started by December 12, 2019 02:07 PM
4 comments, last by Michael Marchesan 4 years, 11 months ago

Greetings,

i've been working for a while to a program (not game) that revolves around graphs, letting the user modify, create, and save graphs. One functionality i've been working on for some time is scripting support, letting the user write an algorithm in angelscript and see it affect the graph graphically in realtime (for example perform a DFS highlighting the nodes and arcs being visited while the algorithm goes on).

Until now the user could add custom variables to arcs and nodes, and access them through scripts (a simple map string>double). Now i'd like to let the user add variables deciding the type as well, so instead of:

node.add_var("myvariable", 5); //in c++ node.vars["myvariable"] = 5; where node.vars is std::map<std::string, double>

they would specify the type to add and type to get.

However this would start adding exceptions to scripts since variant throws an exception when you get the wrong time, and for something which purpose is easily writing and testing simple algorithms, i'd rather avoid having the user check for exceptions every single time they try to get a variable...

Any ideas? Should i just capture the exception in c++ outside of the angelscript run call, so if the script is using invalid types it's interrupted and the error is reported to the user?

Wouldn't you just only define the valid types for add_var then? For example:

void add_var(const string &in name, double value)
void add_var(const string &in name, float value)
void add_var(const string &in name, int value)
void add_var(const string &in name, bool value)

Or only the valid types for a variant type:

void add_var(const string &in name, const variant &in v)

class variant
{
  variant(double value) { /* .. */ }
  variant(float value) { /* .. */ }
  variant(int value) { /* .. */ }
  variant(bool value) { /* .. */ }
}

Or maybe I'm missing your point :D

Advertisement

It sounds like you want something similar to the dictionary add-on. Have you already look at the source code for that one to see if it answers your questions?

Michael Marchesan said:
i'd rather avoid having the user check for exceptions every single time they try to get a variable...


If you don't want the exceptions to be raised to the script, then you obviously need to catch possible exception in the C++ code and return a default response in such cases. You can do this with wrapper functions, so that you don't have to modify the rest of the application code.

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

Sorry for the late reply and poor explanation. Setters are easy with function overloading, the problem are getters, i whish you could overload a function based on the return type but it's something neither c++ nor AngelScript support.

I could just stop the script capturing the exception for getting a wrong type from the variant outside of the script execution, or maybe write my own less safe variant with an union that will simply return gibberish although still valid values if the script is attempting to get the wrong type

This topic is closed to new replies.

Advertisement