Jump to content

Get structure type


Hoze
 Share

Recommended Posts

Hello,

I'd like to make a condition that checks if a unit is a specific building.

From looking into source code and previous issues, I guess I'm trying to do somthing like this:

for (let unit of units){
	let house = Engine.QueryInterface(unit, IID_TemplateManager);
	if (house.type() != "template_structure_civic_house")
		continue;
}

But I have very limited understanding of the code structure. How should I re-write the above? And where can I learn about Engine.QueryInterface?

Link to comment
Share on other sites

You can have a look at https://docs.wildfiregames.com/entity-docs/trunk.html

for (let unit of units){
    // SYSTEM_ENTITY is a special entity that holds all the *manager classes
    // We query its component by its id IID_TemplateManager.
	let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
    // We ask the component to give us the template of a specific entity id.
	const unitTemplate = cmpTemplateManager.GetCurrentTemplateName(unit);
    // Note here that you are referencing a "root" template. There is very little chance it will match.
    // Instead you should use Identity classes.
	if (unitTemplate != "template_structure_civic_house")
		continue;
}

// Identity classes example.
for (let unit of units){
        // We query the identity component by its id IID_Identity for unit.
        const cmpIdentity = Engine.QueryInterface(unit, IID_Identity);
        // It might not have one.
        if (!cmpIdentity)
            continue;

        // A unit has a certain number of classes you can define in the template.
        const classes = cmpIdentity.GetClassesList();
        if (cmpIdentity.GetClassesList().indexOf("house") === -1)
            continue;
}

 

  • Thanks 1
Link to comment
Share on other sites

Thanks Stan for your detailed reply. And since I have the opportunity, thanks for your work on 0ad! :celt: So many compliments to say about the game!

You hinted me in the right direction. It doesn't work as is, and I can't figure out where I should look for details on the methods like:

engine.GetClassesList();

Where can I find the code where they are defined and how does one usually get the list of methods available? I may have missed something obvious in the documentation, if so, sorry, I'm clueless.

  • Like 1
Link to comment
Share on other sites

It's alright. There are two types of simulation components C++ ones (in source/simulation2/components) and JavaScript ones (in binaries/data/mods/public/simulation/components). Components ids, in this case IID_Identity usually match the file they represent, here Identity.js (JavaScript hence the second type)

Engine.QueryInterface returns an interface to an Identity object in that case, allowing you to call of its methods. One of them is GetClassesList. You can find the definition here.

https://code.wildfiregames.com/source/0ad/browse/ps/trunk/binaries/data/mods/public/simulation/components/Identity.js$140

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Thanks to your explanations, I had fun modding components!

Juste in case, for archives, I wanted to customize the Alert system to filter out or in specific buildings, I could modify the component: simulation/components/AlertRaiser.js

 let holder = cmpRangeManager.ExecuteQuery(unit, 0, +this.template.SearchRange, mutualAllies, IID_GarrisonHolder, true).find(ent => {
            

            const classes = Engine.QueryInterface(ent, IID_Identity).GetClassesList();
            
            //Filter only buildings from a class
            if (classes.indexOf("House") === -1)
                return false;
            
           [Other conditions...]

            let cmpGarrisonHolder = Engine.QueryInterface(ent, IID_GarrisonHolder);
            if (!reserved.has(ent))
                reserved.set(ent, cmpGarrisonHolder.GetCapacity() - cmpGarrisonHolder.OccupiedSlots());
            

            return cmpGarrisonHolder.IsAllowedToGarrison(unit) && reserved.get(ent) >= size;
        });
Edited by Hoze
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...