Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    1. Red Dead Redemption 2

      1. Gameplay
      2. Missions
      3. Help & Support
    2. Red Dead Online

      1. Gameplay
      2. Find Lobbies & Outlaws
      3. Help & Support
    1. Crews & Posses

      1. Recruitment
    2. Events

    1. GTA Online

      1. Arena War
      2. After Hours
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Grand Theft Auto Series

    3. GTA Next

    4. GTA V

      1. PC
      2. Guides & Strategies
      3. Help & Support
    5. GTA IV

      1. Episodes from Liberty City
      2. Multiplayer
      3. Guides & Strategies
      4. Help & Support
      5. GTA Mods
    6. GTA Chinatown Wars

    7. GTA Vice City Stories

    8. GTA Liberty City Stories

    9. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    10. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    11. GTA III

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    12. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    13. Wiki

      1. Merchandising
    1. GTA Modding

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Mod Showroom

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

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Red Dead Redemption

    2. Rockstar Games

    1. Off-Topic

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

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

    2. Forum Support

    3. Site Suggestions

MrGTAmodsgerman

[C#.NET] How to read INI file section in dynamic blocks?

Recommended Posts

MrGTAmodsgerman

Hey guys,

 

Lets say, i have a INI File like the following example:


VEHICLE_MODEL = btype2
[SLOT1]
SLOT_POSX: 73588539803
SLOT_POSY: -785656685
SLOT_POSZ: 73588539803
[SLOT2]
SLOT_POSX: 73588539803
SLOT_POSY: -785656685
SLOT_POSZ: 73588539803
SLOT_COUNT = 2 <both slots getting used by the script>
NeedXY = true
godmode = false
invisible = false


VEHICLE_MODEL = infernus
[SLOT1]
SLOT_POSX: 53588539803
SLOT_POSY: -585656685
SLOT_POSZ: -5588539803
[SLOT2]
SLOT_POSX: 7878539803
SLOT_POSY: -785656685 >Not getting used because of SLOT Count 1<
SLOT_POSZ: 73588539803
SLOT_COUNT = 1
NeedXY = false
godmode = false
invisible = false

Ok? Got it?

 

Now, lets say that the SLOT POS XYZ are coordinates that get set by the ini file and loaded by the script. So the car get teleported to the coordinates (not like a vehicle save script)

 

But now lets say, i want that the user just simply can add his own section to the INI file and the scripts reads it and use the new entry for the new defined vehicle.

 

For example

VEHICLE_MODEL = btype2
[SLOT1]
SLOT_POSX: 73588539803
SLOT_POSY: -785656685
SLOT_POSZ: 73588539803
[SLOT2]
SLOT_POSX: 73588539803
SLOT_POSY: -785656685
SLOT_POSZ: 73588539803
SLOT_COUNT = 2 <both slots getting used by the script>
NeedXY = true
godmode = false
invisible = false


VEHICLE_MODEL = infernus
[SLOT1]
SLOT_POSX: 53588539803
SLOT_POSY: -585656685
SLOT_POSZ: -5588539803
[SLOT2]
SLOT_POSX: 7878539803
SLOT_POSY: -785656685 >Not getting used because of SLOT Count 1<
SLOT_POSZ: 73588539803
SLOT_COUNT = 1
NeedXY = false
godmode = false
invisible = false
                                                                           
VEHICLE_MODEL = mercedesw123
[SLOT1]
SLOT_POSX: 53588539803
SLOT_POSY: -585656685
SLOT_POSZ: -5588539803
[SLOT2]
SLOT_POSX: 7878539803
SLOT_POSY: -785656685
SLOT_POSZ: 73588539803
SLOT_COUNT = 2
NeedXY = false
godmode = false
invisible = false

 

 

But here is my problem. All these values are the same. So how can i read them automaticly as single blocks and use the new defined entrys without define them in my script?

I know its possible, because someone else did this https://www.gta5-mods.com/scripts/dlc-vehicles-spawn-on-traffic

or just like in Menyoo.

 

thanks

 

Edited by MrGTAmodsgerman

Share this post


Link to post
Share on other sites
Jitnaught

Use an XML file instead, then you can do something like:

<vehicle>
<model>btype2</model>
<pos>XYZ</pos>
<godmode>true</godmode>
<invisible>false</invisible>
</vehicle>

<vehicle>
<model>infernus</model>
<pos>XYZ</pos>
<godmode>true</godmode>
<invisible>false</invisible>
</vehicle>

 

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
13 minutes ago, Jitnaught said:

Use an XML file instead, then you can do something like:

<vehicle>
<model>btype2</model>
<pos>XYZ</pos>
<godmode>true</godmode>
<invisible>false</invisible>
</vehicle>

<vehicle>
<model>infernus</model>
<pos>XYZ</pos>
<godmode>true</godmode>
<invisible>false</invisible>
</vehicle>

 

Ok, but how to do that in codewise? Any tutorial for XML files? Sorry i am not very skilled in this. What makes it automaticly read new entrys that are not defined?

Edited by MrGTAmodsgerman

Share this post


Link to post
Share on other sites
Jitnaught

Look up "c# xmldocument"

https://stackoverflow.com/a/55840

 

There are multiple ways to parse XML files in C# though. Look up "c# parse XML" to find all of your options.

Share this post


Link to post
Share on other sites
LeeC2202

@MrGTAmodsgermanTry this small project https://drive.google.com/file/d/1tralYo36VwXuW23l9-jBjTIQTrPV8H91/view?usp=sharing

 

It creates this as an output:

<?xml version="1.0" encoding="utf-8"?>
<Vehicles>
  <VehicleDefinitions>
    <VehicleDefinition>
      <Model>infernus</Model>
      <Position>
        <X>0</X>
        <Y>0</Y>
        <Z>0</Z>
      </Position>
      <GodMode>true</GodMode>
      <Invisible>false</Invisible>
    </VehicleDefinition>
    <VehicleDefinition>
      <Model>zion</Model>
      <Position>
        <X>0</X>
        <Y>0</Y>
        <Z>0</Z>
      </Position>
      <GodMode>true</GodMode>
      <Invisible>false</Invisible>
    </VehicleDefinition>
    <VehicleDefinition>
      <Model>comet</Model>
      <Position>
        <X>0</X>
        <Y>0</Y>
        <Z>0</Z>
      </Position>
      <GodMode>true</GodMode>
      <Invisible>false</Invisible>
    </VehicleDefinition>
    <VehicleDefinition>
      <Model>bodhi2</Model>
      <Position>
        <X>0</X>
        <Y>0</Y>
        <Z>0</Z>
      </Position>
      <GodMode>true</GodMode>
      <Invisible>false</Invisible>
    </VehicleDefinition>
  </VehicleDefinitions>
</Vehicles>

I use something similar to this for saving my Players Extended config data and the data for my Driver Info System mod.

 

You just have to be aware that there are some data types that won't serialise, like Color but the majority of things work okay. For Color, I just created a VColor that I convert to and from and that works fine.

 

The main rule for serialising objects, is that the class must have a parameterless constructor. So in the case of the class used in this for the VehicleDefinition, it looks like this:

 

    public class VehicleDefinition
    {
        public string Model;
        public Vector3 Position;
        public bool GodMode;
        public bool Invisible;

        public VehicleDefinition() { }

    }

Also, it will only serialise Public properties. You might want to read up on the XmlAttributes class for more info on controlling the serialisation process.

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
10 hours ago, LeeC2202 said:

Thx for the file. Now i understand that XML stuff a little bit more 😀

 

LqhE8RW.png

This part is the reason why i really created this topic. Because you working with predefined vehicles. But how can it be done without the exact definition. How can i read them as dynamic section?

In my example above, i just go ahead as "user" and add a block of lines with a definition for a mercedesw123, right? But here is the problem. The user can't change my script. So the script should automaticly read the new block with all his values.

So i need to work with something that doesn't exist. I am not quite sure if you understand my whole topic for the problem i have.

Well for example, Menyoo have a objectlist. There are some objectlist files you can download which add new entrys to the file which Menyoo doesn't have in when you download it.

But Menyoo knows that the second line of the object list, is a for example "modelname" and the next one is a bool.

 

Quote

You might want to read up on the XmlAttributes class for more info on controlling the serialisation process.

So is that what you meaning with my problem? Would that help me for that case?

I am sorry, i never builded a script which completely lives from a XML file that  only gets loaded by the script, instead of writing it.

 

My last idea would be to make a second function which writes the XML file by pressing a key. But that is not what i want. Because i saw that its possible without that.

 

Edited by MrGTAmodsgerman

Share this post


Link to post
Share on other sites
LeeC2202
3 hours ago, MrGTAmodsgerman said:

This part is the reason why i really created this topic. Because you working with predefined vehicles

No, it isn't working with predefined vehicles, I just made it load the same file back in as a demonstration. It loads whatever it finds... if it finds nothing, it loads nothing. If the file contains 1, 5, 10 or 50 definitions, it will load them all.

 

If you use that script to output the Test.XML class, manually edit it to add a new section and press the key to load the file. You will see the name of the new section appear on screen, along with the rest.

 

Basically, it will deserialise all objects contained in that XML file, no matter how they were added. So if you added this...

    <VehicleDefinition>
      <Model>dominator</Model>
      <Position>
        <X>0</X>
        <Y>0</Y>
        <Z>0</Z>
      </Position>
      <GodMode>false</GodMode>
      <Invisible>true</Invisible>
    </VehicleDefinition>

...as a new VehicleDefinition at the bottom, then the script would have 5 vehicles in the newVehicles collection. So the file could start off like this:

<?xml version="1.0" encoding="utf-8"?>
<Vehicles>
  <VehicleDefinitions>
  </VehicleDefinitions>
</Vehicles>

And that would create an empty collection, until something was added to it. It's exactly the same as adding a new <Item></Item> to handling.meta when you edit the game files, whatever it finds, as long as it is correct, it will add it to the collection.

 

3 hours ago, MrGTAmodsgerman said:

But Menyoo knows that the second line of the object list, is a for example "modelname" and the next one is a bool. 

No, it doesn't know that and it doesn't care about that. The object list is probably created from a serialised set of objects. So it knows the value is there, simply because the class (or Struct) it was created from had one

 

If you want to see how that works, in the bit where it loads the file back in, change the UI.Notify line to this:

UI.Notify(vd.Model + " / " + vd.GodMode + " / " + vd.Invisible);

Then change the order of the GodMode and Invisible lines in one of the vehicle definitions. Even though you have some that say:

      <GodMode>true</GodMode>
      <Invisible>false</Invisible>

and one that says:

      <Invisible>false</Invisible>
      <GodMode>true</GodMode>

It will still assign the correct values to the relevant properties.

Edited by LeeC2202

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
42 minutes ago, LeeC2202 said:

No, it isn't working with predefined vehicles, I just made it load the same file back in as a demonstration. It loads whatever it finds... if it finds nothing, it loads nothing. If the file contains 1, 5, 10 or 50 definitions, it will load them all.

 

If you use that script to output the Test.XML class, manually edit it to add a new section and press the key to load the file. You will see the name of the new section appear on screen, along with the rest.

 

Basically, it will deserialise all objects contained in that XML file, no matter how they were added. So if you added this...

 

It will still assign the correct values to the relevant properties.

Okey. Well, for my english, i need to play around with this first, to make sure that i understand what you wrote. Thanks.

Share this post


Link to post
Share on other sites
MrGTAmodsgerman

@LeeC2202 Well, basically i understand now how it works. But my way to filter the vehicle models from the xml in the nearby vehicles, doesn't work.

 

            Ped Spieler = Game.Player.Character;
            {
                if (MarkerGameTimeRef < Game.GameTime)
                {
                    MarkerGameTimeRef = Game.GameTime + 10;
                    if (Spieler.IsInVehicle())
                    {
                        foreach (Vehicle nearbyVehicle in World.GetNearbyVehicles(Spieler, 40f))
                        {
                            Vehicles newVehicles = XMLExtensions.XMLDeserialise<Vehicles>(SettingsFilePrefix + "test.xml");

                            foreach (VehicleDefinition vd in newVehicles.VehicleDefinitions)
                            {

                                String Automodel = vd.Model.Trim().ToString();
                                if (nearbyVehicle.Model.ToString() == Automodel)
                                {
                                    UI.Notify(Automodel);

                                    UI.Notify(vd.Model);
                                    nearbyVehicle.AddBlip();
                                    nearbyVehicle.CurrentBlip.IsFlashing = true;
                                }
                            }

Its on Tick.

It doesn't always really work. The game uses a other if statement after that (ignore the part), or all vehicles get blips around me and the minimap starts to get bigger than usual and flickers.

What i did i forgot? Or why can't this script compare the vehiclenames around me with the vehicles from the XML?

 

All i want to do is just doing task with vehicles around me that are only mentioned in the XML file. Thats all.

Share this post


Link to post
Share on other sites
LeeC2202

Don't deserialise XML in your onTick, that's going to be bad. Deserialising is like reading an ini file, you do it once.

 

So you deserialise the XML, that will give you a collection of Vehicles. You then pull out all the vehicle models into a List<string>. Then, in your onTick, you compare the vehicles around you with that list.

 

And if you want to make sure you only do something with a vehicle once, store the Vehicle.Handle in a List<int> and compare with that as well. Although if you do that, you will have to make sure that the vehicle still exists, or your list will just get bigger and bigger, as you add more handles to it.

 

I tend to create a Dictionary<int, Vehicle> and then every now and again, run through the Dictionary vehicles to see if they still exist or not, or do a distance check on them or something. Any process you are happy with really, that just manages the collection size.

Edited by LeeC2202

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
37 minutes ago, LeeC2202 said:

Don't deserialise XML in your onTick, that's going to be bad. Deserialising is like reading an ini file, you do it once.

But what else then? I don't want to use onKey events. And anything else like creating a void, require it to mention the void in a OnTick event or onKeyup/Down. Or did i get something wrong?

Share this post


Link to post
Share on other sites
LeeC2202

I don't really understand that.

 

If you were reading a normal ini file, that stored a key to use, how would you do it? Would you read the ini file when the mod first loaded, store the value in an ActivationKey property, and then check against that in your onKeyUp (or Down) with

if (e.KeyCode == ActivationKey)
{
    // Do something...
}

Or would you read the ini file every time you pressed a key and compare the value with what is in there?

 

That's effectively the same situation as you've got here. You always want to work on the basis of "Read file data once, compare memory data many times". If you do that through a function (or a void as you and Tobsi seem to refer to them as), it's not a problem. Functions help you to organise and structure your code, they encourage re-usability, so use them whenever you can.

 

So if this was me, I would have my Initialise() function read in the XML file into a Global List<Vehicles>. Then I would have a Global List<string> that I would fill with a foreach loop, like this.

foreach (VehicleDefinition vd in MyVehicles.VehicleDefinitions)
{
    VehicleModels.Add(vd.Model.Trim());
}

This is all being done in your Initialisation, so we haven't even hit onTick yet, I also have my Dictionary<int, Vehicle> CollectedVehicles that I am going to use for collected vehicles.

 

Now in your onTick, inside here, I call my CollectVehicles() function

if (Spieler.IsInVehicle())
{
	CollectVehicles();
}

and CollectVehicles() is this

void CollectVehicles()
{
	Vehicle[] nearbyVehicle = World.GetNearbyVehicles(Spieler, 40f));
	
	foreach (Vehicle veh in nearbyVehicles)
	{
		// If the list contains this model string, then it's a matching vehicle
		if (VehicleModels.Contains(veh.Model.ToString()))
		{
			if (!CollectedVehicles.ContainsKey(veh.Handle))
			{
				// This is a unique vehicle, so we store it as collected and do whatever else you need to do
				CollectedVehicles.Add(veh.Handle, veh);
				// Do some Blip stuff here etc...
			}
			else
			{
				// Vehicle is already collected, so we don't need to do anything with it... unless you want to do something.
			}
		}
	}
}

Don't forget, Vehicle.Model is a string like this 0xB779A091, not the name you type to spawn it. So to be honest, I would read the XML and convert all the model names into an int  with

Game.GenerateHash(vd.Model.Trim());

And then store those in a List<int> and compare with Vehicle.Model.Hash, instead of  Vehicle.Model.ToString() in the CollectVehicles() function.

 

So you end up with this:

// Global variables
List<int> VehicleModelhashes = new List<int>();
Dictionary<int, Vehicle> CollectedVehicles = new Dictionary<int, Vehicle>();
Vehicles MyVehicles;
  
// Call once when the mod runs
void Initialise()
{
	MyVehicles = XMLExtensions.XMLDeserialise<Vehicles>("scripts\\test.xml");
	
	foreach (VehicleDefinition vd in MyVehicles.VehicleDefinitions)
	{
		VehicleModelHashes.Add(Game.GenerateHash(vd.Model.Trim()));
	}
}

// In onTick
if (Spieler.IsInVehicle())
{
	CollectVehicles();
}

void CollectVehicles()
{
	Vehicle[] nearbyVehicles = World.GetNearbyVehicles(Spieler, 40f));
	
	foreach (Vehicle veh in nearbyVehicles)
	{
		// If the list contains this model hash, then it's a matching vehicle
		if (VehicleModelhashes.Contains(veh.Model.Hash))
		{
			if (!CollectedVehicles.ContainsKey(veh.Handle))
			{
				// This is a unique vehicle, so we store it as collected and do whatever else you need to do
				CollectedVehicles.Add(veh.Handle, veh);
				// Do some Blip stuff here etc...
			}
			else
			{
				// Vehicle is already collected, so we don't need to do anything with it... unless you want to do something.
			}
		}
	}
}

 

I wrote that code in Notepad++, so don't be surprised if there's an error or three. :D

 

Edit: Just fixed some errors in that last code section... sorry. :blush:

Edited by LeeC2202

Share this post


Link to post
Share on other sites
LeeC2202

Sorry, one last function... every 30 seconds, or every minute, do this in onTick.

void CheckIfVehiclesStillExist()
{
	// you will need a reference for System.Linq for this next line.
	List<int> keys = CollectedVehicles.Keys.ToList();
	
	// Go through the CollectedVehicles collection and check if the vehicles are still in the game world
	foreach (int handle in keys)
	{
		if (CollectedVehicles[handle] == null)
		{
			CollectedVehicles.Remove(handle);
		}
		else
		{
			if (!CollectedVehicles[handle].Exists() || CollectedVehicles[handle].IsDead)
			{
				CollectedVehicles.Remove(handle);
			}
		}
	}
}

That will remove any vehicles from the collection that no longer exist.

Edited by LeeC2202

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
2 hours ago, LeeC2202 said:
if (Spieler.IsInVehicle())
{
	CollectVehicles();
}
void CollectVehicles()
{
	Vehicle[] nearbyVehicle = World.GetNearbyVehicles(Spieler, 40f));
	
	foreach (Vehicle veh in nearbyVehicles)
	{
		// If the list contains this model string, then it's a matching vehicle
		if (VehicleModels.Contains(veh.Model.ToString()))
		{
			if (!CollectedVehicles.ContainsKey(veh.Handle))
			{
				// This is a unique vehicle, so we store it as collected and do whatever else you need to do
				CollectedVehicles.Add(veh.Handle, veh);
				// Do some Blip stuff here etc...
			}
			else
			{
				// Vehicle is already collected, so we don't need to do anything with it... unless you want to do something.
			}
		}
	}
}

 

 

Do i forget something?

Because i tried to play around now with this, fixed some little errors you made and the game do nothing 🤔

        private void onTick(object sender, EventArgs e)
        {
            // Exits from the loop if the game is loading
            if (Game.IsLoading) return;
            //Bikefahren();
            if (Game.Player.Character.IsInVehicle())
            {
                CollectVehicles();
                Wait(30000);
                CheckIfVehiclesStillExist();
                UI.Notify("onTick Event Ende");
            }

        }

Well, you may do a facepalm now, so i am sorry. I have no idea to put "foreach" combined with "wait" into one. I am a damn noob.

But anyways, the UI gets displayed as you can see here, and the game laggs for a second when i reload the script. But thats all.

 

But what about the Initialise function? As far as i know, no void is running when it isn't in any onTick or KeyDown/Up event. So thats my idea why nothing happends.

But should i also mention it in OnTick?

        public cXMLTest()
        {
            Tick += onTick;
            KeyUp += onKeyUp;

            Interval = 100;
        }


        // Call once when the mod runs
        void Initialise()
        {
            MyVehicles = XMLExtensions.XMLDeserialise<Vehicles>("scripts\\test.xml");

            foreach (VehicleDefinition vd in MyVehicles.VehicleDefinitions)
            {
                VehicleModelHashes.Add(Game.GenerateHash(vd.Model.Trim()));
            }
        }

EDIT: Well, i put Initialise now into on Tick with a UI notify message. It only runs in onTick. But it doesn't make the script to work

 

 

        void CollectVehicles()
        {

            Vehicle[] nearbyVehicle = World.GetNearbyVehicles(Spieler, 40f);

            foreach (Vehicle veh in nearbyVehicle)
            {
                // If the list contains this model hash, then it's a matching vehicle
                if (VehicleModelHashes.Contains(veh.Model.Hash))
                {
                    if (!CollectedVehicles.ContainsKey(veh.Handle))
                    {
                        // This is a unique vehicle, so we store it as collected and do whatever else you need to do
                        CollectedVehicles.Add(veh.Handle, veh);
                        // Do some Blip stuff here etc...
                        veh.AddBlip();
                        veh.CurrentBlip.IsFlashing = true;
                        UI.Notify(veh.Model.ToString());
                    }
                    else
                    {
                        // Vehicle is already collected, so we don't need to do anything with it... unless you want to do something.
                    }
                }
            }
        }

Is veh as collected vehicle the right one?

 

EDIT: Btw. I want to load the XML file when the mod is loaded, without pressing anything on the keyboard.

 

Edited by MrGTAmodsgerman

Share this post


Link to post
Share on other sites
LeeC2202

If something says "Run once" the last thing you should be doing, is putting it into something that runs every frame.

 

41 minutes ago, MrGTAmodsgerman said:

EDIT: Btw. I want to load the XML file when the mod is loaded, without pressing anything on the keyboard.

That's what a constructor is for, or if you don't like using the constructor for any reason, then just wrap a piece of code in a bool.

// Global variable
bool RunOnce = false;


// in OnTick
if (!RunOnce)
{
	// Do things here that only happen once...
	
	// ... then set this flag to stop it running again
	RunOnce = true;
}

Maybe I am just not explaining things clearly enough, I don't know. I thought you were familiar with Global variables, using constructors and running functions in onTick.

 

I will put the whole thing into a working project, which should make things easier to understand.

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
7 minutes ago, LeeC2202 said:

If something says "Run once" the last thing you should be doing, is putting it into something that runs every frame.

 

That's what a constructor is for, or if you don't like using the constructor for any reason, then just wrap a piece of code in a bool.

// Global variable
bool RunOnce = false;


// in OnTick
if (!RunOnce)
{
	// Do things here that only happen once...
	
	// ... then set this flag to stop it running again
	RunOnce = true;
}

 

Sorry i thought you was meaning that its possible to run everything without mention all voids in the onTick event.

So you mention only CollectVehicles void in the OnTick Event. Hmm i will watch some more tutorials for that. Hope that helps.

But like i said. Running all voids in the onTick event, doesn't do anything better except the UI notification i wrote into each void, getting displayed :/

Having only collectvehicles onTick doesn't make the script to work. What did i wrong then?

Edited by MrGTAmodsgerman

Share this post


Link to post
Share on other sites
LeeC2202

Here's a fully working project, complete with blips, and notifications so you can see pretty much all that happens. https://drive.google.com/file/d/1tralYo36VwXuW23l9-jBjTIQTrPV8H91/view?usp=sharing

 

I also remembered to add the cleanup code for the Blips... something that is very important as Blips can cause all sorts of problems.

 

I included my test.xml file as I had been messing adding other values, so it's just something that is completely compatible with the Vehicle Definitions the project expects.

 

*sigh* Maybe my vehicle checking code isn't fully working, it's not deleting the blips. :(

 

I don't think I can do anything more tonight, I think my head is screwed from stressing over finite state machines in a mod I am writing. It's been almost 15 years since I used them and I can't remember fully how they work.

 

Edit: God I am so stupid sometimes, add this line at Line 143, before the line that says CarBlips.Remove(key);

if (CarBlips[key].Exists()) CarBlips[key].Remove();

I wasn't actually removing the Blip, just the entry in the Dictionary... time for bed I think. :(

Edited by LeeC2202

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
3 hours ago, LeeC2202 said:

 

*sigh* Maybe my vehicle checking code isn't fully working, it's not deleting the blips. :(

 

I don't think I can do anything more tonight, I think my head is screwed from stressing over finite state machines in a mod I am writing. It's been almost 15 years since I used them and I can't remember fully how they work.

 

Edit: God I am so stupid sometimes, add this line at Line 143, before the line that says CarBlips.Remove(key);

if (CarBlips[key].Exists()) CarBlips[key].Remove();

I wasn't actually removing the Blip, just the entry in the Dictionary... time for bed I think. :(

Well that is what i was about to tell. Never mind, you helped me alot already. I just also tried to draw a marker on the catched vehicle, but i only can see the marker 1 milli second when i reload the scripts.

Another day, another try ;) good night

 

EDIT: There is some real big other question i have. Is it possible to access defined functions from one void to another void? I mean. I want to access for ex. "blipKeys" list in the CollectedVehicles void for example. But i always need to define it as parameter above. Thats what Visual Studio says. But thats not working when i try to access "Spieler = Game.Player.Character" or something like that in one void where "Spieler" is defined in a another one.

 

Edited by MrGTAmodsgerman

Share this post


Link to post
Share on other sites
LeeC2202
5 hours ago, MrGTAmodsgerman said:

EDIT: There is some real big other question i have. Is it possible to access defined functions from one void to another void? I mean. I want to access for ex. "blipKeys" list in the CollectedVehicles void for example. But i always need to define it as parameter above. Thats what Visual Studio says. But thats not working when i try to access "Spieler = Game.Player.Character" or something like that in one void where "Spieler" is defined in a another one.

It sounds like you need to go over some programming basics before you go much further. I have just tried to find a video to explain it and this was about the best of a very bad bunch.

 

 

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
6 hours ago, LeeC2202 said:

It sounds like you need to go over some programming basics before you go much further. I have just tried to find a video to explain it and this was about the best of a very bad bunch.

thx. Will it also help me to create the marker without disappearing?

Share this post


Link to post
Share on other sites
LeeC2202
44 minutes ago, MrGTAmodsgerman said:

Will it also help me to create the marker without disappearing?

Markers have to be drawn every frame to stay visible.

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
24 minutes ago, LeeC2202 said:

Markers have to be drawn every frame to stay visible.

Ok thanks for the video. so

    public class cXMLTest : Script
    {
        // Global variables
        List<int> VehicleModelHashes = new List<int>();
        Dictionary<int, Vehicle> CollectedVehicles = new Dictionary<int, Vehicle>();
        Vehicles MyVehicles;
        public Ped Spieler = Game.Player.Character;
        Dictionary<int, Blip> CarBlips = new Dictionary<int, Blip>();

doing it like that is the right way, right? I mean "Spieler"

Share this post


Link to post
Share on other sites
LeeC2202
14 minutes ago, MrGTAmodsgerman said:

doing it like that is the right way, right? I mean "Spieler"

Yep, that's right. It doesn't need to be public but in this case, it's probably not going to cause you any problems being like that.

 

That being said, I would put "accessibility" on your list to find out about as well. That will teach you about private, public, internal... that kind of thing. That kind of thing does become important when you start using multiple classes in a mod.

 

Edit: This page is quite helpful for that. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/accessibility-levels

Edited by LeeC2202

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
12 minutes ago, LeeC2202 said:

Yep, that's right. It doesn't need to be public but in this case, it's probably not going to cause you any problems being like that.

 

That being said, I would put "accessibility" on your list to find out about as well. That will teach you about private, public, internal... that kind of thing. That kind of thing does become important when you start using multiple classes in a mod.

Ok, so what is the right way now to remove the blip? Define your checkvehiclesexist function carblip remove for public use? Because veh.currentBlip.remove doesn't still work, because you set the blip to his current position. So not attached to the vehicle. Which isn't bad because cost less memory. But then, i need to use CarBlips.Remove which also doesn't do anything, because its the List. And when i try to set it flashing to the blip, then i need to use veh.currentBlip.isflashing, true. So what is the right way to get access?

I already include the missing line for blips, which fixes the blip removement in the void of checkvehiclesexist.

 

Edit: Well thats how i like it. The link u send explains everything short :D

Edited by MrGTAmodsgerman

Share this post


Link to post
Share on other sites
LeeC2202

Once you attach a blip to an entity, you can't remove it unless the entity is removed, you have to set the Alpha level to zero to hide it. That's why I create blips at the position instead. If I wanted it to follow the vehicle, I would update the Blip.Position in an onTick function. I think in the game, Blips are designed to be attached and left alone and they will get removed once something has happened to the entity they are attached to. But modding let's us use them differently and that's when we run into problems like this.

 

Inside a class, nothing has to be public for the rest of the class to have access to it. Public (or internal) is only used when another class needs access to that property from outside.

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
1 minute ago, LeeC2202 said:

Once you attach a blip to an entity, you can't remove it unless the entity is removed, you have to set the Alpha level to zero to hide it. That's why I create blips at the position instead. If I wanted it to follow the vehicle, I would update the Blip.Position in an onTick function. I think in the game, Blips are designed to be attached and left alone and they will get removed once something has happened to the entity they are attached to. But modding let's us use them differently and that's when we run into problems like this.

 

Inside a class, nothing has to be public for the rest of the class to have access to it. Public (or internal) is only used when another class needs access to that property from outside.

So i don't want to attach it. But do i need to attach it first to let it flashing for example? And then dettach it? I only want to remove it, thats all

Share this post


Link to post
Share on other sites
LeeC2202
8 minutes ago, MrGTAmodsgerman said:

So i don't want to attach it. But do i need to attach it first to let it flashing for example? And then dettach it? I only want to remove it, thats all

I have never used flashing blips, so I honestly don't know about that... I would think any blip can flash though.

 

You can't detach a blip, that's the problem. If you notice, there is an Entity.AddBlip but not an Entity.RemoveBlip, once it's attached, I think it's attached for the life of the entity.

 

Edit: I should add, anything regarding Blips is just based on my own experience with them. There may very well be ways of doing things with them that I don't know about.

Edited by LeeC2202

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
1 minute ago, LeeC2202 said:

I have never used flashing blips, so I honestly don't know about that... I would think any blip can flash though.

 

You can't detach a blip, that's the problem. If you notice, there is an Entity.AddBlip but not an Entity.RemoveBlip, once it's attached, I think it's attached for the life of the entity.

So what option do i have now? Forget about the flashing and attach stuff. I want to remove it

Share this post


Link to post
Share on other sites
LeeC2202
1 minute ago, MrGTAmodsgerman said:

So what option do i have now? Forget about the flashing and attach stuff. I want to remove it

Well you already know how to remove a blip that isn't attached to anything, you already have the code for that.

Share this post


Link to post
Share on other sites
MrGTAmodsgerman
5 minutes ago, LeeC2202 said:

Well you already know how to remove a blip that isn't attached to anything, you already have the code for that.

But like i said. It doesn't work.

Share this post


Link to post
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

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

×

Important Information

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