Pick 1 RANDOM Item... but ONLY with a specific variable?

0 favourites
  • 13 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hey All,

    Continue to my other thread I'm looking for a solution.

    In order to test this fast I made only 5 items for start for start (I'm not using Array).

    I have 5 items:

    - Every item is actually an animation within 1 Sprite.

    - Global variable: "Item_Number" (initial with 0 which is NO Item by default)

    - When the Player pickup an object called: "RANDOMIZER" (other sprite) it pick one of ALL the items, using a simple random action:

    int(random(1,5))

    So far it picks any of the 5 items like it should, works great!

    BUT! I want to limit the pick by skip some of the 5 items by adding another global variable such as:

    So I added another Global Variable: "Item_Found" 0 = Not Found 1 = Found

    THE QUESTION:

    What event / action I need to make that any Item with "Item_Found = 0" will ignored or skipped by the RANDOM action?

    Also, I'm not sure but maybe I should make the "Item_Found" instance for the item (sprite) instead of global variable?

    Thanks ahead for anyone who can help me solve this!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can make a While loop to repeat random if it chooses a wrong item, but this is not a great solution. If you have 20 items and 20 variables, this event will be huge!!!

    .

    One way to do this is to keep all these item sprites on the layout (you can move them outside of the visible area). Then use "System pick random instance" event to pick a random item. You can destroy items after they are found or add an instance variable "isFound" to this sprite and do something like this:

    Item isFound=0
    System pick random instance of Item 
     -> Item set isFound=1
     -> Item set position to (mouse.X, mouse.Y)
    

    Another good solution would be using an array or dictionary with all items and remove items that have been found from it. I know that you are trying to avoid using arrays and dictionaries, but I have no idea why :)

  • Thanks for sharing your idea dop2000 I appreciate it!

    My items are on different layout I spawn them when needed, but I have 1 sprite with all the animated items.

    I'm looking for a way to make an event that will use random action to pick ONLY from the items who have the specific variable value of "item_found" = 1

    I'm not sure how it can be done, but for example (in theory only) something like this:

    random("Item_Found=1") 

    I'm aware there is no such code and it's a really bad example but I have no idea how to explain what I'm trying to do with combination of Random + Specific Variable.

    The reason I don't use Array / Array Editor is because I tried it many times, it is super complicated for me to setup, ajax and stuff I don't even understand and not logic for my brain, also it looks hard to find or edit properties especially when not all of my items have the same properties.

    Individual editing my items is much faster, easy to find, and well organized also it's super easy for me to code each item and it's own properties.

    If there is some other solution that is close to what I'm trying to do with a Random action please share I'm very curious about it. Thanks ahead!

  • Just add all items to the layout (outside of visible area) and pick one random instance. This is the easiest solution. See my previous comment.

    .

    I told you many times, arrays and dictionaries are not that complicated. You just need to start using them. It's up to you of course, but if you choose to do everything with global variables, you'll spend ten times more time and will end up with messy and unmanageable code.

  • Just add all items to the layout (outside of visible area) and pick one random instance. This is the easiest solution. See my previous comment.

    What's the difference between the random pick I already use with instance in my sprite for each animation? it still needs to look for ALL the numbers and I can't tell it to choose from a specific variable as I explained... so it is not the easiest solution I guess.

  • Maybe I don't understand your issue, or you don't understand what I'm trying to say... Check out this demo (you should be able to open it in C3):

    dropbox.com/s/209see2urs7zd59/PickRandomItem.capx

  • Dear dop2000, I just had a chance to look at your file, it is VERY visual and very much to my taste but I'm not sure how to apply these simple rules to my game, I got started and I'm stuck so maybe you can help me out here:

    I have a dedicated Layout where I place all the Item instances called: "Item_Common" (I may change it to just "ITEMS" later, the reason is that it's easier to place everything in one layout, and I have it's very own dedicated event sheet that I put properties for every single item as well.

    Also, I think it's much cleaner not to copy past all the instances out of each layout I'm working on, but I'm not sure if I can pick them if they are not on the same layout. (oops?)

    Originally, I just spawned randomly from the "Items" Layout just 1 of the Items:

    1 sprite contains 5 animations, each animation = different item. (the random set the animation, simple)

    The Player spawned to the stage layout (every stage will be a different layout) so I'm not sure if the pick random instance is looking at the Layout? or it looks on it where it is in any layout... explanation how it works please?

    Anyway, I just played with it until I got stuck so what I did is making trying to replace the old "random" choose item with the pick random instance following your example, but in my example it's not changing position (since the Items are not there but on other Layout) so I need to spawn them.

    Is this possible at all? and how? here is my current not-finished code:

    Do you think it can still work consider I need to pick a random instance from a different Layout?

    I hope there is a solution, I really like this visual way you presented to me!

    Thanks ahead!

  • Sprite instances should be on this layout to be able to pick them. It's not possible to pick something from another layout.

    Just copy/paste 5 copies (instances) of Items_common sprite, set correct animations and move them off-screen.

  • Sprite instances should be on this layout to be able to pick them. It's not possible to pick something from another layout.

    Just copy/paste 5 copies (instances) of Items_common sprite, set correct animations and move them off-screen.

    I just tried it (like in your example file) but I need to set the position of the picked one on the origin name of my Player... is there a way to do it?

  • What does "origin name" mean? If you want to set item to player position, do this:

    Item set position to (Player.X, Player.Y)

    Make sure that items and player sprites are on the same layer, or on layers with the same parallax.

  • What does "origin name" mean? If you want to set item to player position, do this:

    Item set position to (Player.X, Player.Y)

    Make sure that items and player sprites are on the same layer, or on layers with the same parallax.

    I must use the the specific origin point of the Player's head, it is animated and holds the Items exactly as the player is animated (Idle, Walk, Attack, etc..)

    But I didn't see any other option to do it as I did on my code before, is there a way to do type it manually?

  • You can use "Set position to another object" action, it allows to select image point.

    Or you can set position to Head.ImagePointX("pointname"), Head.ImagePointY("pointname")

  • You can use "Set position to another object" action, it allows to select image point.

    Or you can set position to Head.ImagePointX("pointname"), Head.ImagePointY("pointname")

    Thanks for the fast reply, I'll give it a shot!

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