How to get the tag of the sector a monster is standing at ?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
doomplayer
Posts: 8
Joined: Thu Oct 10, 2019 10:53 pm

How to get the tag of the sector a monster is standing at ?

Post by doomplayer »

I read in an old post that i could use in zscript:

Code: Select all

sec = self.CurSector;
index = sec.index();
to get the index number of the sector where the monster is currently standing. but i can't figure out how to get the sector's tag.
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Re: How to get the tag of the sector a monster is standing a

Post by Apeirogon »

IIRC

Code: Select all

SectorTagIterator sec_tags = level.CreateSectorTagIterator(tag number);
int sector_id;

while(sector_id = sec_tags.next() )
{
sector_id is a number of a sector in level.sectors struct
}
User avatar
Player701
 
 
Posts: 1640
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: How to get the tag of the sector a monster is standing a

Post by Player701 »

SectorTagIterator is used to find sectors by their tag, which is not what the OP wanted to do. At the moment, it doesn't look like there is a way to get a sector's tag in ZScript. This is because tags are not stored within sectors but rather in a separate data structure (FTagManager).
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Re: How to get the tag of the sector a monster is standing a

Post by Apeirogon »

Extremely inefficient from the point of performance, but should work

Code: Select all

int get_sector_tag(sector s)
{
    int sector_tag = -1;

    for(int i = 0; i < 65536/*or what maximum tags index are?*/; i++)
    {
        SectorTagIterator sec_tags = level.CreateSectorTagIterator(i);

        int sector_id = -1;
        while(sector_id = sec_tags.next() )
        {
            if(sector_id == s.index())
            {
                sector_tag = i;
                break;
            }
        }
        if(sector_tag >= 0)
            break;
    }
    return sector_tag;
}
Last edited by Apeirogon on Thu Sep 24, 2020 12:06 pm, edited 1 time in total.
User avatar
Player701
 
 
Posts: 1640
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: How to get the tag of the sector a monster is standing a

Post by Player701 »

Looks like it's a signed 16-bit integer, so it goes from -32768 to 32767. Not sure if editors allow negative tags, though. Most levels don't have a lot of tags, and they rarely go that high anyway, so counting up from 0 wouldn't be extremely inefficient. Still, this is more of a workaround than anything, the best way would be to suggest a feature to add a method that can retrieve the tag directly.
doomplayer
Posts: 8
Joined: Thu Oct 10, 2019 10:53 pm

Re: How to get the tag of the sector a monster is standing a

Post by doomplayer »

Apeirogon wrote:Extremely inefficient from the point of performance, but should work

Code: Select all

int get_sector_tag(sector s)
{
    int sector_tag = -1;

    for(int i = 0; i < 65536/*or what maximum tags index are?*/; i++)
    {
        SectorTagIterator sec_tags = level.CreateSectorTagIterator(i);

        int sector_id = -1;
        while(sector_id = sec_tags.next() )
        {
            if(sector_id == s.index())
            {
                sector_tag = i;
                break;
            }
        }
        if(sector_tag >= 0)
            break;
    }
    return sector_tag;
}

I tried this but the game is freezing and crashing for some reason. i don't know if i am using it correctly:

Code: Select all

version 4.4.2

Class newzombie : zombieman replaces zombieman {
		
		sector sec;
		int tag;
		
		int get_sector_tag(sector s){
			int sector_tag = -1;
			
			for(int i = 0; i < 100/*or what maximum tags index are?*/; i++)
			{
				SectorTagIterator sec_tags = level.CreateSectorTagIterator(i);
				
				int sector_id = -1;
				while(sector_id = sec_tags.next() )
				{
					if(sector_id == s.index())
					{
						sector_tag = i;
						break;
					}
				}
				if(sector_tag >= 0)
				break;
			}
			return sector_tag;
		}
		
		states {
				Pain:
				PLAY A 1 NODELAY{
					
					sec = self.CurSector;
					tag = get_sector_tag(sec);
					A_LOGint(tag);
					
				}
				
			Goto Super::Pain;
		}
	}

User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49071
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: How to get the tag of the sector a monster is standing a

Post by Graf Zahl »

There is no such function because a sector may have more than one tag!
You cannot possibly know which is the one you want if there's more than one.
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Re: How to get the tag of the sector a monster is standing a

Post by Apeirogon »

This should work.

Code: Select all

    int get_sector_tag(sector s)
    {
        int sector_tag = -15;
        for(int i = 1; i < 100/*or what maximum tags index are?*/; i++)
        {
            SectorTagIterator sec_tags = level.CreateSectorTagIterator(i);
            
            int sector_id = -15;
            while( sector_id = sec_tags.next() )
            {
                if(sector_id < 0)
                    break;
                if(sector_id == s.index())
                {
                    console.printf("sector found");
                    sector_tag = i;
                    break;
                }
                
            }
            if(sector_tag >= 0)
                break;
        }
        return sector_tag;
    }
doomplayer
Posts: 8
Joined: Thu Oct 10, 2019 10:53 pm

Re: How to get the tag of the sector a monster is standing a

Post by doomplayer »

^
this works well thanks so much
Post Reply

Return to “Scripting”