How do I Group Objects Together in a way that can be changed during runtime?

Not favoritedFavorited Favorited 0 favourites
  • 5 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • I am making a card game in which the players can make a deck with 8 cards, and when you press play the players will have four slots during "battle" for their cards. I want each slot to choose one random card from the eight that the player chose before battle and create an instance of it on the battle slot. The only problem is that this group of 8 changes based on the player's action. This makes families useless because they cannot be changed during runtime. Is there a way I can group cards together in a way one random one can be chosen for each slot, and so that the group can be changed by the player?

    I also thought about using instance variables as groups but I haven't found a solution that works yet.

    Tagged:

  • The instance variable idea sounds like it would work.

    If all the cards were just one object type and had different animation frames and variable values and all the cards were created. then just picking 8 from that and in turn four from that would be fairly easy with events.

    Some like having different cards be separate object types. In which case putting all those types in a family would be an option. But again to make it easy to pick you’d want all the instances created beforehand.

    Some like the idea of not having to have a card instance for every card in the deck, so instead you could instead create/destroy the cards on the fly. Simple to do but it will throw a slight inconvenience with picking events. Namely there has to be a so called delay after creating instances so they can be picked normally.

    The closest point is at the next “top level event” but I’m not sure anyone else has utilized that.

    The next closest would be at the end of the frame. Using a wait 0 action defers to the end of the frame.

    Others have tried the next frame or waiting some longer period of time.

    Anyways, I’ve found it cleaner to avoid that altogether and just create the instances beforehand or at the start of the layout. With the cards being a single object type you can just change one card into another by changing its properties instead of creating/destroying.

    Personally I’d be inclined to do the first idea. One object type for all cards. Create each card on the layout and customize each of their animation frames and variables. Some of the variables could be deck=0 and hand=0. You’d just set any to 1 that you’d want to hold. It’s easy to pick them by variable in events. You could just move the other instances off screen.

    The second iteration could be to do the same thing but instead of creating all the cards I’d just create all the ones in your deck. Then I’d have an array or json with all the specs or each possible card and then change the created instances from that.

  • I see how making one sprite with different animation frames could work. However I still do feel stuck.

    I'm not sure how I could get a card slot from the battle slot to choose which animation frame to be from the 8 selections. Is there a way to choose from the eight cards that are selected by the players, and change that specific sprite to a random one of those frames?

    This is probably on me, but I also don't see how the instance variables would work in that situation. Even if all of the cards in the deck had a "deck=1" instance variable, is there a way to change the sprite to a random animation frame from all of the cards with the "deck=1" instance variable?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well, say you have 52 card instances on the layout. They are all the same type and each has a different animation frame. It's probably easier to create all the instances with events, just make sure you have a "card" sprite with an animation with images for all 52 card images, and the animation speed is 0. Then the events to create all the cards would be:

    start of layout
    -- card: destroy
    
    start of layout
    repeat 52 times
    -- create card at (-1000, 0)
    -- card: set animation frame to loopindex

    We place them at -1000 to just get then off screen, but you could put them anywhere.

    Next we want to setup the player's deck of 8 cards. You likely have some way you want to have the player select them but we will just pick 8 random ones to use. Add an instance vaariable to card called "playerDeck" with a default value of 0, then add an event like this:

    start of layout
    for each card ordered by random(1) ascending
    compare: loopindex<8
    -- card: set playerDeck to 1

    Now we want to pick four random cards from the players deck to put in the player's hand. Add an instance variable to card called "playerHand" with a default value of 0, and add an event like this:

    start of layout
    card: playerDeck=1
    for each card ordered by random(1) ascending
    compare: loopindex<4
    -- card: set playerHand to 1

    Then picking a random card from the player's hand is just a matter of two conditions:

    card: playerHand=1
    system: pick random card instance

    To remove a card from the player's hand you'd just set playerHand back to 0.

    Anyways, that covers the picking of the cards. It's up to you how you'd move them around.

    It's one idea at least.

  • Thanks for the idea. I will check it out!

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