Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. The Criminal Enterprises
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

*DO NOT* SHARE MEDIA OR LINKS TO LEAKED COPYRIGHTED MATERIAL. Discussion is allowed.

Parse XML from web without lag


Jitnaught
 Share

Recommended Posts

(Title is supposed to be "Get XML from web without lag/pause".)

Hi everyone.

I'm trying to create a script which gets the current weather condition in your specified city, but I've got a problem. The game lags/pauses when I'm getting the XML. When I say pause, I don't mean like the pause menu, I mean the game stops doing anything until the script receives the XML.

Here is my function that I have now:

public static string GetCurrentConditions(string location)        {            try            {                string condition = "No data";                XmlReader xmlReader = XmlReader.Create(string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&mode=xml", location));                while (xmlReader.Read())                {                    if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "weather") && xmlReader.HasAttributes)                    {                        condition = xmlReader.GetAttribute("number");                        break;                    }                }                return condition;            }            catch (Exception ex)            {                return ex.ToString();            }        }

It works perfectly, but has the lag, which is annoying and I wouldn't want to release a mod that does that. I looked up the fastest way and I found that XmlReader was the fastest (I was using XmlDocument before). I don't think the parsing of the file is the cause of the lag because the XML is a small file. Just look at this.

Is there any other way to get the XML without this lag/pause?

 

P.S. I have never done anything related to XML, so if you think I sound like a noob or something, this is why.

Edited by LetsPlayOrDy
Link to comment
Share on other sites

 

you need to get the weather in a new System.Threading.Thread and then when that is finished... it can set some stringvariable and maybe trigger a bool to let you know the weather is ready for your script to use...

using System.Threading;...string weatherInfo = "Retrieving weather...please wait...";//you can be drawing this as text in a per frame draw event while the thread is runningThread t = new Thread(new ThreadStart(() =>{    //get weather here!!    //dont write any gta related code in here like calling natves    weatherInfo = yourParsedAndFormattedString;}));t.IsBackground = true;//this thread running will not prevent the app gtaiv.exe from closingt.Start();

Thanks! I tried it and it seems to be working. I'll post if otherwise ;)

Link to comment
Share on other sites

InfamousSabre

Ha! had the same idea as me :p I just finished mine a few days ago. used wunderground API
Wont be releasing though. GTA IV's weather just isn't dynamic enough for me to be pleased with the result.
Hope yours goes well :)

Link to comment
Share on other sites

Ha! had the same idea as me :p I just finished mine a few days ago. used wunderground API

Wont be releasing though. GTA IV's weather just isn't dynamic enough for me to be pleased with the result.

Hope yours goes well :)

Yeah, I know. I wish it was more dynamic too :/

I'm still going to release mine though :p

Link to comment
Share on other sites

InfamousSabre

in .net4+ you can also do this instead of creating a new thread

System.Threading.Tasks.Task.Factory.StartNew(() =>{   //get weather here!!   //dont write any gta related code in here like calling natves   weatherInfo = yourParsedAndFormattedString;}));

Haven't personally compared it against a new thread, but have read that its supposed to be less taxing on cpu/ram
*shrug* give it a shot if you'd like.

Link to comment
Share on other sites

NTAuthority

actually the thread pool is primarily meant for short-lived work objects; longer-term tasks are still better off on a manually-managed thread or passing a specific flag to the native thread pool functions that doesn't happen to be exposed in the BCL/FCL.

  • Like 1

SsZgxdL.png

Inactive in GTA/R* title modification indefinitely pursuant to a court order obtained by TTWO. Good job acting against modding!

Link to comment
Share on other sites

oh and i never mentioned your parsing is not the lag, waiting for the server to respond and converting the stream to text (i assume xml reader does this internally but i dont know unless i go look and the wrapped code) is what is lagging. So it depends on the server and it's traffic.

 

Ive parsed huge web pages just using string methods and it is always super quick...

 

getting the response stream and loading into a reader, then doing reader.ReadToEnd()... (better to read lines in a loop anyway)... on a large page will lag... im curious and may took a look at what xmlreader does

 

I already basically knew it isn't the lag. I mentioned that in my post :p

Link to comment
Share on other sites

LordOfTheBongs

 

oh and i never mentioned your parsing is not the lag, waiting for the server to respond and converting the stream to text (i assume xml reader does this internally but i dont know unless i go look and the wrapped code) is what is lagging. So it depends on the server and it's traffic.

 

Ive parsed huge web pages just using string methods and it is always super quick...

 

getting the response stream and loading into a reader, then doing reader.ReadToEnd()... (better to read lines in a loop anyway)... on a large page will lag... im curious and may took a look at what xmlreader does

 

I already basically knew it isn't the lag. I mentioned that in my post :p

 

u sounded unsure so i thought id tell u what i experienced before

Link to comment
Share on other sites

 

 

oh and i never mentioned your parsing is not the lag, waiting for the server to respond and converting the stream to text (i assume xml reader does this internally but i dont know unless i go look and the wrapped code) is what is lagging. So it depends on the server and it's traffic.

 

Ive parsed huge web pages just using string methods and it is always super quick...

 

getting the response stream and loading into a reader, then doing reader.ReadToEnd()... (better to read lines in a loop anyway)... on a large page will lag... im curious and may took a look at what xmlreader does

 

I already basically knew it isn't the lag. I mentioned that in my post :p

 

u sounded unsure so i thought id tell u what i experienced before

 

 

Ah ok

Link to comment
Share on other sites

When I use a thread and put a loop in it to keep checking for the weather, it freezes the game when the game starts. Does anyone know how to fix this? If you need my code just tell me.

Link to comment
Share on other sites

^ Don't know if you meant you wanted to look through the code or not, but I'll post it anyways.

 

This is the code I have right now. It runs when the script starts.

Thread thread = new Thread(new ThreadStart(() =>            {                while (!done)                {                        if (rwEnabled)                        {                            string getCondition = GetCurrentConditions(sLocation);                            if (currentCondition != getCondition)                            {                                currentCondition = getCondition;                                Game.Console.Print("Real Weather: Updated weather: " + CurrentConditionToName(getCondition));                            }							Thread.Sleep(60000);                        }                }            }));            thread.IsBackground = true;            thread.Start();

GetCurrentConditions gets the weather XML then parses it to get the condition (which is a string).

CurrentConditionToName converts the current condition to a name (like "thunderstorm" or "drizzle").

I don't think those are what are causing the freezing, as before I added the thread these functions worked fine.

Link to comment
Share on other sites

see what happens if u comment out the printing to the console, it needs to access the console object and this object i guess needs to be serializable to pass between threads... im not 100% certain though, just from my basic understanding of threading

Well that worked. Thanks :D

Link to comment
Share on other sites

ok cool, yeah i think he needed to add a Serializable attribute to the console class to make it be able to be serialized and rebuilt in the new thread so it could be used in your thread... anyways, just use a bool or whatever u like to trigger the code in your actual script running on the script thread

Already did ;)

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.