Choosing between objects while Creating an Object by Name. Help!

  • Hi everyone,

    I was trying to "create an object by name" by "choosing" between names of different objects, and it worked perfectly... like this....

    Create an object by name ___ choose("Sp_B1","Sp_B2","Sp_B3","Sp_B4","Sp_B5","Sp_B6","Sp_B7")

    Now i want to twist it a little and put the "different objects names" part as an instance variable of an object(let say "X") and now it looks like this...

    Create an object by name ___ str("choose("&X.InstanceVariable&")") where, X.InstanceVariable is set to "Sp_B1","Sp_B2","Sp_B3","Sp_B4","Sp_B5","Sp_B6","Sp_B7".

    The only problem is that this one did not work. Any guesses why?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • the choose command doesn't see the contents of the variable as a list to choose from. it only sees the single text value.

    so you will probably have to do something like:

    set tempVariable to choose("Sp_B1","Sp_B2","Sp_B3","Sp_B4","Sp_B5","Sp_B6","Sp_B7")

    create object by name tempVariable

    then set the instance variable to tempVariable

  • AllanR Sorry but your method did not worked, i made a mistake before when i said it did worked.

    I tried to set the instance variable as "choose("Sp_B1","Sp_B2","Sp_B3","Sp_B4","Sp_B5","Sp_B6","Sp_B7")"

    but it did not worked. Also i tried using a global variable set as "choose("Sp_B1","Sp_B2","Sp_B3","Sp_B4","Sp_B5","Sp_B6","Sp_B7")", it did not worked either...

    Any other guess of what could make it work?

  • works for me...

    with a normal create object, the object is automatically picked after it is created.

    with the create by name, your eventsheet wont know in advance which object got created, so you would have to put them in a family and use pick last created. The instance variable would have to be on the family (obviously).

    https://www.rieperts.com/games/forum/createbyname.c3p

    EDIT: you can't set an instance variable before the object exists... (that may have been part of the problem).

  • I think i might have not been successful in explaining the situation correctly, Let me rephrase it again in a simpler way with an example,

    Lets say there are three objects:

    "Sp_Data", "Sp_B1" and "Sp_B2",

    where object "Sp_Data" have an instance variable named "BChoice" with value "choose("Sp_B1","Sp_B2")"

    Now i want to create object randomly between Sp_B1 & Sp_B2 so i so i create this event,

    1. Every X Second >>> Create Object by name(Choose("Sp_B1","Sp_B2"))______This works fine!

    But i want this event to take the value of name from the instance variable of object "Sp_Data" that is "Bchoice" so i create this event,

    2. Every X Second >>> Create Object by name(Sp_Data.BChoice)______This does not work!

    So question is what do i do to make event "2" work?

  • ok, I see what you mean... choose cannot be used with a single variable like that, so you will have to use tokenCount and tokenAt.

    set the Sp_Data.BChoice to the list of possible objects "Sp_B1,Sp_B2,Sp_B3..."

    then: create object(tokenat(SP_Data.BChoice,floor(random(tokencount(SP_Data.BChoice,","))),","))

    what that does is count how many choices there are:

    HowMany = tokencount(SP_Data.BChoice,",")

    then randomly chooses one of them:

    WhichOne = floor(random(HowMany))

    and then extracts the text of the one choosen:

    tokenat(SP_Data.BChoice,WhichOne,",")

    https://www.rieperts.com/games/forum/createbyname2.c3p

  • if the list of objects doesn't change dynamically at run time, then you can just put all the sprites in a family and use:

    Create object FamilyName

    that will have it randomly choose one of the family members to create...

  • Thanks AllanR i will try "tokencount" solution, i am sure it will work.

    And i do need list to change dynamically.

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