How do I attach code to a template/hierachy/family aka a prefab?

Not favoritedFavorited Favorited 0 favourites
  • 9 posts
From the Asset Store
A collection of various zombie characters sprites for creating a 2D platformer or sidescroller game
  • A couple of years ago, I started working on a classroom tool (senior-high/college) that simulates the electrical grid of a city/region. Link at the bottom.

    I sort of gave up when I failed to create prefabs (custom objects) the way they exist in Unity/Gdevelop. I started recreating the project in Gdevelop a couple of months ago, whith mixed results.

    I want to give Construct another chance to become my go-to tool for this project again.

    So: Is there a way to create complex objects and attach functions to them? An example would be to find out if a certain part of the "prefab" has been clicked on. Another example would be to send numbers or strings to be represented inside of it.

    I have the hierachy/families/object properties figured out but am missing a way to run functions.

    EiLiF - Energy is Life, Energy is Freedom

  • Probably the closest C3 feature is "custom actions" which are like functions that are attached to an object type. It's just as easy to add as an actual function. Right click on the event sheet and select "add function" or "add custom action".

    Besides that, you could probably implement the logic in some different ways that work better with C3 events. For example, You could skip functions entirely, or only use them to not have duplicate stuff.

  • When I need some capability that’s available in standard programming languages, I implement it using JavaScript and synchronize it via global variables.

    But I think there are a few other ways—for example, putting an array into a prefab’s local variable, or having a shared array with a column for each prefab and having a dedicated automation that processes actions for each prefab and its data from the array.

  • Probably the closest C3 feature is "custom actions" which are like functions that are attached to an object type. It's just as easy to add as an actual function. Right click on the event sheet and select "add function" or "add custom action".

    I'll give "custom actions" a try.

    Besides that, you could probably implement the logic in some different ways that work better with C3 events. For example, You could skip functions entirely, or only use them to not have duplicate stuff.

    Already done that and it got messy. Maybe I am just too picky :)

    Much appreciated.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • When I need some capability that’s available in standard programming languages, I implement it using JavaScript and synchronize it via global variables.

    Yeah, I am trying to avoid that.

    But I think there are a few other ways—for example, putting an array into a prefab’s local variable, or having a shared array with a column for each prefab and having a dedicated automation that processes actions for each prefab and its data from the array.

    Again, too much for my 62 year old brain :)

    Thanks for replying though.

  • A follow-up with a cry for help :)

    Custom actions improve the structure and readabilty of the code. I am hung up on picking the correct objects when interacting with them. I tried picking by UIDs, by child. Nothing works. Only ever the first "prefab" placed on the screen will interact properly. A link to the project is here.

  • Add wid_switch to the container WID_SUPPLY is part of. You probably should add all the related stuff to the container too.

    Then just remove the pick by uid stuff. That will solve your current picking issues. Without that fix, event 3 in prefabs wouldn't know which supply the switch relates to, so the first one is picked.

  • It works. Thank you so much.

    Back in '85 I started in Assembly and Basic. More recently C and JS. The concept of containers, templates, hierarchies and families is foreign to me. Families and Hierarchies are easy to understand. I always get hung up on templates and containers.

  • All containers do is when you pick/create/destroy one object in a container the other objects in it get picked/created/destroyed.

    Hierarchies connect objects together so you can move/scale/rotate objects together as one. Lets you destroy the whole lineage if you destroy the parent too. It doesn’t provide any automatic picking of related objects.

    Templates let you recreate an object or hierarchy of objects later with the same setup. They do nothing with picking.

    Families are mostly to reduce the amount of events. If you find you’re doing the same events for multiple types, you could instead add all those types to a family and just do the events once with the family.

    The picking system is the main thing that makes the event sheet differ from other coding languages. But you only really have to deal with it if there are multiple instances of something.

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