Inventory

You must understand the Game Editor concepts, before post here.

Re: Inventory

Postby skydereign » Sat May 02, 2009 1:04 am

This may seem long, and a bit confusing, but I will explain the idea behind this demo. It is the change animation one. Using the actual actor would just cause confusion within gameEditor, requiring extra code to sort things out. Using the actual actor would still require methods to search through the given inventory slots, meaning it would use this code plus extra, to sort zdepth. I would need to design a new method to get around most of this, but this demo should do exactly what you want it to do, with less confusion.
I think the reason you think using the actor would be less complicated would be the mouse button down differentiation. If that is the case, I explain what to do thoroughly in the following.

Global Code->global
Code: Select all
#define MAX 24

int itemtype[MAX];
int row;

int ActionNum;

Okay, I commented these within the demo but I'll expound on their functions. These are the only global variables. The #define now sets the word MAX to equal 24. This demo has a limited inventory of 24. To increase the amount of items it can hold, you should increase the 24 by multiples of 8.

itemtype is an int array. If you don't know what an array is, it is essentially [x] number of ints, in this case MAX number (24). Each int is an inventory spot, the number represents what item. 0=empty, 1=Item1, 2=Item2, and so on
row is an int that determines which set of 8 you are displaying. While row equals 0, the inventory display actors show 0-7, while row equal 1, they display 8-15, and so on.

ActionNum is what you are going to do. While it equals 0, you cannot do anything, If it equals 1, you can put things into your inventory. If it equals 2, you can use items in the inventory, in this demo using items destroys them.

Global Code->ofst
Code: Select all
int
ofst (int r, int c)
{
    return (r*8+c);
}

Okay, this function is used to retrieve certain spots in the itemtype[]. When given row and column, it will give back a single int that corresponds to whichever inventory spot is in row r, column c. This is used frequently, especially by the itemDisplay actors.

Item->MouseButtonDown(left)->Script Editor
Code: Select all
int i;
int item = 1;
switch(ActionNum)
{
    case 0:
    break;
 
    case 1:
    for(i=0; i<MAX; i++)
    {
        if(itemtype[i]==0)
        {
            if(i%8==0 && i!=0)
            {
                row++;
            }
            itemtype[i]=item;
            DestroyActor("Event Actor");
            break;
        }
    }
    ActionNum=0;
    break;
 
    case 2:
    break;
}

This is a big one to understand. i is initialized which will be used for a loop that searches where to place the item. The int item is also important to get right. You can copy/paste this into each items mbd event. For each one, item needs to equal a different value. This value is very important, as it is what goes into the itemtype, signifying to the itemDisplay actors which item to show.

Next is the switch(ActionNum), if you don't know how switches work, here is a simple switch.
Code: Select all
switch(variable)
{
    case 0: // when the switch is run if variable==0
                // code until break, then exit the switch
    break;
    case 1: // else if variable==1, run code until break the exit switch
    break;
}

You can create as many cases as you wish, you will need one for every item. The only case with code within it is case 1, which is if you have selected pickup. When you click the item, it looks for the first available itemtype[]. (first that is equal to zero) It searches through all, excecuting when that itemtype[] is zero, but it breaks, exiting the for loop. The next bit if(i%8==0 && i!=0) checks if the next spot is the eighth spot in the row. If so, go to the next row, so you can see the new item. Now, it sets the itemtype[], or inventory spot, to whatever the item is. Afterwards, it gets rid of the action type, setting it to nothing, 0.

itemDisplay->DrawActor->Script Editor
Code: Select all
int i;
switch(itemtype[ofst(row,cloneindex)])
{
    case 0:
    for(i=ofst(row,cloneindex); i<MAX; i++)
    {
        if(itemtype[i]>0)
        {
            itemtype[ofst(row,cloneindex)]=itemtype[i];
            itemtype[i]=0;
            break;
        }
    }
    break;
}
animpos=itemtype[ofst(row,cloneindex)];

i again is used for a loop, this loop is used to slide items so that all item spots are being used, or all are in front/lower spots.The switch statement determines what itemtype is in it. In the case of 0, nothing in it, the square checks if it can slide an item down, and the spot. It then changes its animpos to fit the item.

