Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


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
21 replies to this topic

#1 JETeran   Members   -  Reputation: 265

Like
0Likes
Like

Posted 22 August 2012 - 03:50 PM

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 !!

Edited by JETerán, 22 August 2012 - 04:47 PM.

Success with courage.

José Eduardo Terán

Project Manager & Game Programming Professor. DSK International Campus

Indie Game Developer
Google + Account


Sponsor:

#2 JETeran   Members   -  Reputation: 265

Like
0Likes
Like

Posted 22 August 2012 - 04:34 PM

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 !!

Edited by JETerán, 22 August 2012 - 04:47 PM.

Success with courage.

José Eduardo Terán

Project Manager & Game Programming Professor. DSK International Campus

Indie Game Developer
Google + Account


#3 JETeran   Members   -  Reputation: 265

Like
0Likes
Like

Posted 22 August 2012 - 04:45 PM

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 !!

Success with courage.

José Eduardo Terán

Project Manager & Game Programming Professor. DSK International Campus

Indie Game Developer
Google + Account


#4 JETeran   Members   -  Reputation: 265

Like
0Likes
Like

Posted 22 August 2012 - 05:26 PM

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.

Edited by JETerán, 22 August 2012 - 05:35 PM.

Success with courage.

José Eduardo Terán

Project Manager & Game Programming Professor. DSK International Campus

Indie Game Developer
Google + Account


#5 JETeran   Members   -  Reputation: 265

Like
0Likes
Like

Posted 24 August 2012 - 12:06 PM

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.

Success with courage.

José Eduardo Terán

Project Manager & Game Programming Professor. DSK International Campus

Indie Game Developer
Google + Account


#6 prototypical   Members   -  Reputation: 301

Like
1Likes
Like

Posted 24 August 2012 - 02:43 PM

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.

Edited by prototypical, 24 August 2012 - 02:44 PM.


#7 JETeran   Members   -  Reputation: 265

Like
0Likes
Like

Posted 24 August 2012 - 05:20 PM

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[i] 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 !!

Success with courage.

José Eduardo Terán

Project Manager & Game Programming Professor. DSK International Campus

Indie Game Developer
Google + Account


#8 prototypical   Members   -  Reputation: 301

Like
1Likes
Like

Posted 24 August 2012 - 08:45 PM

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.

#9 JETeran   Members   -  Reputation: 265

Like
0Likes
Like

Posted 25 August 2012 - 03:07 PM

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 !!

Success with courage.

José Eduardo Terán

Project Manager & Game Programming Professor. DSK International Campus

Indie Game Developer
Google + Account


#10 prototypical   Members   -  Reputation: 301

Like
1Likes
Like

Posted 28 August 2012 - 10:47 PM

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

#11 JETeran   Members   -  Reputation: 265

Like
0Likes
Like

Posted 29 August 2012 - 12:39 PM

Thanks prototypical for your response.

Sorry yes, I was new to objects and didn't explain myself clearly.

What I did was create many objects (TeddyBear for example) and that objects has it's properties: TeddyBear.id = 0; TeddyBear._image = (instance of the image) and so on.

Then, in the Store.as file. I'm catching the objects I will use in the current mission (say mission 1) and put them in an array called storeMission1.

So far so good. I was able to add the TeddyBear and even I can show the image in scene.

But now I have a small problem.

I have another array called currentInventory where I want to catch the objects I click from storeMission1.

I'm trying: storeMission1[0].addEventListener ( ... ) but I get an error: says that addEventListener is not a function.

What could be wrong?

I tried changing store to a vector instead of a array, but didn't help.

Any idea?

With that, I can rest TeddyBear._cost and add the image to the inventoryArray and do the rest of the store code. This is the only part that I'm trapped !!

Any suggestion will help !!

Thanks prototypical !!

Success with courage.

José Eduardo Terán

Project Manager & Game Programming Professor. DSK International Campus

Indie Game Developer
Google + Account


#12 Deft   Members   -  Reputation: 218

Like
1Likes
Like

Posted 29 August 2012 - 01:43 PM

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 !!


Hello!
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.

Edited by Deft, 29 August 2012 - 01:46 PM.


#13 JETeran   Members   -  Reputation: 265

Like
0Likes
Like

Posted 29 August 2012 - 01:56 PM

WOW thanks Deft for your reply, was very informative.

The thing is that most of what I written is done. I'm only stuck in the last reply I did in this post.

