AS3.0 Using arrays for game inventory and how to track objects?

Started by
20 comments, last by jeteran 11 years, 7 months ago
Hey guys, it’s good to be around here.

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

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:
[s]- 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.startDrag() but says that the object is null, why?[/s]
- 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 !!
Advertisement
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 !!
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(sceneObject1)) {

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 !!
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.
Hi everyone.

So far so good. What I have done so far is this:
1) Create an array called mission1 that holds every object of that mission.
2) Create an array called inventory that holds all the inventory objects.

Now, this is getting a little more complicated.

The thing is that I want to be able to recognize the type of the inventory object I'm actualy using in the mission. And that way too, will help me pass the objects from the game store to the mission.

So I though I will have to change the inventory array to a 2D array, that can store the "id" of the object and the "name" of the object.

Am I correct with this?

Thanks so much, really hope you can give me ANY guidance about the subject.
I'm not sure that I am completely understanding what you are trying to do, specifically in regards to a 2D Array.

Just create an GameObject class or object and push that into the single dimensional array.

for example :

var item:Object = {id:124, name:"axe"}
inventory.push(item);

** using the squiggly braces "{}" is shorthand for creating a new object.



I used an object, but I'd think that you'd want to have a class for all game/inventory objects that would include any information about that object.
Prototypical, thanks so much for your response.

So far so good in that way (using an array) and I'm able to call them each time I use them over an game object.

Now, if I change the inventory to an object, how can I handle it?

For example, with the array, I created a FOR that reads from X to Y number I use inventory to track them for each objects in the game scene (which are in a array too). In that case, how can I manipulate the inventory() object to interact with objects in the game scene? Should I use an object() for the game scene too?

Thanks again man , everything is working out really great !!
I don't understand what you are asking.

I just suggested not using a multidimensional array to track inventory, but instead to use a single dimensional array containing objects.
Prototypical thanks for your reply and sorry that I wasn't more clearly.

I have changed a little bit thanks to you. I'm working now on creating a function that has all the properties of objects, like _id, _name, _cost and so on. Then, in another function, I have the objects that catch the other function's properties. Then I call thos objects in the store.

What do you think about it? Is still better to use objects? If so, how can I get the valures from inside them? like, in your example, how can I get the "name" from item, just intem.name ?

Thanks once again !!
Not sure what you mean by a function with properties. Do you mean you are creating a class ?

This topic is closed to new replies.

Advertisement