-Note that the previous demo changed the animation, instead of animpos=. This is more efficient and easier to understand. I kept the switch statement, because it is good to get in the habit of avoiding unnecessary ifs. In this case it doesn't really matter.
-Also, the animation for itemDisplay must correlate with the item numbers. Just add more animations to the bottom of the itemDisplay.png, then edit the animation within gE, and set the frames to the right amount.
-This way also gets rid of the little flash when you use an item.

itemDisplay->MouseButtonDown(Left)->Script Editor
Code: Select all
switch(ActionNum)
{
    case 0:
    break;
 
    case 1:
    break;
 
    case 2:
    switch(itemtype[ofst(row,cloneindex)])
    {
        case 1:
        itemtype[ofst(row,cloneindex)]=0;
        break;
        case 2:
        itemtype[ofst(row,cloneindex)]=0;
        break;
    }
    ActionNum=0;
    break;
}

This uses the same structure as the item's mbd event, except the code is in case 2, the use ActionNum. Inside is another case, determining what to do pending on what item is there. In both case 1 and 2, the item does not actually do anything, it just disappears. You would insert your item's use there.

The rest is pretty much the same concept, such as the text display, that code is within the mouse enter event of the itemDisplay actors. Change the item name description to what you wish. I believe that covers it, if not feel free to ask any questions.

If this really is too complex, I could work on an easier one that uses the moving of actors, but I would need a bit of time to format it in a way that it is less complex. I don't think you'll need it though, if you understand the concepts I used, I think you can understand the actual goings on of this demo.
Attachments
InventoryCommented.zip
(101.45 KiB) Downloaded 152 times
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Inventory

Postby MonteyPython » Sat May 02, 2009 2:27 am

this version works best :mrgreen: thnxs alot :mrgreen: :mrgreen: :mrgreen:
id giv u 1,000,000,000 points but i cant quite do that now :mrgreen:
MonteyPython
 
Posts: 24
Joined: Tue Apr 21, 2009 6:15 pm
Score: 0 Give a positive score

Re: Inventory

Postby MonteyPython » Mon May 04, 2009 8:10 am

Wait, i'm not quite sure i get all of this. Could i have the demo where the actual actor move?
MonteyPython
 
Posts: 24
Joined: Tue Apr 21, 2009 6:15 pm
Score: 0 Give a positive score

Re: Inventory

Postby skydereign » Mon May 04, 2009 6:10 pm

What part don't you get? If you want the actor moving, does that mean you want an easier version of it that uses the actor? Like I said before, I would have to think about how to do that without using this as a base. So if that is the case, it may take me awhile. I can always explain what you don't get. Anyway, telling me would help me understand what to avoid using on the other version if you are hellbent on moving the actor.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Inventory

Postby MonteyPython » Mon May 04, 2009 9:51 pm

i dont get where to put the funtion for the item :twisted:
MonteyPython
 
Posts: 24
Joined: Tue Apr 21, 2009 6:15 pm
Score: 0 Give a positive score

Re: Inventory

Postby skydereign » Mon May 04, 2009 9:59 pm

What do you mean function of the item? Do you mean item use? If so the function for use of the item when it is in the inventory is in the mouse button down event on the actor itemDisplay. It is the case 2 of the switch ActionNum. If ActionNum==2, then that means use the item.
itemDisplay->MouseButtonDown(Left)->Script Editor
Code: Select all
switch(ActionNum)
{
    case 0:
    break;

    case 1:
    break;

    case 2: // This signifies when use is selected and you click on the item spot in itemDisplay
    switch(itemtype[ofst(row,cloneindex)])
    {
        case 1:
        // This would be the use of the Red Mushroom, example would be change animation
        ChangeAnimation("mario", "marioBig", FORWARD);
        itemtype[ofst(row,cloneindex)]=0;
        break;
        case 2:
        // This would be use of Green Mushroom, add a life
        Lives++;
        itemtype[ofst(row,cloneindex)]=0;
        break;
    }
    ActionNum=0;
    break;
}

If you mean use an item not in the inventory, still out on the field, then you would put that in the mouse button down event of the actual actor. Again it would be for the case 2 of the switch ActionNum.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Inventory

