Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Inventory System


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 mstriker   Members   -  Reputation: 100

Like
0Likes
Like

Posted 15 February 2012 - 12:09 PM

Hello,

I find myself trying to script a realistic inventory system, and I am having trouble, so I thought this was the best place to ask for aid. Basically it's a multi-player game, "hardcore Roleplaying" (basically it's actually with a local chat and Roleplaying rules), it's in the modern era, therefore, for example, people can steal a badge from a police officer and use it to fake being a cop, people can craft stuff if they are in the primary sector (agriculture).

Therefore, each item will have different values, as of badges, IDs, vehicle registrations and ownerships, everything may be robbed and everything can be shared.

I have been trying to figure the best way to do this, maybe anyone has something in mind?

Thank you in advance for your unconditional help,

Matthew Striker.

Sponsor:

#2 menyo   Members   -  Reputation: 485

Like
0Likes
Like

Posted 15 February 2012 - 12:39 PM

It helps if you tell us in what language you are working.

But i would simply add and remove the items from a binary file stored on the server to prevent cheating. You just have to serialize the item class and add it to a inventory XML file belonging to the player, once used or dropped remove it.

Current Project: TechnoFlux read all about it on my

DEV BLOG


#3 mstriker   Members   -  Reputation: 100

Like
0Likes
Like

Posted 15 February 2012 - 12:53 PM

It's PAWN for San Andreas Multiplayer, it's kind of like C, but I am experienced on the language and If you guys talk to me in another language like C, C#, C++ I can script it into PAWN. I honestly just need the basics on what variables to use and what functions would be needed, specially in unique items like badges that can be changed (like, my name is deskoft, I can give deskoft's ID to another player, and he can fake being deskoft).

#4 menyo   Members   -  Reputation: 485

Like
1Likes
Like

Posted 15 February 2012 - 01:51 PM

Well i can help you with writing/reading XML data in C#.

Let's say this is your item class that uses a string for the type of the item and a int for the player ID.

public class TheData
	{
		public string SomeName;
		public int SomeNumber;
	}

Here is a simple class i could serialize this data into xml and load it back in.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

class Serializer
	{
		TheData data = new TheData();
		public Serializer(string theName, int theNumber)
		{
			data.SomeName = theName;
			data.SomeNumber = theNumber;
		}
		public void Save(string fileName)
		{
			Stream stream = File.Create(fileName);
			XmlSerializer serialize = new XmlSerializer(typeof(TheData));
			serialize.Serialize(stream, data);
			stream.Close();
		}
		public static TheData Load(string fileName)
		{
			Stream stream = File.OpenRead(fileName);
			XmlSerializer serialize = new XmlSerializer(typeof(TheData));
			return (TheData)serialize.Deserialize(stream);
		}
	}

Current Project: TechnoFlux read all about it on my

DEV BLOG


#5 mstriker   Members   -  Reputation: 100

Like
0Likes
Like

Posted 15 February 2012 - 01:57 PM

Well i can help you with writing binary data in C#.

Let's say this is your item class that uses a string for the type of the item and a int for the player ID.

public class TheData
	{
		public string SomeName;
		public int SomeNumber;
	}

Here is a simple class i could serialize this data into xml and load it back in.

class Serializer
	{
		TheData data = new TheData();
		public Serializer(string theName, int theNumber)
		{
			data.SomeName = theName;
			data.SomeNumber = theNumber;
		}
		public void Save(string fileName)
		{
			Stream stream = File.Create(fileName);
			XmlSerializer serialize = new XmlSerializer(typeof(TheData));
			serialize.Serialize(stream, data);
			stream.Close();
		}
		public static TheData Load(string fileName)
		{
			Stream stream = File.OpenRead(fileName);
			XmlSerializer serialize = new XmlSerializer(typeof(TheData));
			return (TheData)serialize.Deserialize(stream);
		}
	}

Using these .net references in the serialization class.

using System.Xml;
using System.Xml.Serialization;


Thank you very much, I'll give that a try and see how I go.

#6 menyo   Members   -  Reputation: 485

Like
1Likes
Like

Posted 15 February 2012 - 02:18 PM

I updated the code, you'll need more references like System.IO for handling files.

Current Project: TechnoFlux read all about it on my

DEV BLOG


#7 mstriker   Members   -  Reputation: 100

Like
0Likes
Like

Posted 15 February 2012 - 02:27 PM

How would we load the item if the player is offline though?

#8 menyo   Members   -  Reputation: 485

Like
1Likes
Like

Posted 15 February 2012 - 03:55 PM

With the Load method?

You don't need someones inventory if someone is offline right? If you do want that then the filename should not be thisplayer.xml but otherplayer.xml. You could also make a dictionary that uses userID's that point to filenames or something like that. You could serialize that dictionary too and do something like this:

1. Player wants to do something with another persons inventory:
2. Load inventory dictionary.
3. Lookup that persons ID in the dictionary.
4. Load the inventory attached to this ID.
5. Do something with that inventory.

Current Project: TechnoFlux read all about it on my

DEV BLOG


#9 mstriker   Members   -  Reputation: 100

Like
0Likes
Like

Posted 15 February 2012 - 04:17 PM

That is the problem, we are trying to script exchangable items. Include IDs, maybe car plates that you can craft into the cars or send them to a specialized place, basically if the player has another player's item, the other player won't have it back, if it's stolen the police will have to find out.

#10 menyo   Members   -  Reputation: 485

Like
1Likes
Like

Posted 15 February 2012 - 04:35 PM

Well, since you always know from what player your going to steal something you have it's ID. You just need to find a way to link that ID to the file that holds his inventory like a dictionary. Or call your files something like inventory_<ID_NR>.xml.

Then you just remove the Stolen object from that user and add it to the user that stole it and tag it stolen within the item object so the police have a way to catch this petty criminal :D.

You can also serialize lists so when you have an item class and give each playerclass a List<item> inventory; you can save this to XML. Then when something needs to be removed and added to another players inventory list. you just switch it to another players list.

Current Project: TechnoFlux read all about it on my

DEV BLOG





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS