Likelyhood of coming across issues with nested custom events

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
User avatar
LordAwsomeness
Posts: 203
Joined: August 12th, 2013, 2:20 pm
Location: U.S.A.

Likelyhood of coming across issues with nested custom events

Post by LordAwsomeness »

So i am a huge culprit of nested macros. Like macros inside of macros inside of macros inside of macros inside of macros, etc (this still isnt even close to how deep my nested macros go; its actually far worse than this...). My Addon that ive been working on for 3 years is very poorly optimized and im finally at the point where i want to change a majority of my nested macros to nested custom events instead. I am just curious if anybody has any insight as to whether this will help things, or just make things worse.

Examples of what nested macros i plan to replace with custom events:
  • Nested macros of [while] [do] for my shops and my attribute and weapon swapping menus and sub menus and their submenus down to like 5 or 6 levels of deepness.
  • nested macro events that are the same function (harm unit instances that i could just use a custom event to fire with variables setting the values needed to cause the damage)
- Been playing Wesnoth since 2004 and the 1.0.x versions.
- Creator of Undead Invasion MP Scenario Pack.
- Creator of Valeria MP Adventure
- Creator of LA_RPG ERA
User avatar
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Re: Likelyhood of coming across issues with nested custom events

Post by WhiteWolf »

In a nutshell, the general problem with macros is that regardless of how much we'd want them to act as functions, they are not. They reduce code only for the viewer, but as soon as it's executed, their content is just "copied and inserted", so to the computer it's the same as if there was no "optimization" at all.

Therefore, though I could be wrong, cutting out macros won't make it any worse. Replacing them with fired events is almost guaranteed to help the performance of your code.
Fired events are at their best when used to elevate certain methods that are repeated a lot, but do not need to handle dynamic variable information. That is harder with fired events, and macros are better at it because of their parameter list.

A bit of further reading on optimization and when exactly to use fired events, and when it's better to use a macro.

(PS: You probably know this and it's just me reading a bit of confusion into your post, but just to be sure :) : There's a difference between a nested event and a fired event.)
Main UMC campaigns: The Ravagers - now for 1.16, with new bugs!
Old UMC works: The Underness Series, consisting of 5 parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
User avatar
lhybrideur
Posts: 357
Joined: July 9th, 2019, 1:46 pm

Re: Likelyhood of coming across issues with nested custom events

Post by lhybrideur »

Firing events is far better in term of optimization.
Here are my shop events if you need it.

Code: Select all

