need help in Power ups using Stack code

Don't know how to make something and can't find a tutorial? Ask for one here.

need help in Power ups using Stack code

Postby Masky94 » Sat Apr 25, 2015 8:00 am

hello, i understand the code for stack, but i want to implement it in a game that can use power ups, let's say my actor grab a shield power ups, it store in the inventory with 3 slots. so i will also need an actor which is shield, when pressing a key the shield will shows ups and used, so how do i implement the function in this code below? where do i place the power ups, and the key and used the power ups, also the function that grab and store into the inventory when the actor got and power ups?
need some experts' suggestion, would appreciate your help , thank you.

Code: Select all
#define MAX 100

int stack[MAX];
int top = 0; // first empty place

// add an element
int push (int x) {
    if (top == MAX) return 0; // stack is full, operation failed
    stack[top++] = x;
    return 1; // operation succeeded
}

// take an element
int pop () {
    int x;
    if (top == 0) return -1; // stack is empty, operation failed (return -1, or any other negative number,
                                      // or 0, if stack contains natural numbers)
    x = stack[--top];
    return x;
}

// read the top, without removing the element
int read() {
    int x;
    if (top == 0) return -1; // stack is empty, operation failed
    x = stack[top];
    return x;
}

// is stack empty
int empty() {
    if (top == 0) return 1;
    else     return 0;
}

// is stack full
int full() {
    if (top == MAX) return 1;
    else     return 0;
}
Masky94
 
Posts: 10
Joined: Thu Apr 16, 2015 6:13 am
Score: 0 Give a positive score

Re: need help in Power ups using Stack code

Postby koala » Sat Apr 25, 2015 10:28 am

What exactly do you need stack for? For storing shield (item) into inventory, for powering-up player, or for something else?
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: need help in Power ups using Stack code

Postby Masky94 » Sat Apr 25, 2015 12:39 pm

trying to use the stack for like storing 3 power-ups, and using the last-in first-out theory, then when i press the key, the actor will use the last power-up that collected in game
Masky94
 
Posts: 10
Joined: Thu Apr 16, 2015 6:13 am
Score: 0 Give a positive score

Re: need help in Power ups using Stack code

Postby koala » Sat Apr 25, 2015 3:12 pm

Masky94 wrote:trying to use the stack for like storing 3 power-ups, and using the last-in first-out theory, then when i press the key, the actor will use the last power-up that collected in game
Well, you could make a stack of structures. For instance:
Code: Select all
#define MAX 3

typedef struct power_up {
    Actor *act; // points to collected item
    double power;
    // other structure fields
} powerUp;

powerUp stack[MAX];
int top = 0;
When you press the [space] key, you activate script that calls function pop(). In this case pop() will return powerUp.
Code: Select all
powerUp myPower = pop();
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: need help in Power ups using Stack code

Postby Masky94 » Sat Apr 25, 2015 5:20 pm

thanks, but still. i am not quite understand the line
Actor *act; // points to collected item <--- is this where i place the power-up actor here?
double power; <--- what does this line do?

powerUp myPower = pop(); <--- what does the "myPower" do?

so this few lines actually already have the function of a simple stack? i thought i need to code that when the power-ups reach 3 items, so it cant pick up another power-ups
Masky94
 
Posts: 10
Joined: Thu Apr 16, 2015 6:13 am
Score: 0 Give a positive score

Re: need help in Power ups using Stack code

Postby koala » Sat Apr 25, 2015 6:16 pm

If I understand you right, collected items go to inventory. Then, from inventory you take items by principle first-in-last-out (or last-in-first-out). So, inventory is a stack.

Masky94 wrote: Actor *act; // points to collected item <--- is this where i place the power-up actor here?
Yes.
Masky94 wrote:double power; <--- what does this line do?
This is where you store how much "power" collected item gives.
Masky94 wrote:powerUp myPower = pop(); <--- what does the "myPower" do?
powerUp is a type. It is a structure that contains pointer to actor (item), "power" that item gives, and if you have some other values you need to save. That type is powerUp. Type of myPower is powerUp. In myPower you store item from top of the stack.
Masky94 wrote:so this few lines actually already have the function of a simple stack? i thought i need to code that when the power-ups reach 3 items, so it cant pick up another power-ups
You need to write code for stack as in first post, just instead of int, functions will deal with powerUp type. Then you call those functions when you need them.
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: need help in Power ups using Stack code

Postby Masky94 » Sun Apr 26, 2015 10:32 am

so i need to write this code as the stack/inventory for the actor(playable character)?

Code: Select all
#define MAX 100

int stack[MAX];
int top = 0; // first empty place

// add an element
int push (int x) {
    if (top == MAX) return 0; // stack is full, operation failed
    stack[top++] = x;
    return 1; // operation succeeded
}

// take an element
int pop () {
    int x;
    if (top == 0) return -1; // stack is empty, operation failed (return -1, or any other negative number,
                                      // or 0, if stack contains natural numbers)
    x = stack[--top];
    return x;
}

// read the top, without removing the element
int read() {
    int x;
    if (top == 0) return -1; // stack is empty, operation failed
    x = stack[top];
    return x;
}

// is stack empty
int empty() {
    if (top == 0) return 1;
    else     return 0;
}

// is stack full
int full() {
    if (top == MAX) return 1;
    else     return 0;
}


then i add a function for collecting the power-ups separately with this one ?
Code: Select all
#define MAX 3

