Answered [BO3] Errors with functions that end in ("");

Status
Not open for further replies.

SuperMods

New Member
Messages
3
Reaction score
0
Points
1
So this is my option with the function of the magic bullet using a standard pistol.
self addOpt("main_bullets", "Magic Bullet", func_magicbullettest("pistol_standard");

When using this template self addOpt("main_bullets", "Magic Bullet", func_magicbullettest
it is expecting you to just add this at the end ); which is what some functions do, however when using the magic bullet function it requires you to input what gun you want to use like the line of code above, but because it has ("pistol_standard"); at the end it doesn't understand.
And this problem is holding me back from so much functions, please help me how to use functions like this
func_magicbullettest("pistol_standard");
 

Cxwh

Veteran
Messages
64
Reaction score
45
Points
793
You need to add another parameter called input or arguments (or anything else). So addOpt should like this
Code:
self function addOpt(menu, name, function, input) //input incase you want to pass variables through function
Now, since pistol standard is an argument, you need to pass it through the input parameter.
Code:
self addOpt("main_bullets", "Magic Bullet", &func_magicbullettest, "pistol_standart");
Note that I used function pointers (&func_magicbullettest instead of func_magicbullettest())
And in your controls function you execute the function like this
Code:
self [[&function]](input);

To further help you understand I'll show you how I do it

My addMenu function
Code:
function addMenu(parent, label, access = 0)
{
    self.occult["menu"][label] = [];
    self.occult["menu"][label]["access"] = access; //Access level you need to access the menu
    self.occult["menu"][label]["label"] = label; //Name of the menu
    self.occult["menu"][label]["parent"] = parent; //Menu's parent
    self.occult["menu"][label]["options"] = []; //Creating an array for options
    self.occult["menu"][label]["position"] = 0; //Menu position (needed for scrolling)

    if(!empty(self.occult["menu"][parent])) //check if a menu parent is defined
        self addOption(parent, label, &submenu, label); //if a parent is defined it should add a submenu option to the parent menu
}


My addOption function
Code:
function addOption(parent, label, func, input, type = "thread", entity = self)
{
    index = self.occult["menu"][parent]["options"].size; //Get how many options are in the submenu
    self.occult["menu"][parent]["options"][index] = []; //Create an array for the new option we're about to add

    self.occult["menu"][parent]["options"][index]["label"] = label; //Option's name
    self.occult["menu"][parent]["options"][index]["func"] = func; //Option's function
    self.occult["menu"][parent]["options"][index]["input"] = input; //Input (needed to pass args)
    self.occult["menu"][parent]["options"][index]["type"] = type; //Don't mind that
    self.occult["menu"][parent]["options"][index]["entity"] = entity; //Don't mind that either
}


Part of my monitor() option (used to monitor players behavior)
Code:
//Selects option
if(self JumpButtonPressed())
{
    selected = self.occult["menu"][self getcurrentmenu()]["options"][self getmenuposition()]; //Get the selected option
    if(selected["type"] == "thread") //if the options should be threaded
    {
        if(empty(selected["input"])) //if input is undefined just execute the function without any arguments
            selected["entity"] thread [[selected["func"]]](); //executes the function

        //if input is defined and it's an array
        else if(isArray(selected["input"]))
        {
            //in case that input is an empty array
            if(selected["input"].size == 0)
                self iprintln("^1ERROR: ^3Cannot call function: no arguments passed");

            //if theres 1 var stored in the array
            else if(selected["input"].size == 1)
                selected["entity"] thread [[selected["func"]]](selected["input"][0]); //Execute the function with 1 argument

            //if theres 2 var stored in the array
            else if(selected["input"].size == 2)
                selected["entity"] thread [[selected["func"]]](selected["input"][0], selected["input"][1]); //Execute the function with 2 argument

            //if theres 3 var stored in the array
            else if(selected["input"].size == 3)
                selected["entity"] thread [[selected["func"]]](selected["input"][0], selected["input"][1], selected["input"][2]); //Execute the function with 3 argument

            //if theres 4 var stored in the array
            else if(selected["input"].size == 4)
                selected["entity"] thread [[selected["func"]]](selected["input"][0], selected["input"][1], selected["input"][2], selected["input"][3]); //Execute the function with 4 argument

            //if theres 5 var stored in the array
            else if(selected["input"].size == 5)
                selected["entity"] thread [[selected["func"]]](selected["input"][0], selected["input"][1], selected["input"][2], selected["input"][3], selected["input"][4]); //Execute the function with 5 argument

            //if there are more than 5 arguments
            else
                self iprintln("^1ERROR: ^3Cannot call function: too many arguments passed");
        }
        //if input is defined, but not an array (so it's just a string or number or whatever)
        else
            selected["entity"] thread [[selected["func"]]](selected["input"]); //Execute the function with 1 argument
    }
    //if the option type is immediate (don't mind that part)
    else if(selected["type"] == "immediate")
    {
        if(empty(selected["input"]))
            selected["entity"] [[selected["func"]]]();
        else if(isArray(selected["input"]))
            selected["entity"] [[selected["func"]]](selected["input"][0], selected["input"][1], selected["input"][2], selected["input"][3], selected["input"][4]);
        else
            selected["entity"] [[selected["func"]]](selected["input"]);
    }
    //if the entity the function will be called on isn't self
    if(selected["entity"] != self)
        self iPrintln(selected["label"] + " was called on " + getName(selected["entity"])); //tell player that he called the function on another entity
    wait 0.2;
}
//cred mainly fallen


I know this looks confusing as f**k so just send me a message if you need any help!
 
  • Like
Reactions: P!X

SuperMods

New Member
Messages
3
Reaction score
0
Points
1
You need to add another parameter called input or arguments (or anything else). So addOpt should like this
Code:
self function addOpt(menu, name, function, input) //input incase you want to pass variables through function
Now, since pistol standard is an argument, you need to pass it through the input parameter.
Code:
self addOpt("main_bullets", "Magic Bullet", &func_magicbullettest, "pistol_standart");
Note that I used function pointers (&func_magicbullettest instead of func_magicbullettest())
And in your controls function you execute the function like this
Code:
self [[&function]](input);

To further help you understand I'll show you how I do it

My addMenu function
Code:
function addMenu(parent, label, access = 0)
{
    self.occult["menu"][label] = [];
    self.occult["menu"][label]["access"] = access; //Access level you need to access the menu
    self.occult["menu"][label]["label"] = label; //Name of the menu
    self.occult["menu"][label]["parent"] = parent; //Menu's parent
    self.occult["menu"][label]["options"] = []; //Creating an array for options
    self.occult["menu"][label]["position"] = 0; //Menu position (needed for scrolling)

    if(!empty(self.occult["menu"][parent])) //check if a menu parent is defined
        self addOption(parent, label, &submenu, label); //if a parent is defined it should add a submenu option to the parent menu
}


My addOption function
Code:
function addOption(parent, label, func, input, type = "thread", entity = self)
{
    index = self.occult["menu"][parent]["options"].size; //Get how many options are in the submenu
    self.occult["menu"][parent]["options"][index] = []; //Create an array for the new option we're about to add

    self.occult["menu"][parent]["options"][index]["label"] = label; //Option's name
    self.occult["menu"][parent]["options"][index]["func"] = func; //Option's function
    self.occult["menu"][parent]["options"][index]["input"] = input; //Input (needed to pass args)
    self.occult["menu"][parent]["options"][index]["type"] = type; //Don't mind that
    self.occult["menu"][parent]["options"][index]["entity"] = entity; //Don't mind that either
}


Part of my monitor() option (used to monitor players behavior)
Code:
//Selects option
if(self JumpButtonPressed())
{
    selected = self.occult["menu"][self getcurrentmenu()]["options"][self getmenuposition()]; //Get the selected option
    if(selected["type"] == "thread") //if the options should be threaded
    {
        if(empty(selected["input"])) //if input is undefined just execute the function without any arguments
            selected["entity"] thread [[selected["func"]]](); //executes the function

        //if input is defined and it's an array
        else if(isArray(selected["input"]))
        {
            //in case that input is an empty array
            if(selected["input"].size == 0)
                self iprintln("^1ERROR: ^3Cannot call function: no arguments passed");

            //if theres 1 var stored in the array
            else if(selected["input"].size == 1)
                selected["entity"] thread [[selected["func"]]](selected["input"][0]); //Execute the function with 1 argument

            //if theres 2 var stored in the array
            else if(selected["input"].size == 2)
                selected["entity"] thread [[selected["func"]]](selected["input"][0], selected["input"][1]); //Execute the function with 2 argument

            //if theres 3 var stored in the array
            else if(selected["input"].size == 3)
                selected["entity"] thread [[selected["func"]]](selected["input"][0], selected["input"][1], selected["input"][2]); //Execute the function with 3 argument

            //if theres 4 var stored in the array
            else if(selected["input"].size == 4)
                selected["entity"] thread [[selected["func"]]](selected["input"][0], selected["input"][1], selected["input"][2], selected["input"][3]); //Execute the function with 4 argument

            //if theres 5 var stored in the array
            else if(selected["input"].size == 5)
                selected["entity"] thread [[selected["func"]]](selected["input"][0], selected["input"][1], selected["input"][2], selected["input"][3], selected["input"][4]); //Execute the function with 5 argument

            //if there are more than 5 arguments
            else
                self iprintln("^1ERROR: ^3Cannot call function: too many arguments passed");
        }
        //if input is defined, but not an array (so it's just a string or number or whatever)
        else
            selected["entity"] thread [[selected["func"]]](selected["input"]); //Execute the function with 1 argument
    }
    //if the option type is immediate (don't mind that part)
    else if(selected["type"] == "immediate")
    {
        if(empty(selected["input"]))
            selected["entity"] [[selected["func"]]]();
        else if(isArray(selected["input"]))
            selected["entity"] [[selected["func"]]](selected["input"][0], selected["input"][1], selected["input"][2], selected["input"][3], selected["input"][4]);
        else
            selected["entity"] [[selected["func"]]](selected["input"]);
    }
    //if the entity the function will be called on isn't self
    if(selected["entity"] != self)
        self iPrintln(selected["label"] + " was called on " + getName(selected["entity"])); //tell player that he called the function on another entity
    wait 0.2;
}
//cred mainly fallen


I know this looks confusing as f**k so just send me a message if you need any help!

Amazing thank you so much :smile:
 
Status
Not open for further replies.
Top