[event]
        name=moveto
        first_time_only=no
        [filter]
            side=1
            x=4
            y=7
        [/filter]
        [store_gold]
            side=1
            variable=gold
        [/store_gold]
        {VARIABLE is_on_shop yes}
        [modify_unit]
            [filter]
                name=Gaelyn
            [/filter]
            id=Gaelyn
        [/modify_unit]
        [modify_unit]
            [filter]
                name=Denogyc
            [/filter]
            id=trader
        [/modify_unit]
        [set_variable]
            name=x1
            value=4
        [/set_variable]
        [set_variable]
            name=y1
            value=7
        [/set_variable]
        [message]
            speaker=trader
            message= _ "Welcome to the Black Hammer Forge. Orcs nearly stole us everything but, what's left, you won't find it anywhere for a better price. What can I do for you today?"
            [option]
                message="Buy weapons"
                [command]
                    [fire_event]
                        name=buy_weapons
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
            [option]
                message="Buy armours"
                [command]
                    [fire_event]
                        name=buy_armours_forge
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
            [option]
                message="Sell"
                [command]
                    [message]
                        speaker=trader
                        message= _ "To sell an object, drop it on the floor and select sell."
                    [/message]
                [/command]
            [/option]
            [option]
                message="Nothing"
                [command]
                [/command]
            [/option]
        [/message]
        {CLEAR_VARIABLE gold}
    [/event]

    [event]
        name=buy_weapons
        first_time_only=no
        [message]
            speaker=trader
            message= _ "What kind of weapon do you want to buy?"
            [option]
                message="Staffs"
                [command]
                    [fire_event]
                        name=buy_staffs_forge
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
            [option]
                message="Bows"
                [command]
                    [fire_event]
                        name=buy_bows
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
            [option]
                message="Swords"
                [command]
                    [fire_event]
                        name=buy_swords
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
            [option]
                message="Maces"
                [command]
                    [fire_event]
                        name=buy_maces
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
            [option]
                message="Axes"
                [command]
                    [fire_event]
                        name=buy_axes
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
            [option]
                message="Exit"
                [command]
                [/command]
            [/option]
        [/message]
    [/event]

    [event]
        name=buy_armours_forge
        first_time_only=no
        [message]
            speaker=trader
            message= _ "What kind of weapon do you want to buy?"
            [option]
                message="Gloves"
                [command]
                    [fire_event]
                        name=buy_gloves_forge
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
            [option]
                message="Boots"
                [command]
                    [fire_event]
                        name=buy_boots_forge
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
            [option]
                message="Helms"
                [command]
                    [fire_event]
                        name=buy_helms_forge
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
            [option]
                message="Armours"
                [command]
                    [fire_event]
                        name=buy_armours_forge_2
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
            [option]
                message="Shields"
                [command]
                    [fire_event]
                        name=buy_shields_forge
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
            [option]
                message="Exit"
                [command]
                [/command]
            [/option]
        [/message]
    [/event]

    [event]
        name=buy_staffs_forge
        first_time_only=no
        [message]
            speaker=trader
            message= _ "What kind of staff do you want to buy?"
            {SHOP_ITEM _"Curved staff" 75 80 items/staff.png i1}
            {SHOP_ITEM _"Young wood staff" 30 51 items/staff.png i3}
            {SHOP_ITEM _"Damaged staff" 21 88 items/staff.png i2}
            {SHOP_ITEM _"Staff" 16 39 items/staff.png i4}
            {SHOP_ITEM _"Twisted staff" 6 24 items/staff.png i5}
            [option]
                message="Back"
                [command]
                    [fire_event]
                        name=buy_weapons
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
        [/message]
    [/event]

    [event]
        name=buy_bows
        first_time_only=no
        [message]
            speaker=trader
            message= _ "What kind of bow do you want to buy?"
            {SHOP_ITEM _"Bow" 66 81 items/bow.png i6}
            {SHOP_ITEM _"Damaged bow" 57 82 items/bow.png i7}
            {SHOP_ITEM _"Fragile bow" 27 67 items/bow.png i9}
            {SHOP_ITEM _"Long old bow" 26 69 items/bow.png i10}
            {SHOP_ITEM _"Bad war bow" 24 86 items/bow.png i8}
            [option]
                message="Back"
                [command]
                    [fire_event]
                        name=buy_weapons
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
        [/message]
    [/event]

    [event]
        name=buy_swords
        first_time_only=no
        [message]
            speaker=trader
            message= _ "What kind of sword do you want to buy?"
            {SHOP_ITEM _"Long blunt sword" 57 83 items/sword.png i11}
            {SHOP_ITEM _"Blunt claymore" 39 84 items/sword.png i12}
            {SHOP_ITEM _"Sword" 30 176 items/sword.png i13}
            {SHOP_ITEM _"Tarnished sword" 12 36 items/sword.png i14}
            [option]
                message="Back"
                [command]
                    [fire_event]
                        name=buy_weapons
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
        [/message]
    [/event]

    [event]
        name=buy_maces
        first_time_only=no
        [message]
            speaker=trader
            message= _ "What kind of mace do you want to buy?"
            {SHOP_ITEM _"Old finned mace" 24 85 items/mace.png i15}
            {SHOP_ITEM _"Mace" 21 41 items/mace.png i19}
            {SHOP_ITEM _"Nicked mace" 15 42 items/mace.png i18}
            {SHOP_ITEM _"Common hammer" 12 43 items/hammer-runic.png i17}
            {SHOP_ITEM _"Chew bone" 3 57 items/mace.png i16}
            [option]
                message="Back"
                [command]
                    [fire_event]
                        name=buy_weapons
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
        [/message]
    [/event]

    [event]
        name=buy_axes
        first_time_only=no
        [message]
            speaker=trader
            message= _ "What kind of axe do you want to buy?"
            {SHOP_ITEM _"Splitting maul" 27 31 items/axe.png i24}
            {SHOP_ITEM _"Blunt axe" 24 87 items/axe.png i20}
            {SHOP_ITEM _"Splitter" 18 90 items/axe.png i22}
            {SHOP_ITEM _"Woodman's axe" 18 62 items/axe.png i23}
            {SHOP_ITEM _"Massive axe" 17 89 items/axe.png i21}
            [option]
                message="Back"
                [command]
                    [fire_event]
                        name=buy_weapons
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
        [/message]
    [/event]

    [event]
        name=buy_gloves_forge
        first_time_only=no
        [message]
            speaker=trader
            message= _ "What kind of gloves do you want to buy?"
            {SHOP_ITEM _"Rough leather gloves of vivacity" 1410 70 items/gauntlets.png i25}
            {SHOP_ITEM _"Ripper leather gloves of the fox" 1410 71 items/gauntlets.png i26}
            {SHOP_ITEM _"Aggressive studded leather gloves" 390 78 items/gauntlets.png i27}
            [option]
                message="Back"
                [command]
                    [fire_event]
                        name=buy_armours_forge
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
        [/message]
    [/event]

    [event]
        name=buy_boots_forge
        first_time_only=no
        [message]
            speaker=trader
            message= _ "What kind of boots do you want to buy?"
            {SHOP_ITEM _"Rough studded leather boots of the fox" 708 73 items/boots.png i28}
            {SHOP_ITEM _"Spiky leather boots" 555 74 items/boots.png i29}
            {SHOP_ITEM _"Studded leather boots of the fox" 555 75 items/boots.png i30}
            [option]
                message="Back"
                [command]
                    [fire_event]
                        name=buy_armours_forge
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
        [/message]
    [/event]

    [event]
        name=buy_helms_forge
        first_time_only=no
        [message]
            speaker=trader
            message= _ "What kind of helm do you want to buy?"
            {SHOP_ITEM _"Olaf's helm of the fox" 285 76 items/helmet.png i31}
            {SHOP_ITEM _"Felt morion of the fox" 189 79 items/helmet.png i32}
            [option]
                message="Back"
                [command]
                    [fire_event]
                        name=buy_armours_forge
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
        [/message]
    [/event]

    [event]
        name=buy_armours_forge_2
        first_time_only=no
        [message]
            speaker=trader
            message= _ "What kind of armour do you want to buy?"
            {SHOP_ITEM _"Riveted leather" 582 68 items/armour-leather.png i34}
            {SHOP_ITEM _"Worn brigandine" 400 66 items/armour.png i35}
            {SHOP_ITEM _"Cracked rigid leather" 180 77 items/armour-leather.png i33}
            {SHOP_ITEM _"Linen cloth" 60 63 items/armour-leather.png i36}
            [option]
                message="Back"
                [command]
                    [fire_event]
                        name=buy_armours_forge
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
        [/message]
    [/event]

    [event]
        name=buy_shields_forge
        first_time_only=no
        [message]
            speaker=trader
            message= _ "What kind of shield do you want to buy?"
            {SHOP_ITEM _"Rough wooden targe of the fox" 801 91 items/buckler.png i37}
            {SHOP_ITEM _"Avenging thick wooden shield" 453 92 items/buckler.png i38}
            {SHOP_ITEM _"Firm wooden targe" 384 95 items/buckler.png i41}
            {SHOP_ITEM _"Painted shield" 72 93 items/buckler.png i39}
            {SHOP_ITEM _"Wooden targe" 42 94 items/buckler.png i40}
            [option]
                message="Back"
                [command]
                    [fire_event]
                        name=buy_armours_forge
                        [primary_unit]
                            x,y=$x1,$y1
                        [/primary_unit]
                    [/fire_event]
                [/command]
            [/option]
        [/message]
    [/event]

