Thread: How to access an object in memory when select one in a dropdown list?

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    8

    How to access an object in memory when select one in a dropdown list?

    It's windows forms. I create a few objects of the same class. Let's say I need to test the interaction between two game characters, I have two comboboxes (dropdown lists) where I want existing objects to be listed, then I want to select an object in each of the comboboxes and click the button "punch" which means object_1 punches object_2. So the application should do something with object_2 properties. How do I access these objects, select them, change their property values etc? I mean, in combobox it's gonna be just text Character_1, 2, 3 etc for objects, it's not actual reference to objects.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,423
    Somewhere you have an array / vector / list of all the game characters.

    So your UI first traverses this list to populate your combobox dialog.
    Code:
    [x] player 1  [ ] player 1
    [ ] player 2  [ ] player 2
    [ ] player 3  [x] player 3
    [ ] player 4  [ ] player 4
    PUNCH!
    Your punch button callback handler inspects the check boxes to check for things like
    - only two players selected.
    - a player isn't trying to punch themselves.

    Then I imagine you would then be able to do something like
    Code:
    players[selection2].punch( players[selection1].getStrength );
    which means the selected player2 is punched with the strength of the selected player1.

    Then perhaps you do something like
    Code:
    if ( ! players[selection2].isConscious() ) {
      // It's a KO!
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2017
    Posts
    8
    I like your answer. So I initially fill the array with all pre-created objects. Then one of the characters is defeated. He should visually disappear from the scene, reset his properties to default and wait till next spawn. In this case, he just should disappear from the comboboxes, not from array, array always keep all the characters regardless of their status. I mean, looks like I have to have an array of pointers, and each object should have the status (active/inactive), and I go through the array, check each object status and remove inactive from combobox. Anyway, how do I link combobox item to the object it represents? Combobox item has text and value properties. I don't really know how to link a combobox item to an array item.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to bind a dynamic dropdown list in C#?
    By Kyle Flores in forum C# Programming
    Replies: 2
    Last Post: 12-26-2016, 03:32 AM
  2. Object to Object Access
    By Arpan Das in forum C++ Programming
    Replies: 8
    Last Post: 09-05-2011, 09:25 AM
  3. List to select from
    By jat421 in forum C# Programming
    Replies: 2
    Last Post: 08-25-2011, 08:22 AM
  4. link list build: memory access denied
    By sed_y in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2011, 07:17 PM
  5. dropdown list? / reg? problems
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 06:58 PM

Tags for this Thread