I really wish you have any suggestion, is breaking me appart !!

Thanks so much !!

Success with courage.

José Eduardo Terán

Project Manager & Game Programming Professor. DSK International Campus

Indie Game Developer
Google + Account


#14 Deft   Members   -  Reputation: 218

Like
1Likes
Like

Posted 29 August 2012 - 03:43 PM

Ok, I'm in :)

I'm trying: storeMission1[0].addEventListener ( ... ) but I get an error: says that addEventListener is not a function.
What could be wrong?

Because of arrays does not have an event listener implementation. You should understand, that not all objects has event system.
Arrays is performance-dependent classes (memory & speed), so, they shouldn't have event listeners. Moreover, arrays are more system resources. If you want have event listeners on your inventory or item, use custom class with event listeners implementation (for this you need to read manuals how to do such thing, if you don't know :)).
I'm interested in what type of event listener you want to add? With custom event listener you can add specific events (for example, onItemUse or onItemExchange and etc.).

And if storeMission1 is game level, do not use it as array, use more OOP techniques, for example, just define getter method for getting some object from current level.

I hope, I understood your problem.

#15 JETeran   Members   -  Reputation: 265

Like
0Likes
Like

Posted 29 August 2012 - 03:50 PM

Hey Deft, thanks SO much for your reply !!

Ok I'm still working on this, but can't get farther.

I totaly undestood your point about arrays, thanks once again.

I'm interested in what type of event listener you want to add? With custom event listener you can add specific events (for example, onItemUse or onItemExchange and etc.).

I want to add MouseEvent.CLICK so when the user clicks the object, it adds to the inventory array, in the left of the screen. Any other way to do this?

storeMission1 is the array that I want to put all the objects I want to display only in mission1. The game level is called from another file called Missions where I call the function Mission1 and appears the main character and the levels.

ANY suggestion will be very welcome !! Truth is that I have been working on this game for the past days and can't see anything clearly :( But I will do my best to understand !

Success with courage.

José Eduardo Terán

Project Manager & Game Programming Professor. DSK International Campus

Indie Game Developer
Google + Account


#16 Deft   Members   -  Reputation: 218

Like
1Likes
Like

Posted 29 August 2012 - 04:14 PM

Any other way to do this?

Yeap, you can do this by implementing your own class for inventory item.
You need to extend your item from internal ActionScript Sprite class (that classes has event listeners by default). By extending it you gain opportunity to draw an object and use it events. And moreover you can implement your own data and methods for storing data about inventory item.
For example:
[source lang="delphi"]class InventoryItem extends Sprite{ private: var _itemId: int; InventoryItem(itemId: int) { _itemId = itemId; addEventListener(MouseEvent.CLICK, onItemClick); } function onItemClick(...) { // Your own code }}[/source]

Just like this Posted Image

Edited by Deft, 29 August 2012 - 04:15 PM.


#17 JETeran   Members   -  Reputation: 265

Like
0Likes
Like

Posted 30 August 2012 - 04:57 PM

Awesome Deft, thanks so much for your message.

Ok, I already have a file called GameObjects.as where I have all the objects that will be used in the game.

Following your suggestion, what I'm going to do now is create a StoreCreation.as file that extends GameObjects instead of Sprite (right??) So I can use the game objects that I need to put in the store in different missions. F.i. inside StoreCreation will have StoreMission1() function that will have object 1 and 2, StoreMission2() that will have object 1, 2 and 3 and so on.

What do you say? I will start doing it right now.

Thanks so much Deft for your time !!

Edited by JETerán, 30 August 2012 - 04:58 PM.

Success with courage.

José Eduardo Terán

Project Manager & Game Programming Professor. DSK International Campus

Indie Game Developer
Google + Account


#18 Deft   Members   -  Reputation: 218

Like
0Likes
Like

Posted 30 August 2012 - 05:19 PM

Following your suggestion, what I'm going to do now is create a StoreCreation.as file that extends GameObjects instead of Sprite (right??)

If your GameObjects class extends from Sprite - yes, StoreCreation will automatically be extended hierarchically from Sprite. But do not miss that thing that you should clean up your game object storage when objects does not needed anymore - that's performance important tip.

I think you on the right way :)

#19 JETeran   Members   -  Reputation: 265

Like
0Likes
Like

Posted 30 August 2012 - 05:32 PM

Hey Deft, thanks so much for your reply.

Yes indeed, GameObjects extends from Sprite and now StoreCreation extends from GameObjects.

So far so good.

I already created the StoreCreation and I can now eventListenerClick for the first object !! so far ;)

