How do I pass objects/sprite names to a function parameter?

0 favourites
  • 9 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • I have some generators that I want to be chosen at random to spawn some objects.

    i.e. choose 2/5 of (generator) and spawn (object).

    There are a few 'pairs' of these, so I don't really want to rewrite the function several times (and it's bad 'coding').

    How can I use params to pass in object names for those two variables in C3?

    I suppose I use the string param type, but I can't quite work out how to access it for something like 'pick random instance', where you'd usually select a sprite, for e.g.

  • There is an expression Family.ObjectTypeName

    You can pass it to the function, and inside the function use "Create object (by name)" action.

  • Thanks, I'll try that out when I get chance.

    I tried a search in the manual to find that expression to see if there's somewhere to look before posting to forums next time, but I can't find it.

    How could I have known about this expression?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It's in the documentation under System Reference -> System expressions

  • I did try that, thinking that would be the obvious answer, but I couldn't find it.

    Did a search in the page for 'family' and can't find anything.

    Am I searching the right page?

    construct.net/en/make-games/manuals/construct-3/system-reference/system-expressions

  • Sorry, this is not a system expression, it's under "common expressions":

    construct.net/en/make-games/manuals/construct-3/plugin-reference/common-features/common-expressions

  • Wonderful, thanks for that! I have another page to search now!

  • To help others with this issue, this is what I ended up doing:

    I have two families - one has my 'generators' in. The other has my 'generated objects' in.

    For the 'Generators' family, there is a family-level boolean of 'used' (necessary to use so that you don't get multiple objects spawning on the same site).

    ----+ System: On start of layout

    -----> Functions: Call fruitGenerator (generator: "genBerry", fruit: "berry", number: 2)

    -----> Functions: Call fruitGenerator (generator: "genCarrot", fruit: "carrot", number: 4)

    ----* On function 'fruitGenerator'

    ----* Parameter 'generator' (String)

    ----* Parameter 'fruit' (String)

    ----* Parameter 'number' (Number)

    | Local number i‎ = 0

    --------+ System: While

    --------+ System: i < number

    ------------+ System: Pick a random generators instance

    ------------+ System: generators.ObjectTypeName = generator

    ------------+ generators: [X] Is used

    -------------> System: Create object fruit on layer 1 at (generators.X, generators.Y), create hierarchy: False

    -------------> System: Add 1 to i

    -------------> generators: Set used to True

    --------+ System: Pick all generators

    ---------> generators: Set used to False

    Thanks again for the help!

  • You need to move "Pick random generator instance" condition to be the last condition in the event:

    generators is Not used
    Pick generators with objectTypeName=...
    Pick random generators instance
    

    Otherwise, if "Pick random" is first, it may pick a generator with the wrong type, or used, and the event will not be executed.

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