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:
- 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 !!
Sorry, I didn't read all of your posts. But I can answer you on first questions:
First, can you use objects for storing player items? Of course, yes. ActionScript has one specific feature: all objects are passed by reference. So, if you created inventory item, that item will always mean the same item.
The best way is to instantiate each player inventory into special object and store each inventory item as instance of custom class.
When you use item as custom item instance, you can do what you want - trade, store, destroy item and etc.
What do you mean about slice or push? Item must have owner (null if it is dropped item), slicing arrays for moving item? Nop, not a good idea.
Method pop() is stack method. If you don't know, what does it mean - read manuals about stack architecture. When you pop item, you take last pushed item from array. Why you need it for player inventory? If you want to change item owner - it is not best way. If you want to implement "Use item", stack.pop() it is not a best way too.
You need just a custom Inventory class and custom Item class. Then you'll be able to do with items what do you want.
I have a practice in server-side game development for managed languages, and in that case, items should have owners - its just much easier to manipulate with such items, than moving items from one array to another.