Items help

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
FokaCream
Posts: 1
Joined: January 25th, 2019, 10:09 pm

Items help

Post by FokaCream »

I just spent days trying to create "collectable items" like a sword, bow or a item to enchant an attack with Magical
I was thinking of something like the Artifact Mod, i searched the entire wiki but can't unferstand how items work
I tried Using effects, events etc and even all together, but it doesn't do anything



(Preset WMLs are Welcome, also forums and sites for help)
{Yup i'm a Noob}
User avatar
Ravana
Forum Moderator
Posts: 3000
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Items help

Post by Ravana »

Most commonly items are implemented as [object][effect]. Effect can also be added as trait https://github.com/ProditorMagnus/Oroci ... #L263-L277
User avatar
James_The_Invisible
Posts: 534
Joined: October 28th, 2012, 1:58 pm
Location: Somewhere in the Northlands, fighting dark forces
Contact:

Re: Items help

Post by James_The_Invisible »

You can find some examples in mainline. Look at data/core/macros/items.cfg. Several mainline campaigns also make use of them so you can check them out. (User-made campaign Legend of the Invincibles also makes heavy use of them but it might be too complicated for someone who is new to WML.)
User avatar
beetlenaut
Developer
Posts: 2825
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: Items help

Post by beetlenaut »

Here are two examples from UtBS with a lot of irrelevant code removed. The first gives the unit a ring of speed when it enters the hex. This is about the bare minimum code required to add an object to a unit. The second example shows how it is usually done in a game. It enchants a weapon, but it has a message and options to allow the player to choose what to do. You probably want to do all that eventually. (I don't think I introduced an error, but I didn't test the code, so I can't be sure.)

Code: Select all

[event]
    name=moveto

    [filter]
        x=10, y=10
    [/filter]

    [object]
        name= _ "Ring of Speed"
        description= _ "This ring will increase your maximum speed by 1."
        [filter]
            x=10, y=10
        [/filter]

        [effect]
            apply_to=movement
            increase=1
        [/effect]
    [/object]
[/event]

Code: Select all

[event]
    name=moveto
    first_time_only=no

    [filter]
        x=10, y=10
        side=1
    [/filter]

    [message]
        speaker=unit
        message= _ "Should I use this enchantment?"
        [option]
            label= _ "Yes, I’ll take it."

            [command]
                [object]
                    # An object with an ID can only be taken once:
                    id=enchantment
                    [filter]
                        x=10, y=10
                        side=1
                    [/filter]

                    [effect]
                        apply_to=attack
                        range=melee
                        [set_specials]
                            {WEAPON_SPECIAL_MAGICAL}
                        [/set_specials]
                    [/effect]
                [/object]
                
                # This part removes the IMAGE from the map:
                [remove_item]  
                    x=10, y=10
                [/remove_item]
            [/command]
        [/option]

        [option]
            label= _ "No, I think someone else should use it."
            # Without this, the player couldn't undo if they chose "no":
            [command]
                [allow_undo]
                [/allow_undo]
            [/command]
        [/option]
    [/message]
[/event]
EDIT: Welcome to the forum!
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
TrashMan
Posts: 601
Joined: April 30th, 2008, 8:04 pm
Contact:

Re: Items help

Post by TrashMan »

Personally, I prefer to use a PICK_UP macro. Less errors and easier to implement.

Code: Select all

#define PICK_UP _IMG _X _Y _VERBOSE_MSG _OBJECT_CODE_AND_ACTIONS
    [event]
        name=victory
        [clear_variable]
            name=pickups.generic_flag_{_X}_{_Y}
        [/clear_variable]
    [/event]
    [item]
        image={_IMG}
        x={_X}
        y={_Y}
    [/item]
    [event]
        name=moveto
        first_time_only=no
        [filter]
            side=1
            x={_X}
            y={_Y}
        [/filter]
        {REDRAW}
        [if]
            [variable]
                boolean_equals=no
                name=pickups.generic_flag_{_X}_{_Y}
            [/variable]
            [then]
                [message]
                    speaker=unit
                    message={_VERBOSE_MSG}
                [/message]
                [message]
                    speaker=narrator
                    image=wesnoth-icon.png
                    message= _ "Do you want this unit to pick up this item?"
                    [option]
                        message= _ "Yes"
                        [command]
                            {_OBJECT_CODE_AND_ACTIONS}
                            [+object]
                                [+then]
                                    # Mark object as picked-up; won't happen if the object's filter
                                    # doesn't match primary_unit
                                    [set_variable]
                                        name=pickups.generic_flag_{_X}_{_Y}
                                        value="yes"
                                    [/set_variable]
                                [/then]
                            [/object]
                        [/command]
                    [/option]
                    [option]
                        message= _ "No"
                        [command]
                            [allow_undo]
                            [/allow_undo]
                        [/command]
                    [/option]
                [/message]
            [/then]
        [/if]
    [/event]
#enddef

#define RING_DEFENSE X Y
    {PICK_UP (items/ring-gold.png) ({X}) ({Y})
    ( _ "A gold ring, and there is a inscription on it. 'May enemy attacks shatter upon you like water upon a might rock.'")
    (
        [object]
            image=items/ring-gold.png
            duration=forever

            name= _ "Ring of Impregnable Defense"
            description= _ "+10% defense on all terrains."
            [filter]
                x={X}
                y={Y}
            [/filter]
            [then]
                [remove_item]
                    x,y={X},{Y}
                [/remove_item]
            [/then]
            [effect]
				apply_to=defense
				replace=no
				[defense]
					deep_water=-10%
					shallow_water=-10%
					flat=-10%
					forest=-10%
					frozen=-10%
					desert=-10%
					hills=-10%
					mountains=-10%
					village=-10%
					castle=-10%
					cave=-10%
					fungus=-10%
				[/defense]
            [/effect]
			[effect]
				apply_to=overlay
				add="overlays/armor-icon.png"
			[/effect]				
        [/object]
    )}
#enddef

And then in the scenario file you place it with somewhere in the [prestart] {RING_DEFENSE 33 70}
Light travels much faster than sound, that's why some people seem bright until you hear them speak.

>>> MY LITTLE LAB! <<<
Post Reply