2019-07-26 08:51:44 (edited by Zarvox 2019-07-26 08:52:43)

Is it possible to use an array entry or string variable when asking bgt to move to a function? For example, string func="function";
//do some code
//go to function 3
(func)3();

is there any way to do this or do I have to write out the whole function name I want to go to?

2019-07-26 08:55:27

I believe you can do call backs, but you can't take a string and have it's contents be processed as the name of something. IE
string function_to_call = "start_game";
string name = "Liam";
if (name == "Liam")
function_to_call();

Also check the topic subject. You may want to slightly rename it. XD

Much less active on this forum than in the past.

Check out my live streams: http://lerven.me
follow me on Twitter: http://twitter.com/liamerven

2019-07-26 09:00:09

As an aside, in other languages like Python, I believe you can make arrays or well lists in python nomenclature of methods... I think. BGT is extremely inflexible when it gets to more advanced things. for example you're not able to pass an array as a function argument unless you've previously declared that array earlier.

Much less active on this forum than in the past.

Check out my live streams: http://lerven.me
follow me on Twitter: http://twitter.com/liamerven

2019-07-26 14:34:11

@Liam: thats not true, AngelScript (BGT) allows fast-declaration of arrays as an argument, although it doesn't look nice:

function_with_string_array_argument(array<string> = {"this", "is", "a", "test"});

This however isn't described in the BGT help file at all. To get more information about such nitpicky details, i'd recommend to consult the AngelScript documentation. BGT, just like AngelScript, does have a more or less good multi-inheritance functionality by using mixin classes, just as another example.
Best Regards.
Hijacker

2019-07-26 14:48:06

Oh cool. I wasn't aware you could do that. I love being wrong sometimes.

Much less active on this forum than in the past.

Check out my live streams: http://lerven.me
follow me on Twitter: http://twitter.com/liamerven

2019-07-26 15:17:25 (edited by amerikranian 2019-07-26 15:18:53)

Liam, you can store functions in python. Just put their names into a list without the parentheses

2019-07-26 15:29:28 (edited by Zarvox 2019-07-26 15:29:50)

what is this thing about arrays and function arguements? Never heard of that before. Can someone please explain what it does?

2019-07-27 15:20:29

@2 It's possible, but not easy.

/* A simple example to demonstrate calling a function through an array */

funcdef int calculate_operation(int num1, int num2); /* Define the signature of the functions that will be in our array */

int add(int a, int b)
    {
    return a + b;
}

int sub(int a, int b)
    {
    return a - b;
}

void main()
    {
    calculate_operation @[]operations = {add, sub};
    alert("Adding 2 and 2", operations[0](2, 2)); /* Calls the add operation */
    alert("Subtracting 2 and 2", operations[1](2, 2)); /* Calls the sub operation */
}

In this example, if we can call functions inside an array and if we define a function to map strings to their calculate_operation handles or use a dictionary, we can call functions by name.
For example, if we map "add" to array position 0, we can now add two numbers.

2019-07-27 16:01:30 (edited by Ethin 2019-07-27 16:05:04)

Going to derail the topic and say that C/C++ make this much easier. C:

void func() {
}
// call function
call_callback(&func);

Literally that's all you do. You can store functions in arrays, but you should use a typedef (otherwise your code looks ugly). So something like

typedef (void *function)();

Can then be made into a function pointer like this

void call_callback(function fnc) {
fnc();
}

Looks ugly, but its used all around the world for things like callbacks. .And you can store them in arrays too.
Like BGT though, you can't store a function name in a C string and then call it (I don't think). I've never seen it done, IMO, so it may not be possible. If its possible then its probably incredibly disgusting.
I think its possible in Python, not sure how though.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-07-27 16:33:44

For calling a single function in BGT, it's almost the same thing, replace & with @ and typedef with funcdef and the proper syntax.
In Python these things are much easier. For calling a function as a string we can use the getattr function:
>>> import operator
>>> getattr(operator, "add")(2, 2)
4
Replace "add" with a variable and you can call any function present in the operator module.
It's insecure, though.

2019-07-27 17:10:09

Wow I never thought of using getattr. smile And I didn't know that about BGT.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-07-27 23:50:42

@8: ah, that's how you do that. That would've been real nice a long time ago. I've been using a newStringArray function I wrote, which maxes out at however many arguments I feel like typing.
I was just this very day working on something involving arrays of callbacks, without having checked for if that actually works. How convenient that this thread should appear at the same time. smile
Javascript and Java have ... variations? On the concept. Java's is nightmarishly labyrinthine, since everything in Java has to be in a subclass of an implementation of an interface with a 50 character name and 5 layers of wrapper, so I'm not actually sure if it has an analog to string shortcuts for functions.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2019-07-28 03:14:53

@12 I didn't test this, but according to this question https://stackoverflow.com/questions/144 … ring-java, in Java, assuming that

arr[i]

is a string and the method is in the same class, you can do:

getClass().getMethod(arr[i]).invoke(this);