Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Gaming and Game Development

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-22-2012, 11:29 PM   #1
jeteran
Registered User
 
Join Date: Aug 2012
Posts: 23
Default [AS3] Using arrays for game inventory and how to track objects?

Hey guys, it’s good to be around here once again.

I’m facing a little problem that has to be with arrays in the game. This is what I want to do and how I plan to do it, and my results. I want to be as clear as possible, really hate when people just say “help” and paste their code :S And too, because I hope will help any other people that are asking the same questions.

I’m trying to make a very simple inventory system. Keep in mind that I already check some of them on the Internet, but it really bothers me to just “copy-paste” code that it’s not mine. So I’m using them for learning purpose. I want to go step by step, so for now I will concentrate only in the inventory (not the buying system)

Ok now, I have 2 scenes in the game. The briefing scene where you can buy goods and save them in your inventory, and the other one is the game scene where you use the objects you bought.

In the briefing scene, I want to save the objects I bought in my inventory, for this I think is best use a fixed array. That array has always 4 slots.

What I want to do is save that array and passed it to the game scene and put them in another array.

With this (I hope) clear explanation, I have some questions:
- Can I use only one array for that inventory? So I save the objects in the briefing scene and call it in the game scene?
- With a future buy system, (that I think it will be another array) what can be the best method to pass them to the inventory array? With just push (or slice?)?


Now, about the game scene:
With the objects in the inventory, I want to be able to drag them to objects in the game scene (table, computer, etc) and, if the inventory objects hitTest the table AND the character, drop it. I’m working this in a different file than the briefing scene BTW.

Now I have some Qs:
- I have 3 objects inside an array called invObjects. To put them in the scene, I used a simple FOR that tells the program to put them in X order. They do. Just after this, I added an event listener to know where the invObject is clicked. In that function, I did: invObjects[i].startDrag() but says that the object is null, why?
- When that object is used correctly in the scene, I want it to pop(). If I pop it, those the other objects stay where they are? Pop it is the best method to be used?


I’ll appreciate ANY suggestion you give me. Anyway I’m working on all this.

Thanks so much guys for your help !!

Last edited by jeteran; 08-22-2012 at 11:32 PM.
jeteran is offline   Reply With Quote
Old 08-22-2012, 11:29 PM   #2
jeteran
Registered User
 
Join Date: Aug 2012
Posts: 23
Default

Hey guys, once again.

So far so good ! I'm working right now in the game scene and this is what I did:

1) I create 2 "game objects" and linked them in AS.
2) I create 4 "inventory objects" that are inside an array.
3) I was having problems with all the object's depth, so what I did is push to the end the "game objects" and bring to front the inventory objects.
4) I create 2 listeners for the "inventory objects", one MOUSE_DOWN and the other MOUSE_UP. Mouse down calls startDrag() functon and mouse up calls stopDrag().
5) Was tinny nightmare to manipulate each objects inside the array; i found out that "event.currentTarget." does a great job. To set the objects' depth, I used "event.currentTarget.valueOf()" because I found out that valueOf() returns an object.

With this structure, am I going to a good place? I believe in results but I want to achieve them in a very clean and good way.

Ok what I'm going to try now is that, when the "inventory object" touches "scene object", make "inventory object" disappear. I think I will use another listener to this.

I will appreciate any suggestions for the Qs i put in the beginning.

Thanks for your help !!
jeteran is offline   Reply With Quote
Old 08-22-2012, 11:30 PM   #3
jeteran
Registered User
 
Join Date: Aug 2012
Posts: 23
Default

Hi hi.

