Loading a unit onto a boat

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
storsnorre
Posts: 11
Joined: August 8th, 2014, 1:49 am

Loading a unit onto a boat

Post by storsnorre »

I have found boats rather boring to play with and they are seldom used in the first place. I'm trying to allow ordinary "landlocked" units to enter onto a boat so they can be transported across water. Instead of using multiple recall lists, I intend to use a storage space inside the definition of a boat (all units have this) called "variables", but I'm less than stellarly successful with this. If anyone know of any examples of this being done elsewhere, I'll be really interested. Or if anyone knows that the "variables" storage space is unsuitable for storing other units, please let me know.

Here's my attempt at code that's troubling me:
I create a menu item that is accessible only if you rightclick on a boat that belongs to you (side=1) and has a "nonboat" unit (also side=1) next to it. This part of the code appear to be functional. I first "store" the boat into tmpboat, then "store" the neighbor landlocked unit onto tmpboat.variables. When I save the game at this point (that's why CLEAR_VARIABLE is commented out in the code below), there is a tmpboat unit in the saved game (saving as text file), and it has the landunit in it (tmpboat.variables). However, after having "unstored" the boat, it's "variables" is still empty.
Where's the bug?

Code: Select all

#define PICKUPPASSENGERMENU
[event]
    name=prestart

    # Set up a menu item for a boat picking up all nearby units except other boats. 

    [set_menu_item]
        id=Pmenu_pickuppassenger
        description="Pick up nearby units"
        # image= ... a boat image ...
        
        # Define on which hexes this menu option should appear:
        # The hex should have a boat on side=1 and it should have a nonboat side=1 unit adjacent
        [filter_location]
            [filter]
                side=1
                type=Boat,Galleon,Pirate Galleon,Transport Galleon
                [filter_adjacent]
                    side=1
                    [not]
                        type=Boat,Galleon,Pirate Galleon,Transport Galleon
                    [/not]
                [/filter_adjacent]
            [/filter]
        [/filter_location]

        # Define what to do when the menu item is chosen. 
        # $x1, $y1 is the coordinates on which the menu was triggered and has the boat ($unit) that is to be loaded
        [command]
            [message]
                speaker=narrator
                message=_"Location is ($x1,$y1). Boat is $unit.name."
            [/message]

            # store_unit can store many units at the same time. 
            # Is it possible to store an array of units into the boat.variables?  (see SingleUnitWML.html)

            # First store the boat on which the menu item was triggered, since we are modifying it:
            [store_unit]
                variable=tmpboat
                [filter]
                    id=$unit.id    
                [/filter]
            [/store_unit]

            # Now grab all the nearby nonboat units and store them into tmpboat.variables: 
            [store_unit]
                variable=tmpboat.variables
                mode=append
                kill=yes
                [filter]
                    side=1
                    [filter_location]
                        x,y=$x1,$y1
                        radius=1
                    [/filter_location]
                    [not]
                        type=Boat,Galleon,Pirate Galleon,Transport Galleon
                    [/not]
                [/filter]
            [/store_unit]

            # Restore the boat that has units stored on it onto the map            
            [unstore_unit]
                variable=tmpboat
            [/unstore_unit]
            
            #[clear_variable]
            #    name=tmpboat
            #[/clear_variable]

            [message]
                speaker=narrator
                message=_"Units are $unit.variables.length"
            [/message]

        [/command]

    [/set_menu_item]

[/event]

#enddef
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: Loading a unit onto a boat

Post by Sapient »

You should store to a subcontainer within variables.

Code: Select all

variable=tmpboat.variables.passengers
instead, you created a bunch of [variables] containers, but the game only recognizes and uses the first one (the original variables container).
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
User avatar
pyrophorus
Posts: 533
Joined: December 1st, 2010, 12:54 pm

Re: Loading a unit onto a boat

Post by pyrophorus »

storsnorre wrote:... If anyone know of any examples of this being done elsewhere, I'll be really interested. Or if anyone knows that the "variables" storage space is unsuitable for storing other units, please let me know...
You could take a look at my campaign Return To Noelren (1.10), scenario "Building an army". It features boats accepting to load and unload units. If this fits your need, ask me. I have made an easy to include macro version.

Friendly,
Groggy_Dice
Inactive Developer
Posts: 165
Joined: February 4th, 2011, 6:19 am
Contact:

Re: Loading a unit onto a boat

Post by Groggy_Dice »

In 1.11, SotBE 11 has been rewritten to have a boarding/debarking system for ships carrying the human army across the river.
Ports:
Prudence (Josh Roby) | By the Sword (monochromatic) | The Eight of Cembulad (Lintana~ & WYRMY)
Resources:
UMC Timeline (Dec) | List of Unported UMC (Dec) | wmllint++ (Feb)
Post Reply