I think, as you say, I'm in the right track.

Ok now, I'm working on inserting the objects in the inventory.

To do this, I have mainInventory (right now an array) inside all the global vars of the game. Because the inventory will be global (and will keep the objects bought during all the game), I am working on adding the objects clicked in the StoreCreation to it.

Back in the StartGame.as, I'm looking the way to show those objects already inside mainInventory.

I'm on this right now, but ANY suggestion will be awesome !! Posted Image

EDIT 1:
Ok I tried to put 1 object to the inventory array but couldn't. this is all inside StoreCreation.as:

Main._instanceMain.addChild(TeddyBear._image); //successfuly appears in screen

TeddyBear._image.addEventListener(MouseEvent.CLICK, TeddyClick);

function TeddyClick(event:MouseEvent)
   {
	statistics.mainInventory.push(TeddyBear); //Ok i have mainInventory as global inside Statistics.as.  
  
	trace(statistics.mainInventory); // Each time I click that object in the store, I can see that each time, an object is added [TeddyBear] [TeddyBear]
	Main._instanceMain.addChild(statistics.mainInventory[0]._image); //This works great, but...
	(statistics.mainInventory[0]._image.x = 100; //When I try to put it in his location, it moves the image in the store, as if they were the same !!
   }
I'm missing something? This makes complete sense but don't know why is not adding to mainInventory and can't access that instance. It acts like if it were an instance of the instance :S

EDIT 2:
WOW it's been a long night.

Ok so far:
- I was able to move the hero between the rooms :D
- I have a GUI.as now, that stores things like game time, a nice menu when you press ESC. and a group of empty images of game objects. :D
- I create a tempo object inside GUI.as that works as an inventoryObject (taken from GameObjects.as) and I put it in the first empty image of game objects. :D
- I can detect when the hero is in front of the room2's in-game object (table) (for now, will work on the rest with same formula) and the inventoryObject is left over: hero AND table. :D

Now what's left:
URGENT: - Finish the inventory / store system so I can implement the inventory array in the GUI (Please help of EDIT 1 above)
- Make the others hitTests for the rest of the in-game objects. Easy.
- Make the hero look left when user hits left key. Tricky, I was some time doing this and couldn't figure out, tomorrow I will give it another try (later, actually)
- Make the hero's animation (I have the animation in the MC, I just need to play() it the hero moves)
URGENT: -Finish the game design, if player ends game, how to call again to call the MissionSelection function (I will work on what you told me Deft about it) :D

And that's pretty much what's next.

If I get this done fast, I could work on the rest 7 missions. Crap I will have only like 10 more hours before deadline.

If I get it done or note, I am in debt with you guys. Thanks so much for all your support in this desperate moments !!

After this, I will more close to this community, I want to help too. I learn A LOT.

See you in some hours, gonna take a quick nap :)





Thanks so much !!

Edited by JETerán, 31 August 2012 - 02:58 AM.

Success with courage.

José Eduardo Terán

Project Manager & Game Programming Professor. DSK International Campus

Indie Game Developer
Google + Account


#20 Deft   Members   -  Reputation: 218

Like
1Likes
Like

Posted 31 August 2012 - 11:49 AM

URGENT: - Finish the inventory / store system so I can implement the inventory array in the GUI (Please help of EDIT 1 above)
- Make the others hitTests for the rest of the in-game objects. Easy.
- Make the hero look left when user hits left key. Tricky, I was some time doing this and couldn't figure out, tomorrow I will give it another try (later, actually)
- Make the hero's animation (I have the animation in the MC, I just need to play() it the hero moves)
URGENT: -Finish the game design, if player ends game, how to call again to call the MissionSelection function (I will work on what you told me Deft about it)

I don't know, was your problem with array push released or not, but anyway: I'd said a few posts later about that thing that Flash engine transfer variables (non-scalar types, such as custom objects) as links, and not as values.
This is means that when you call addChild and use image1, you put some object on the scene. When you trying to add second object on the scene by using same image1 sprite, actually you just adding the same object that already on the game stage. The goal is create second instance of your sprite and that's all.

Turn left/right can be done by reversing source hero image by x axis Posted Image In ActionScript it can be done by replacing hero image each time key pressed.

Yeap, playing hero animation is very simple.

Edited by Deft, 31 August 2012 - 11:50 AM.





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