Ok based on what I was trying to do ("inventory object" touching "scene objects") what I did is just create a listener like this:
if (event.currentTarget.valueOf().hitTestObject(scene Object1)) {

If it does, I disappear the "inventory object". Now my question is:
- is this a good method to get rid of that object? Or how do I kill it?

I'm going to try now is that, if the object doesn't touch the "scene objects", go back to it's initial position.
BTW, how can I add all the "scene objects" in an array? I mean, the way that the array of "inventory objects" can interacte with the "scene objects"? This is because I will have tons of IFs if I want the "inventory objects" to act with the "scene objects"

Thanks so much guys !!
jeteran is offline   Reply With Quote
Old 08-22-2012, 11:31 PM   #4
jeteran
Registered User
 
Join Date: Aug 2012
Posts: 23
Default

Hello again guys.

I'm happy tell you that I accomplished in taking back the "inventory object" to his place if is not left in the "scene object"

Ok now the thing is that I will have around 7 objects in 3 different room. How can I handle that?

I thought about 3 things:
- Have an array for the missions, rooms and inventory items separately. Total 3 arrays.
- Because it will be more complex (will have like 3 missions, each mission with 3 different rooms and each room with 3 "scene objects), I thought that I can create an 2D array that will have the mission and their rooms. If I do this, can I track the inventory objects with the second part of that array?
- Have a 3D array that has the missions, the rooms and the inventory. The problem here is that the inventory objects are dinamic, it doesn't matter?

You have ANY other suggestion? Or what is the best of those 3?

Thanks so much guys, let's keep doing a great job.

Last edited by jeteran; 08-22-2012 at 11:35 PM.
jeteran is offline   Reply With Quote
Old 08-26-2012, 02:52 AM   #5
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,507
Default

Sorry there's too much to read for me but I'll tell you how I make dynamic inventory systems. First of all I use Vector only and not array especially for this. I also use intensively Interface to give types to all inventory object possible (so that more object can be created later and still handled by the system). One object can have many type depending on what it does. Those objects can give you their position, size, state, strength, and so on depending on what the 'master' Interface requires (IInventoryObject). This process needs to be very organized but at the end the system is not only efficient and solid but can also accept new created objects.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 08-26-2012, 01:23 PM   #6
jeteran
Registered User
 
Join Date: Aug 2012
Posts: 23
Default

Hey ASWC, thanks SO much for your reply.

What you say makes total sense. I have to tell you that I'm a little new to all this and I really hope that you can tell me technically how to achieve this.

Even though, I want to try to do it by myselft. This is what I catch:

I have a class called ObjectsProperties that has all the properties of the objects (say id, name, description, cost, etc). Then, I have another class called GameObjects where I create all the objects of the game and they catch the properties from ObjectsProperties.

Then in the main file, I create an array called Store, then I push() the objects I want to display in the game. I have too an Inventory array that will catch the objects pushed by the Store once the player clicks.

That makes sense? If this is somehow similar of what you told me, the only thing left is, instead of using arrays, use vectors. How can I handle them to achieve all this??

Once again, thanks SO much ASWC for your guidance.
jeteran is offline   Reply With Quote
Old 08-27-2012, 12:09 AM   #7
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,507
Default

One thing about Vectors. In AS3 the superclass of all object is the class Object. In a way an array is just like a Vector in that it can store anything that is an Object (the superclass of all objects) but that's all you can know about what you put in an array: they are Object. With a Vector you define the object type that this Vector can deal with, let's say IIventoryObject type. Now this vector can store all object of this type including all subclass of that class if any. Here again the vector behaves like an array in that it deals with a superclass and all its subclasses expect that t is not anymore the superclass Object. So unlike array now you know exactly what's stored in your vector and so no need for type checking, casting and so on. In an inventory system the inventory can have a vector storing all object in the inventory, all those object sharing a common object type (eg: IInventoryObject) which is the type the vector can deal with. Suddenly sorting, dealing with, performing actions, running methods, etc, on all those objects becomes very easy and in term of coding you create a strong and flexible code.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 08-27-2012, 12:47 AM   #8
jeteran
Registered User
 
Join Date: Aug 2012
Posts: 23
Default

This is awesome ASWC, I got it right !!

I'm going to start researching more on vectors and try to modify my coding habits to have a better and solid code !!

Thanks so much for your guidance !!
jeteran is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:15 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.