Postby MonteyPython » Tue May 05, 2009 12:39 am

Well, what about for the other options? (look at, push, pull etc.
MonteyPython
 
Posts: 24
Joined: Tue Apr 21, 2009 6:15 pm
Score: 0 Give a positive score

Re: Inventory

Postby skydereign » Tue May 05, 2009 12:45 am

Well, you would assign those actions to a number. ActionNum(0=do nothing, 1 pickup, 2 use, 3 push, 4 pull) Then, depending on where that action can take place, put it into the switch statement. Push and pull can't be done within the inventory, so you would put them into the mouse button down event. Then within the case, you put the code.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Inventory

Postby MonteyPython » Tue May 05, 2009 2:05 am

Well, in my game there are a lot of different items they're not all just mushrooms and stuff, there are like 20 different items, so you can see why i would want to move the actor
MonteyPython
 
Posts: 24
Joined: Tue Apr 21, 2009 6:15 pm
Score: 0 Give a positive score

Re: Inventory

Postby skydereign » Tue May 05, 2009 2:15 am

Actually, the only reason I can see is that you don't understand what I am doing. You would make a case for each actor. If the problem is keeping track of the numbers, either create the actor/items with the numbers in their names, or put a commented out guide in Global Code. You would need to set up the code for each one anyways, this allows them all to be in the same place. And to reference, just comment each spot allowing you to see which is which. What you would do if I made the actor move, is have an event in each actors mouse button down, and maybe use switches to determine the ActionNum. This is the same thing but separated, making it harder to edit.
Like I have said before, if you really want me to set up one with moving the actor, then tell me. Be warned, it may take awhile since I do not have a lot of time. In a week or so I will be free again, so if you are willing to wait I can help you there.

If you were to edit the code of the items, it would be much easier if they were all centralized. I used to make a lot of events, but now use code that is much more efficient and easier to manipulate as all relevant code is placed in the same spot. The two switches, one for action, one for item type. The ActionNum switch is the outer one, and at most you would put your 15 item switches within each ActionNum case, but you won't need to do that.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Inventory

Postby MonteyPython » Tue May 05, 2009 3:37 am

I think it wuld be great if u made the actor moving version, i don't mind waiting, i have another game im working on :mrgreen:
MonteyPython
 
Posts: 24
Joined: Tue Apr 21, 2009 6:15 pm
Score: 0 Give a positive score

Re: Inventory

Postby MonteyPython » Sun May 17, 2009 2:37 am

Hey um... its been like 2 weeks, so i was wonder if uve had any progress :mrgreen:
MonteyPython
 
Posts: 24
Joined: Tue Apr 21, 2009 6:15 pm
Score: 0 Give a positive score

Re: Inventory

Postby skydereign » Sun May 17, 2009 4:13 am

I just finished with most of school, which was preventing me from working, so I can get started tonight. I'll see what I can do, now that I am free, it shouldn't be that long from now.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Inventory

Postby MonteyPython » Sun May 17, 2009 8:50 pm

Ok cool. Thnx again :mrgreen:
MonteyPython
 
Posts: 24
Joined: Tue Apr 21, 2009 6:15 pm
Score: 0 Give a positive score

Re: Inventory

Postby skydereign » Tue May 19, 2009 7:30 am

Yeah, sorry this took so long. This was actually a lot easier then I had first thought. Two reasons... First, I was testing with zdepth awhile back, and it was causing gameEditor to crash, at least when I changed the zdepth of two actors to the same. This is not the case, so it can be done quite simply. Another, is that I don't use actor variables. I declare all my own variables, so I was in the habit of not using the actor ones. I usually make my own structs and do it as such... So, with both those, I thought it was much harder than it really was. This example uses int arrays, actor int, and some global ints. The logic is very similar to my previous examples, so I did not comment it. If you need any explanations, just ask. Sorry about the variable names switch. I did not look at my previous example, but the names are pretty self explanatory...
I may have left out something, I don't think so, but maybe.
Attachments
moveInvent.zip
(8.77 KiB) Downloaded 147 times
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

PreviousNext

Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest

cron