typedef struct power_up {
    Actor *act; // points to collected item
    double power;
    // other structure fields
} powerUp;

powerUp stack[MAX];
int top = 0;


and then the last thing is add an event for [space] key
Code: Select all
powerUp myPower = pop();


so are my steps going right? and thanks for your guidance till now
Masky94
 
Posts: 10
Joined: Thu Apr 16, 2015 6:13 am
Score: 0 Give a positive score

Re: need help in Power ups using Stack code

Postby koala » Sun Apr 26, 2015 4:01 pm

That is code for stack that stores integers. You will use that code, but instead of int type it will deal with previously defined type powerUp.

GlobalCode:
Code: Select all
#define MAX 3

typedef struct power_up {
    Actor *act; // points to collected item
    double power;
    // other structure fields
} powerUp;

powerUp stack[MAX];
int top = 0;
powerUp error = { NULL; } // returns when operation fails, fills only the first field (act)

// add an element
int push (powerUp x) {
    if (top == MAX) return 0; // stack is full, operation failed
    stack[top++] = x;
    return 1; // operation succeeded
}

// take an element
powerUp pop () {
    powerUp x;
    if (top == 0) return error;

    x = stack[--top];
    return x;
}

// read the top, without removing the element
powerUp read() {
    powerUp x;
    if (top == 0) return error; // stack is empty, operation failed
    x = stack[top];
    return x;
}

// is stack empty
int empty() {
    if (top == 0) return 1;
    else     return 0;
}

// is stack full
int full() {
    if (top == MAX) return 1;
    else     return 0;
}


Let's say player's character collides with an item. To add it to inventory, call function push(collected_item) in script editor. When you want to activate the last collected item press [space] and it will activate another script editor that calls function pop(). Of course, you will need to store somewhere item returned by that function, so you can use its power-up:
Code: Select all
myPower powerUp = pop();
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: need help in Power ups using Stack code

Postby koala » Mon Apr 27, 2015 12:34 am

Hey, Masky94! :D

Examine this: powerUp source (ged and data files)
You collect items, store them in inventory and use them. It uses stack.
However, for some reason this doesn't work when run as Windows executable. To be more precise, there are no items and no grass. I've made those as clones and I suppose that is the problem. Someone else, who knows GE better, should explain you (us) this. :D
But it is a good example of what you've asked.

Don't forget to use Script Reference. :!: :mrgreen:
Attachments
powerUp source.zip
ged and data files
(96.93 KiB) Downloaded 173 times
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: need help in Power ups using Stack code

Postby Masky94 » Mon Apr 27, 2015 2:46 pm

hey koala,
that is what i wanted to do, do you understand how it actually work?
i do really need to know how that work...
Masky94
 
Posts: 10
Joined: Thu Apr 16, 2015 6:13 am
Score: 0 Give a positive score

Re: need help in Power ups using Stack code

Postby koala » Mon Apr 27, 2015 3:53 pm

Masky94 wrote:hey koala,
that is what i wanted to do, do you understand how it actually work?
i do really need to know how that work...
I understand, I've made it. :lol: Maybe first you examine it a little bit, and ask me if something's unclear? :D
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: need help in Power ups using Stack code

Postby koala » Mon Apr 27, 2015 10:14 pm

koala wrote:However, for some reason this doesn't work when run as Windows executable. To be more precise, there are no items and no grass. I've made those as clones and I suppose that is the problem. Someone else, who knows GE better, should explain you (us) this. :D
I've solved this problem by adding one big Activation Region. :mrgreen:
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: need help in Power ups using Stack code

Postby Masky94 » Tue Apr 28, 2015 6:40 am

yes the part that i am unclear is the collecting item.

let's say i have a car, then collide with the power-ups (named shield_item), and where is the line of the code that store the power ups? let's say i wanted to know where do i place the actor named (shield_item) in the code?

also i need help when i press the key and activate the shield aka pop() function, then another actor named "shield" will surround the car and follow the car everywhere until it destroyed, can you tell me how to code that? also should i make it "dont appear at startup" on the shield? if i did that i cant parent it with the car.

another thing is, there is 3 boxes under the screen that let you see the power-ups, since i only have one, what if i dont want to make that boxes in my game, does the stack will still work?

or.....i can send you my game, i already have all the items in the game, just having a slight problem with the stack, could you help me check for it, and refine it? it would be much help for me if you can do that :D
Masky94
 
Posts: 10
Joined: Thu Apr 16, 2015 6:13 am
Score: 0 Give a positive score

Re: need help in Power ups using Stack code

Postby Masky94 » Tue Apr 28, 2015 10:52 am

hey i got it done! thanks for the help all the time! :D :D
Last edited by Masky94 on Wed Apr 29, 2015 7:17 am, edited 1 time in total.
Masky94
 
Posts: 10
Joined: Thu Apr 16, 2015 6:13 am
Score: 0 Give a positive score

Re: need help in Power ups using Stack code

Postby koala » Wed Apr 29, 2015 11:09 am

Masky94 wrote:hey i got it done! thanks for the help all the time! :D :D
I am glad to hear that. :D
Sorry I didn't reply, I was working on powerUp Tutorial and I wanted to finish that first and then answer you here, but you've done it. :mrgreen:
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score


Return to Tutorial Requests

Who is online

Users browsing this forum: No registered users and 1 guest