Multiple things happening in one if condition.

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!)
ATDynaX
Posts: 16
Joined: Mon Mar 04, 2024 11:42 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia (Modern GZDoom)

Multiple things happening in one if condition.

Post by ATDynaX »

This is the situation. I have a fuse in my inventory and need to insert it into a fuse box to trigger multiple things.
1. The texture needs to switch from No Fuse to inserted fuse.
2. The fuse needs to be deleted from my inventory.
3. A line portal must be activated.

Problems i have is that i can't set multiple actions under one if condition if i want an else condition. I have to use several if conditions.
Or is it possible to trigger several things with one if condition. For now it works with several ifs, but the last point, that is for setting the portal, is still an issue.
I want to use the switched texture as the trigger, but i don't know how to code that. Something like: IF texture on line 25 is "Fusein texture", line_setportal target xy.

Code: Select all

script "Fusein" (int tag)
{

	if (CheckInventory("Fuse"))
		SetLineTexture (23, SIDE_FRONT, TEXTURE_BOTTOM,"MITFUSE");
	if (CheckInventory("Fuse"))
		TakeInventory("Fuse",1);
		Line_SetPortalTarget(24,25);
}
User avatar
MartinHowe
Posts: 2044
Joined: Mon Aug 11, 2003 1:50 pm
Location: Waveney, United Kingdom

Re: Multiple things happening in one if condition.

Post by MartinHowe »

Braces can be used to group multiple statements into one statement:

Code: Select all

script "Fusein" (int tag)
{
    if (CheckInventory("Fuse"))
    {
        SetLineTexture (23, SIDE_FRONT, TEXTURE_BOTTOM,"MITFUSE");
        TakeInventory("Fuse",1);
        Line_SetPortalTarget(24,25);
    }
    else
    {
        // Do 'else' things here
    }
}
As for the texture, most people keep a bool variable for that in this manner:

Code: Select all

bool isONTextureShowingOnLine = true;

script "Fusein" (int tag)
{
    if (CheckInventory("Fuse"))
    {
	 isONTextureShowingOnLine = false;
         ...

ATDynaX
Posts: 16
Joined: Mon Mar 04, 2024 11:42 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia (Modern GZDoom)

Re: Multiple things happening in one if condition.

Post by ATDynaX »

MartinHowe wrote: Tue Jun 11, 2024 6:00 am Braces can be used to group multiple statements into one statement:

Code: Select all

script "Fusein" (int tag)
{
    if (CheckInventory("Fuse"))
    {
        SetLineTexture (23, SIDE_FRONT, TEXTURE_BOTTOM,"MITFUSE");
        TakeInventory("Fuse",1);
        Line_SetPortalTarget(24,25);
    }
    else
    {
        // Do 'else' things here
    }
}
As for the texture, most people keep a bool variable for that in this manner:

Code: Select all

bool isONTextureShowingOnLine = true;

script "Fusein" (int tag)
{
    if (CheckInventory("Fuse"))
    {
	 isONTextureShowingOnLine = false;
         ...

Thank you. It is so simple, but when you can't find it in the Wiki, you are screwed.
User avatar
ramon.dexter
Posts: 1562
Joined: Tue Oct 20, 2015 12:50 pm
Graphics Processor: nVidia with Vulkan support
Location: Kozolupy, Bohemia

Re: Multiple things happening in one if condition.

Post by ramon.dexter »

Structuring code with braces is a basic knowledge... You're using braces for the whole script. Once you use braces in one place, you can use braces in other place. Like if/else, while loops, or any other place that needs specific structure.
User avatar
Caligari87
Admin
Posts: 6190
Joined: Thu Feb 26, 2004 3:02 pm
Preferred Pronouns: He/Him

Re: Multiple things happening in one if condition.

Post by Caligari87 »

Please be considerate in your replies. Everyone needs to start somewhere learning correct and efficient syntax; it's not exactly common knowledge when you're just starting out.

8-)
ATDynaX
Posts: 16
Joined: Mon Mar 04, 2024 11:42 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia (Modern GZDoom)

Re: Multiple things happening in one if condition.

Post by ATDynaX »

ramon.dexter wrote: Tue Jun 11, 2024 11:13 pm Structuring code with braces is a basic knowledge... You're using braces for the whole script. Once you use braces in one place, you can use braces in other place. Like if/else, while loops, or any other place that needs specific structure.
I actually have no idea when i should use braces and when not. Especially when the code works without braces, which is the case with if conditions.
User avatar
ramon.dexter
Posts: 1562
Joined: Tue Oct 20, 2015 12:50 pm
Graphics Processor: nVidia with Vulkan support
Location: Kozolupy, Bohemia

Re: Multiple things happening in one if condition.

Post by ramon.dexter »

So, braces are used to structure blocks of code. Blocks of code means multiple commands that are structured into blocks of lines with several commands. You can read more here:
https://www.quora.com/What-do-these-sym ... -otherwise

Basically, if/else statements work without braces for a single line of code. Multiple lines has to be put into braces, because interpreter reads the code by lines. Command directly followed by if(condition) function is considered part of the if command. Next line is another command, not belonging to the if statement. So if you need to use more than one function in the if() statement, you have to put it into braces. Same for any other function.

Read the following beginners guide, it shows how to use braces, especially the part "flow control".
https://zdoom.org/wiki/A_quick_beginner ... ide_to_ACS

Return to “Scripting”