SHOP ITEM is a macro I use from LotI as well as the whole item management system of LotI. Basically, it simply drops the object on the hex where the buyer is and remove the price of the object from side's gold. Arguments are name of the object in the shop, price of the object, number of the object in my objects list, icon of the object, shop item number.

Code: Select all

#define SHOP_ITEM NAME PRICE NUMBER IMAGE VARIABLE_NAME
	[option]
		label={NAME} + " (" + {PRICE} + _" gold)"
		image={IMAGE}
		[command]
			{FOREACH item_list.object j}
				[if]
					[variable]
						name=item_list.object[$j].number
						equals={NUMBER}
					[/variable]
					[then]
						{VARIABLE description $item_list.object[$j].description}
					[/then]
				[/if]
			{NEXT j}
			[message]
				id=trader
				[or]
					id=trader1
				[/or]
				[or]
					id=trader2
				[/or]
				[or]
					id=trader3
				[/or]
				message= _ "Look at it. Do you really want it?
$description"
				[option]
					label= _ "Yes."
					[command]
						[if]
							[variable]
								name=gold
								greater_than_equal_to={PRICE}
							[/variable]
							[else]
								[message]
									speaker=trader
									message= _ "You do not have enough gold. Listen, if I sold it to you for less gold, I would not have enough gold to bribe the enemies to leave me alone."
								[/message]
							[/else]
							[then]
								{PLACE_ITEM {NUMBER} $x1 $y1}
								[gold]
									side=$side_number
									amount=-{PRICE}
								[/gold]
								{VARIABLE_OP items_dropped add 1}
								{VARIABLE {VARIABLE_NAME} 1}
								[fire_event]
									name=item_pick
									[primary_unit]
										x,y=$x1,$y1
									[/primary_unit]
								[/fire_event]
							[/then]
						[/if]
					[/command]
				[/option]
				[option]
					label= _ "No."
					[command]
					[/command]
				[/option]
			[/message]
			{CLEAR_VARIABLE description}
		[/command]
		[show_if]
			[variable]
				name={VARIABLE_NAME}
				not_equals=1
			[/variable]
		[/show_if]
	[/option]
