1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Using the example code of setWeather, get system.ArgumentException

Discussion in 'Scripting Help' started by FraGmenT, Jan 11, 2017 at 10:02 PM.

  1. FraGmenT

    FraGmenT Member

    Messages:
    23
    Likes Received:
    2
    Joined:
    Wednesday
    First of all, I only have a small experience with C#. I found out about setWeather through the following link: https://wiki.gtanet.work/index.php?title=setWeather .

    I viewed the code, I actually copy-pasted the code and I get the following error when executing the command with any ID: " System.ArgumentException, the object of type System.String cannot be converted to type System.Int32 ".

    It's pretty strange that I copy paste the code and get this error, I also don't have any idea why this is happening because I'm declaring "int weather" when the client executes the command.


    Code:
        [Command("weather", Description = "Sets the world weather", GreedyArg = true)]
        public void Command_Weather(Client player, int weather)
        {
            if (weather < 0 || weather > 13)
                player.sendChatMessage("~r~Invalid weather ID.");
            else
            {
                API.setWeather(weather);
                API.sendNotificationToAll("Weather set to ~b~" + weather + "~w~ by ~b~" + player.name);
            }
        }
    Thanks in advance!

    EDIT: I "FIXED" THE CODE BY USING string weather INSTEAD OF int weather.


    Code:
    [Command("weather", Description = "Sets the world weather", GreedyArg = true)]
        public void Command_Weather(Client player, string weather)
        {
          int weatherid = Int32.Parse(weather);
    
            if (weatherid < 0 || weatherid > 13)
                player.sendChatMessage("~r~Invalid weather ID.");
            else
            {
                API.setWeather(weatherid);
                API.sendNotificationToAll("Weather set to ~b~" + weather + "~w~ by ~b~" + player.name);
            }
        }
    This is just a temporary solution I guess, I'm still looking forward to more information on why I couldn't run the original example code.
     
    Last edited: Jan 11, 2017 at 10:12 PM
  2. Amit_B

    Amit_B Member Language Board Moderator

    Messages:
    20
    Likes Received:
    7
    Joined:
    Oct 27, 2016
    Hey,

    I acutally wrote the code you've seen in the wiki.
    I'm also not sure why it isn't working, pretty sure it's a bug as another page on the wiki said that we could make commands with int parameters.

    However, I've modified the wiki page with another version of a working command with a similiar workaround fix as the one you've found:
    Code:
            [Command("weather", Description = "Sets the world weather", GreedyArg = true)]
            public void Command_Weather(Client player, string wea)
            {
                int weather = -1;
                bool flag = int.TryParse(wea, out weather);
                if (!flag || weather < 0 || weather > 13)
                    player.sendChatMessage("~r~Invalid weather ID.");
                else
                {
                    API.setWeather(weather);
                    API.sendNotificationToAll("Weather set to ~b~" + Utils.GetWeatherName(weather) + "~w~ by ~b~" + player.name);
                }
            }
    
    Also, as we're new with the mod, have a bonus code to use / learn from: http://pastebin.com/sce79bu0
    It's from a debug script which I intend to release soon.

    Good luck!
     
    FraGmenT likes this.
  3. FraGmenT

    FraGmenT Member

    Messages:
    23
    Likes Received:
    2
    Joined:
    Wednesday
    Thank you for the further information and the pastebin is definitely valuable!
     
    Amit_B likes this.

Share This Page