Working on a shop system with buy and equip..

0 favourites
  • 7 posts
From the Asset Store
A well commented template with list menus and shops to use in your top-down RPGs or metroidvania games!
  • hi there,

    for the game I'm making I need a shop system, so I did the ingame shop tutorial and got a basic structure.

    In my game, there is a sprite called 's_sword', this consists of multiple frames, each frame is another sword.

    The variables I have so far are:

    total_money; your total amount of gold.

    current_weapon: the weapon you currently have equipped.

    current_weapon=1, means s_sword frame 1.

    I got a system working for buying now..

    But this is where I get stuck. I have a another sprite called s_sword_icon, that gets placed in the shop.

    each frame of the s_sword_icon should be linked to the frames in s_sword. as example: s_sword_icon frame 1 = s_sword frame 1.

    when the player presses a certain keyboard key and is overlapping frame 4 of s_sword_icon, the current_weapon should be 4.

    when this happens the s_sword_icon should change into another sprite called s_sword_icon_bought, meaning you purchased the sword and now can choose to equip this one or another one.

    I hope it is somewhat clear what I want to achieve, and I'll post updates when I figure stuff out.

    And if anyone has a better and more efficient way of doing this, I would be glad to know

    I also read a lot about Arrays, but those seem look difficult.

  • If I understand correctly something like this should work:

    Create a new animation "bought" for s_sword_icon and use the same frame numbers for the bought items..

    on key pressed

    is overlapping - s_sword_icon

    • set current weapon to s_sword_icon.animationframe
    • s_sword_icon star animation bought - with current frame (or just set the framenumber to currentweapon ofcourse)
  • i think i understand you very well , you want to do this without arrays , here i suggest a solution

    a full shope system for you from placing sprites to picking the bought weapons for the character.

    1st - for the s_sword_icon sprite make 2 animation containing the sword famres in the first animation is just the sword icone , or locked , and the second have th e same frames but with the indication that the sword was bought and make a variable for the s_sword_icon name it sword_number make the default value <varies> construct 2 will now that there will be different value for each instance and second value for the price (sword_cost) and third value isbough=false and it's optional if you want a power for each weapon add another value name it sword_power(integer or float).

    2nd - in the shope make a sprite that will work as a slot i mean where the sword_icon should appear ( somthing like boxes , make them instances from the same sprite lets name it sword_slot make 2 values for each instance one name it to isbought(boolean) a second value is slot_number(integer) ather values are sword_cost and sword_power

    -- here to fill the not bought slots

    3rd -in the event-sheet (Trigger event + AND for each s_sword_slot AND s_sword_slot.isbough=false )

    * create new object ( s_sword_icon)

    * set s_sword_icon animation frame to (s_sword_slot.slot_number)

    * set s_sword_icon.sword_num to (s_sword_slot.slot_number)

    * set s_sword_icon.sword_cost to s_sword_slot.sword_cost

    * set s_sword_icon.sword_power to s_sword_slot.sword_power

    * set s_sword_icon.isbought=false

    * set s_sword_icon position to another object "s_sword_slot".

    -- here to fill the already bought slots:

    4th - ( Trigger event + for each s_sword_slot AND s_sword_slot.isbough=true )

    * create new object ( s_sword_icon)

    * set s_sword_icon.animation ("nimation2")

    * set s_sword_icon.animation frame to (s_sword_slot .slot_number)

    * set s_sword_icon.isbought=true

    * set s_sword_icon.sword_num to (slot_number)

    * set s_sword_icon.sword_cost to (array_swords.X[s_sword_slot.slot_number])

    * set s_sword_icon.sword_power to (array_swords.Y[s_sword_slot.slot_number])

    * set s_sword_icon position to another object "s_sword_slot".

    -- here the buying operation

    5th - (on double touched object s_sword_icon AND on double touched object s_sword_slot AND s_sword_icon.isbough=false and s_sword_ico.sword_cost <= total_money )

    -- here you can make the buying actions such as :

    * set s_sword_slot.isbought=true

    * set s_sword_icon.isbought=true

    * set s_sword_icon.animation ("nimation2")

    * set s_sword_icon.animation frame to (s_sword_icon.sword_number)

    * substract from total _money ( s_sword_icon.sword_cost)

    -- here you can select the sword you bought

    6th - (on touched object s_sword_icon AND s_sword_icon.isbough=true )

    -- here you can make sword selection actions such as :

    * set s_sword.animation frame to (s_sword_icon.sword_number)

    * set s_sword.power to (s_sword_slot.sword_power)

    if you need to understand somthing i'm here, i'm sorry i know it looks confusing i made the solution in my head i don't have the project opened so i couldn't explain more.

  • Hey thanks for the detailed reply.

    I tried to make what you told, but I couldnt implement this line: set s_sword_icon.sword_cost to (array_swords.X[s_sword_slot.slot_number])

    Right now when you buy a sword the sword icon changes from red to green, which is good.

    But I still need to figure out how to display other swords using just 1 sprite.

    Here is my capx: shop_test.capx

    Edit: I'm also trying out LittleStain suggestion atm

  • [quote:2igy93jr]Hey thanks for the detailed reply.

    I tried to make what you told, but I couldnt implement this line: set s_sword_icon.sword_cost to (array_swords.X[s_sword_slot.slot_number])

    Right now when you buy a sword the sword icon changes from red to green, which is good.

    But I still need to figure out how to display other swords using just 1 sprite.

    Here is my capx: shop_test.capx

    You can just add several frames (Images) to the bought and notbought animations. And as the player switch through the shop you just change the animation frame. From what i can see you don't have a functionality to do this yet. But what you can do is something like this:

    However in your case it wont work. Because your program is running wild and you keep making s_sword_icons.

    You need to add some condition to prevent that, this event will eventually kill your game and computer as it is running endlessly and keep making sprites:

    After running for a few secs:

    I would suggest that before you continue with your game, take a look at functions. Learning those and getting used to using them all the time, will be a huge benefit.

    I wrote this example some time ago for another post of how to use functions, which explain it very basically, maybe that can be helpful, otherwise ill check the manual:

    [quote:2igy93jr]Since I just made a very simple example of a function and how to call it in another post. Ill add it here as well, as functions are actually very easy to use, the moment you get them.

    1. Add function plugin

    You first have to add the function object to your project. You find it in the list where you also add mouse, keyboard etc.

    2. Create a function

    Once the plugin is added it will be available for your whole project. To create one you add it like you would any other event. And you have 2 options to choose from:

    "On function"

    "Compare parameter"

    For now you only need "On function" and this can be compared to adding an "On mouse click" event, but instead of "On mouse click" already being added for you, you can decide what it should be called, and when to trigger it, which is done by calling it.

    So after you select the "On function" you will be able to give it a name.

    "Name" is just a reference so C2 know which function you are calling whenever you do a function call. So you can make it whatever you like.

    In the above example I want the function to give me the license plate of a car. So giving it a name of "Get car license plate" describe what the function does so I can remember it. But in theory I could have called it "Moon rising" and still make it return the license plate of a car.

    3. What a function does

    Now that you have added a function it will be empty and not very useful. To make it useful you want it to do something for you. And you can see a function like a machine you put something into it, and it does something and then it might throw something out.

    In the above example I throw in a car, and I want the function to give me the license plate of that particular car.

    Input = Car

    Output = license plate

    4. Calling a function

    A call to a function is an action, so like you would set the text of an text object, you will make the function call as an action.

    When you press "Add action" you can again select function, but this time you will have two other options "Call function" and "Set return value". Since we need to call a function you just select "Call function"

    And again It ask you for a name. Now this name need to be the name of the function you want to call. So in this case "Get car license plate". This will make C2 aware that this is the function it should look for.

    5. Parameters

    In this window you can also add parameters. Which would be the input, so in our case we want to throw in a car, since we want the license plate of a car. We need to tell the function which car we want the license plate from.

    So if you press "Add parameter" there will be added a field called "Parameter 0" These names are locked, so you can't change them. But you need to be aware of the order in which you add them. But for now since we only need 1 it doesn't matter.

    Since we want a specific license plate of a car, we need something that is unique for a car. So the UID of the car objects works very well, as its unique for any object. So we throw that into the parameter 0.

    6. Make the function do something

    So now the function call is ready and will do what it is suppose to, however since our function is empty it doesn't do anything.

    So the first thing we have to do, since a function isn't that clever even though we have already thrown it a "Car object" it still doesn't know what that parameter is for. So to make sure it knows, we add:

    "Car pick instance with UID function.param(0)"

    Since we stored the UID in parameter 0, we can also use it to select a car which match this UID. Now the function know what car we are talking about.

    7. Return value

    The same way as we added the function call, we now add an action of "Set return value", when you add it you get the possibility to set a Value. This is what will be returned when the function is done, doing what I does. In our case we want it to simply return the license plate of a car.

    Since we have already told it what car we want the license plate of. We can simply set the return value to "Car.license_plate"

    8. Using the return value

    Now that it have returned the license plate we want to use it for something. So what we can do is to check the return value and then act on it.

    In this case we want to check if the returned license plate is "12345678" and if that's the case then do something.

    That is the basic of using functions and in this example we returned a license plate, however its perfectly fine to not have a function return anything.

    When C2 reads your code and it gets to a function call, it actually jumps into that function, and will not continue reading your code, until it gets back from the function. So you could actually add everything that are in the function instead of the function call, and it would be exactly the same.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you, that was really helpful. definitely going to try functions out.

    Well I got the system working now.

    You can buy weapons by pressing UP when you overlap the matching weapon icon. this unlocks the weapon.

    When you bought/unlock it, the icon changes to a equip button and lets you change your weapon to the matching icon.

    When you don't have enough money the amount of gold text will flash to show that.

    when you leave and re-enter the room, you still will have the weapons unlocked that you had before you left the room. This was done by giving the sword icon object a persist behavior.

    So that's basically everything I wanted.

    Here is a screenshot of the code:

    As you can see I barely used any variables which make the evensheet really big and messy.

    The cost of weapons also arent in a variable, because the costs are based on animations, not individual objects.

    The only variables used are: total_money, current_weapon.

    And here is the capx if anyone wants to copy everything: capx.

    If you have suggestions on how to simplify and make the events more efficient, please tell.

    thanks!

  • i will try to optimise it for you guys and implement if needed

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)