#enddef
Last edited by lhybrideur on September 22nd, 2020, 12:50 pm, edited 1 time in total.
User avatar
Ravana
Forum Moderator
Posts: 2952
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Likelyhood of coming across issues with nested custom events

Post by Ravana »

Your example does not have any nested events - [event][event] [/event][/event].
User avatar
lhybrideur
Posts: 357
Joined: July 9th, 2019, 1:46 pm

Re: Likelyhood of coming across issues with nested custom events

Post by lhybrideur »

True. It is not nested events. It is use of fire_event to avoid nested events actually.
I modified my previous post accordingly (and added the SHOP_ITEM macro too)
User avatar
LordAwsomeness
Posts: 203
Joined: August 12th, 2013, 2:20 pm
Location: U.S.A.

Re: Likelyhood of coming across issues with nested custom events

Post by LordAwsomeness »

WhiteWolf wrote: September 20th, 2020, 7:10 pm (PS: You probably know this and it's just me reading a bit of confusion into your post, but just to be sure :) : There's a difference between a nested event and a fired event.)
haha I know nested events are events inside of events (say like a custom event inside of a moveto event). Thank you! Your insight is helpful!

Ravana wrote: September 21st, 2020, 6:46 pm Your example does not have any nested events - [event][event] [/event][/event].
The point of this post isnt to display nested events. The point of this post is to ask people who are possibly knowledgeable in this area if having a ton of nested events are gonna cause issues.
lhybrideur wrote: September 21st, 2020, 3:16 pm Nested events are far better in term of optimization.
Here are my nested shop events if you need it.

SHOP ITEM is a macro I use from LotI as well as the whole item management system of LotI. Basically, it simply drops the object on the hex where the buyer is and remove the price of the object from side's gold. Arguments are name of the object in the shop, price of the object, number of the object in my objects list, icon of the object, shop item number.
Yes! Thank you!
I will be looking into this! The menus from LotI has really been growing on me recently and Dugi has been helping teach me the lua that will aid me in making my own menus like that sometime in the future! (when i have more time to commit). I appreciate your input on nested events!

EDIT: Some of my code already has nested events like having a [set_menu_item] inside of the start event and having custom events inside of the [set_menu_item]'s commands.
- Been playing Wesnoth since 2004 and the 1.0.x versions.
- Creator of Undead Invasion MP Scenario Pack.
- Creator of Valeria MP Adventure
- Creator of LA_RPG ERA
User avatar
Pentarctagon
Project Manager
Posts: 5531
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Likelyhood of coming across issues with nested custom events

Post by Pentarctagon »

Are you really asking about nested events, or are you asking about using [fire_event]? Those are different things.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
Post Reply