Help on creating an array of stored units.

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.
User avatar
MoonyDragon
Posts: 149
Joined: November 29th, 2017, 5:46 pm

Re: Help on creating an array of stored units.

Post by MoonyDragon »

I attempted a similar ship mod during the development of 'Palms amid Blue Dunes' (currently on ice) and perhaps my solution can be of some help to you...

First, I created a custom dummy ability and slapped it on a custom ship unit:

Code: Select all

#define ABILITY_TRANSPORT
    # Canned definition of the transport ability to be included in an [abilities] clause.
    [dummy]
        id=transport
        name= _"transport"
        description= _"This unit is capable of transporting non-mechanical units of the same side.
        
To board adjacent units, select this unit and press 'c'. To unboard units onto adjacent terrain, select this unit and press 'v'."

        [filter]
            status=transport
        [/filter]
    [/dummy]
#enddef

#define NOTE_TRANSPORT
[special_note]
    note= _"This unit is capable of transporting non-mechanical units of the same side."
[/special_note]
#enddef

Code: Select all

[unit_type]
    id=Carrack
    name= _"Carrack"
    race=mechanical
    image="units/transport/transport-galleon.png"
    profile="units/transport/transport-galleon.png~RC(magenta>red)~XBRZ(4)"
    hitpoints=56
    movement_type=float
    movement=6
    experience=100
    level=2
    alignment=neutral
    advances_to=null
    {AMLA_DEFAULT}
    cost=46
    usage=mixed fighter
    description= _"Carracks are well-armed ships that carry troops. If they reach the shore, they can land the troops to attack."
    {NOTE_TRANSPORT}
    {NOTE_MARKSMAN}
    die_sound=wose-die.ogg

    [abilities]
        {ABILITY_TRANSPORT}
    [/abilities]

    [defend]
        [if]
            hit_sound_start_time=-25
            hits=hit
            [hit_sound_frame]
                sound=wose-hit.ogg
            [/hit_sound_frame]
        [/if]
    [/defend]

    [movement_costs]
        sand=2
        castle=3
    [/movement_costs]

    [defense]
        deep_water=50
        shallow_water=60
        reef=70
        swamp_water=70
        sand=80
        castle=70
    [/defense]

    [resistance]
        blade=100
        pierce=100
        impact=100
        fire=150
        cold=80
        arcane=40
    [/resistance]

    [attack]
        name=ballista
        description= _"ballista"
        icon=attacks/ballista.png
        type=pierce
        range=ranged
        damage=32
        number=1
        [specials]
            {WEAPON_SPECIAL_MARKSMAN}
        [/specials]
    [/attack]

    [attack_anim]
        [filter_attack]
            name=ballista
        [/filter_attack]
        missile_start_time=-150
        [missile_frame]
            duration=150
            image="projectiles/bullet.png"
            image_diagonal="projectiles/bullet.png"
        [/missile_frame]
        start_time=-300
        {SOUND:HIT_AND_MISS crossbow.ogg crossbow-miss.ogg -300}
    [/attack_anim]
[/unit_type]

And then, I coded some macros that are explained below... I can add a more detailed explanation later, if you wish so. Basically, ENTER_TRANSPORT stores the FILTER_UNITS units in a variable inside the FILTER_VESSEL units. LEAVE_TRANSPORT does the reverse, and TRANSPORT_SHIP_MENU is the menu that allows the player to right-click the ships. TRANSPORT_SHIP_EVENTS is responsible for ejecting all stored units from a dying ship and clearing up the variable and unit mess after victory.

Code: Select all

# perhaps a lua version of this would cause less trouble... at some point, create the following:
# tag [enter_transport] will require [filter_vessel] (store_unit keys) for transport and [filter_units]     (store_unit keys)     for passengers
# tag [leave_transport] will require [filter_vessel] (store_unit keys) for transport and [filter_locations] (store_location keys) for passengers

#define ENTER_TRANSPORT FILTER_VESSEL FILTER_UNITS
[store_unit]
    {FILTER_VESSEL}

    variable=vessel
[/store_unit]

[for]
    array=vessel
    variable=n
    reverse=yes
    [do]
        [for]
            start=$vessel[$n].variables.passengers.length
            variable=i
            end=5 # $vessel[$n].level*2
            [do]
                [store_unit]
                    {FILTER_UNITS}
                
                    variable=target
                [/store_unit]

                [if]
                    [variable]
                        name=target.length
                        greater_than=0
                    [/variable]

                    [then]
                        [set_variable]
                            name=random
                            rand=0..$($target.length-1)
                        [/set_variable]

                        [store_unit]
                            [filter]
                                x,y=$target[$random].x,$target[$random].y
                            [/filter]
                            variable=vessel[$n].variables.passengers
                            mode=append
                        [/store_unit]

                        [clear_variable]
                            name=random
                        [/clear_variable]

                        [kill]
                            x,y=$vessel[$n].variables.passengers[$i].x,$vessel[$n].variables.passengers[$i].y
                        [/kill]
                
                        [sound]
                            name=miss-[1~3].ogg
                        [/sound]
                
                        [move_unit_fake]
                            x=$vessel[$n].variables.passengers[$i].x,$vessel[$n].x
                            y=$vessel[$n].variables.passengers[$i].y,$vessel[$n].y
                            type=$vessel[$n].variables.passengers[$i].type
                            side=$vessel[$n].variables.passengers[$i].side
                        [/move_unit_fake]

                        [if]
                            [variable]
                                name=vessel[$n].variables.passengers[$i].canrecruit
                                equals=yes
                            [/variable]
                            [then]
                                [set_variable]
                                    name=vessel[$n].canrecruit
                                    value=yes
                                [/set_variable]
                            [/then]
                        [/if]

                        [unstore_unit]
                            variable=vessel[$n]
                            find_vacant=no
                        [/unstore_unit]
                
                        [unit_overlay]
                            x,y=$vessel[$n].x,$vessel[$n].y
                            image=misc/blank-hex.png~BLIT(misc/leader-crown-alt.png~FL(horiz),0,$($i*8))
                        [/unit_overlay]
                
                        [store_unit]
                            [filter]
                                x,y=$vessel[$n].x,$vessel[$n].y
                            [/filter]
                            variable=vessel[$n]
                        [/store_unit]
                    [/then]
                [/if]

                [clear_variable]
                    name=target
                [/clear_variable]
            [/do]
        [/for]

        [if]
            [variable]
                name=vessel[$n].variables.passengers.length
                greater_than=0
            [/variable]
            [then]
                [modify_unit]
                    [filter]
                        x,y=$vessel[$n].x,$vessel[$n].y
                    [/filter]
        
                    [effect]
                        apply_to=status
                        add=transport
                    [/effect]
                [/modify_unit]
            [/then]
        [/if]
        
        [clear_variable]
            name=vessel[$n]
        [/clear_variable]
    [/do]
[/for]
#enddef


#define LEAVE_TRANSPORT FILTER_VESSEL FILTER_LOCATIONS
[store_unit]
    {FILTER_VESSEL}

    variable=vessel
[/store_unit]

[for]
    array=vessel
    variable=n
    reverse=yes
    [do]
        [for]
            array=vessel[$n].variables.passengers
            variable=i
            reverse=yes
            [do]
                [store_locations]
                    {FILTER_LOCATIONS}

                    variable=target
                [/store_locations]

                [if]
                    [variable]
                        name=target.length
                        greater_than=0
                    [/variable]

                    [then]
                        [set_variable]
                            name=random
                            rand=0..$($target.length-1)
                        [/set_variable]
                        
                        [sound]
                            name=miss-[1~3].ogg
                        [/sound]
    
                        [move_unit_fake]
                            x=$vessel[$n].x,$target[$random].x
                            y=$vessel[$n].y,$target[$random].y
                            type=$vessel[$n].variables.passengers[$i].type
                            side=$vessel[$n].variables.passengers[$i].side
                        [/move_unit_fake]
        
                        [unstore_unit]
                            variable=vessel[$n].variables.passengers[$i]
        
                            x=$target[$random].x
                            y=$target[$random].y
                        [/unstore_unit]
        
                        [clear_variable]
                            name=random
                        [/clear_variable]
    
                        [if]
                            [variable]
                                name=vessel[$n].variables.passengers[$i].canrecruit
                                equals=yes
                            [/variable]
                            [then]
                                [set_variable]
                                    name=vessel[$n].canrecruit
                                    value=no
                                [/set_variable]
                            [/then]
                        [/if]
    
                        [clear_variable]
                            name=vessel[$n].variables.passengers[$i]
                        [/clear_variable]
    
                        [unstore_unit]
                            variable=vessel[$n]
                            find_vacant=no
                        [/unstore_unit]
    
                        [remove_unit_overlay]
                            x,y=$vessel[$n].x,$vessel[$n].y
                            image=misc/blank-hex.png~BLIT(misc/leader-crown-alt.png~FL(horiz),0,$($i*8))
                        [/remove_unit_overlay]
    
                        [store_unit]
                            [filter]
                                x,y=$vessel[$n].x,$vessel[$n].y
                            [/filter]
                            variable=vessel[$n]
                        [/store_unit]
                    [/then]
                [/if]

                [clear_variable]
                    name=target
                [/clear_variable]
            [/do]
        [/for]

        [if]
            [variable]
                name=vessel[$n].variables.passengers.length
                less_than=1
            [/variable]
            [then]
                [modify_unit]
                    [filter]
                        x,y=$vessel[$n].x,$vessel[$n].y
                    [/filter]
    
                    [effect]
                        apply_to=status
                        remove=transport
                    [/effect]
                [/modify_unit]
            [/then]
        [/if]

        [clear_variable]
            name=vessel[$n]
        [/clear_variable]
    [/do]
[/for]
#enddef



#define TRANSPORT_SHIP_MENU
[event]
    name=prestart

    [set_menu_item]
        id=enter_ship
        description= _"Enter This Ship"
        image=icons/unit-groups/race_mechanical2_30.png~SCALE(25,25) #buttons/WML-custom.png
        [default_hotkey]
            key="c"
        [/default_hotkey]

        [show_if]
            [have_unit]
                x,y=$x1,$y1
                side=$side_number
                race=mechanical

                [not]
                    status=transport
                [/not]

                [filter_adjacent]
                    side=$unit.side
                    [not]
                        race=mechanical
                    [/not]
                [/filter_adjacent]
            [/have_unit]
        [/show_if]

        [command]
            {ENTER_TRANSPORT
            (
                [filter]
                    x,y=$x1,$y1
                [/filter]
            )
            (
                [filter]
                    [filter_location]
                        x,y=$x1,$y1
                        radius=1
                    [/filter_location]
            
                    side=$side_number
                    [not]
                        race=mechanical
                    [/not]
                [/filter]
            )
            }
        [/command]
    [/set_menu_item]

    [set_menu_item]
        id=leave_ship
        description= _"Leave This Ship"
        image=icons/unit-groups/race_mechanical2_30-pressed.png~SCALE(25,25) #buttons/WML-custom.png
        [default_hotkey]
            key="v"
        [/default_hotkey]

        [show_if]
            [have_unit]
                x,y=$x1,$y1
                side=$side_number
                race=mechanical

                status=transport
            [/have_unit]
        [/show_if]

        [command]
            {LEAVE_TRANSPORT
            (
                [filter]
                    x,y=$x1,$y1
                [/filter]
            )
            (
                terrain=!,Wo*,Q*,X*
        
                [filter_adjacent_location]
                    x,y=$x1,$y1
                [/filter_adjacent_location]
        
                [not]
                    [filter]
                    [/filter]
                [/not]
            )
            }
        [/command]
    [/set_menu_item]
[/event]
#enddef



#define TRANSPORT_SHIP_EVENTS
[event]
    name=last breath
    first_time_only=no

    [filter]
        race=mechanical
        status=transport
    [/filter]

    {LEAVE_TRANSPORT
    (
        [filter]
            x,y=$x1,$y1
        [/filter]
    )
    (
        [not]
            [filter]
            [/filter]
        [/not]
    
        [and]
            x,y=$x1,$y1
            radius=2
        [/and]
    )
    }
[/event]


[event]
    name=victory

    [clear_menu_item]
        id=enter_ship
    [/clear_menu_item]

    [clear_menu_item]
        id=leave_ship
    [/clear_menu_item]

    {LEAVE_TRANSPORT
    (
        [filter]
            race=mechanical
            status=transport
        [/filter]
    )
    (
        x=recall
        y=recall
    )
    }
[/event]
#enddef

To make use of it all, include the following code in your scenario or modification:

Code: Select all

{TRANSPORT_SHIP_MENU}
{TRANSPORT_SHIP_EVENTS}
Default L0 Era - Level 1 leaders with level 0 recruits!
User avatar
Legend_Rider
Posts: 35
Joined: June 20th, 2018, 4:51 pm

Re: Help on creating an array of stored units.

Post by Legend_Rider »

MoonyDragon wrote: February 4th, 2021, 10:53 pm To make use of it all, include the following code in your scenario or modification:

Code: Select all

{TRANSPORT_SHIP_MENU}
{TRANSPORT_SHIP_EVENTS}
Any idea why this is happening? how to fix? :augh:
error preprocessor:
Macro/file 'TRANSPORT_SHIP_MENU' is missing
at ~add-ons/ships_mod/ships_mod.cfg:37
included from ~add-ons/ships_mod/_main.cfg:2
I didn't change anything, I simply copied your code. :doh:

Could be I didn't put these {TRANSPORT_SHIP_MENU} {TRANSPORT_SHIP_EVENTS} in the right place? :hmm:
User avatar
MoonyDragon
Posts: 149
Joined: November 29th, 2017, 5:46 pm

Re: Help on creating an array of stored units.

Post by MoonyDragon »

Legend_Rider wrote: February 5th, 2021, 7:19 pm
error preprocessor:
Macro/file 'TRANSPORT_SHIP_MENU' is missing
at ~add-ons/ships_mod/ships_mod.cfg:37
included from ~add-ons/ships_mod/_main.cfg:2
I didn't change anything, I simply copied your code. :doh:

Could be I didn't put these {TRANSPORT_SHIP_MENU} {TRANSPORT_SHIP_EVENTS} in the right place? :hmm:
In your _main.cfg file, you reference ships_mod.cfg before the macros/ folder, which causes the error. You cannot use a macro that has not yet been defined.

I have attached a modified version of your mod below, where you can additionally build a "Carrack" for 46g. I have tested its transport ability on 1.15 and it works flawlessly. If you play on 1.14, delete the {NOTE_TRANSPORT} macro from abilities.cfg and Carrack.cfg, since these are 1.15-exclusive.
ships_mod.7z
(6.05 KiB) Downloaded 160 times
Default L0 Era - Level 1 leaders with level 0 recruits!
User avatar
Legend_Rider
Posts: 35
Joined: June 20th, 2018, 4:51 pm

Re: Help on creating an array of stored units.

Post by Legend_Rider »

MoonyDragon wrote: February 5th, 2021, 7:41 pm In your _main.cfg file, you reference ships_mod.cfg before the macros/ folder, which causes the error. You cannot use a macro that has not yet been defined.
Oh, I wasn't aware that the order was important here, good to know! Thanks! :)
MoonyDragon wrote: February 5th, 2021, 7:41 pm I have attached a modified version of your mod below, where you can additionally build a "Carrack" for 46g. I have tested its transport ability on 1.15 and it works flawlessly. If you play on 1.14, delete the {NOTE_TRANSPORT} macro from abilities.cfg and Carrack.cfg, since these are 1.15-exclusive.
Ok, good info, I hadn't seen {NOTE_TRANSPORT} before, but didn't know that it was exclusive to 1.15+

Big help with these pointers, and your macro.
Being able to look at something that does most of what I want to accomplish, I can begin to understand it all better and get a feel for how variables work etc.

I do have a few questions:

1. I see people use $i a lot, is there any specific significance to that letter? or is it simply something people choose because it isn't used for anything else?

2. If you could help explain a little bit of what $n is used for here / what it does how it works that would be awesome too!

Edit:
3. When you say:
# at some point, create the following:
# tag [enter_transport] will require [filter_vessel] (store_unit keys) for transport and [filter_units] (store_unit keys) for passengers
# tag [leave_transport] will require [filter_vessel] (store_unit keys) for transport and [filter_locations] (store_location keys) for passengers
is that something you're recommending that I do at some point? or is it something that's related to the work you (were?) doing on your project?

Thanks a bundle!
User avatar
MoonyDragon
Posts: 149
Joined: November 29th, 2017, 5:46 pm

Re: Help on creating an array of stored units.

Post by MoonyDragon »

Legend_Rider wrote: February 5th, 2021, 8:13 pm 1. I see people use $i a lot, is there any specific significance to that letter? or is it simply something people choose because it isn't used for anything else?
People use $i in for/while loops mostly out of convention, and because writing $iterator each time cramps things unnecessarily. The iterator variable is deleted as soon as the loop ends, so 'used for anything else' isn't normally an issue.

Legend_Rider wrote: February 5th, 2021, 8:13 pm 2. If you could help explain a little bit of what $n is used for here / what it does how it works that would be awesome too!
If I use a loop within a loop, then the iterator from the outer loop is still running, ergo not yet deleted, during the time the inner loop starts and ends.
This means that I will have to use a different iterator variable for my inner loop to not mix things up. The convention is to use $i,$j,$k,$l,etc - but in this instance I used $n for no special reason whatsoever.

Legend_Rider wrote: February 5th, 2021, 8:13 pm 3. When you say:
# at some point, create the following:
# tag [enter_transport] will require [filter_vessel] (store_unit keys) for transport and [filter_units] (store_unit keys) for passengers
# tag [leave_transport] will require [filter_vessel] (store_unit keys) for transport and [filter_locations] (store_location keys) for passengers
is that something you're recommending that I do at some point? or is it something that's related to the work you (were?) doing on your project?
Nah, that was originally a message to myself, to create two WML tags that replace the macro's job. I still haven't gotten around to learn LUA yet, so it's irrelevant for now.
Default L0 Era - Level 1 leaders with level 0 recruits!
User avatar
Legend_Rider
Posts: 35
Joined: June 20th, 2018, 4:51 pm

Re: Help on creating an array of stored units.

Post by Legend_Rider »

MoonyDragon wrote: February 5th, 2021, 9:56 pm People use $i in for/while loops mostly out of convention, and because writing $iterator each time cramps things unnecessarily. The iterator variable is deleted as soon as the loop ends, so 'used for anything else' isn't normally an issue.
MoonyDragon wrote: February 5th, 2021, 9:56 pm If I use a loop within a loop, then the iterator from the outer loop is still running, ergo not yet deleted, during the time the inner loop starts and ends.
This means that I will have to use a different iterator variable for my inner loop to not mix things up. The convention is to use $i,$j,$k,$l,etc - but in this instance I used $n for no special reason whatsoever.
Ok, very informative :) :eng: (I didn't really know what was going on at all aka. didn't know the existence of iterators... :P )
MoonyDragon wrote: February 5th, 2021, 9:56 pm Nah, that was originally a message to myself, to create two WML tags that replace the macro's job. I still haven't gotten around to learn LUA yet, so it's irrelevant for now.
Alright, so I don't have to mess with any of that :whistle:

Thanks a bundle!
User avatar
Pentarctagon
Project Manager
Posts: 5562
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Help on creating an array of stored units.

Post by Pentarctagon »

$i isn't really an iterator variable, it's the current index of the array that's being looped over. WML generally doesn't have the concept of an iterator variable outside of [foreach] though, so for example:

Code: Select all

[foreach]
    array=some_array
    variable=iter_var
    index_var=i
    [do]
    # stuff
    [/do]
[/foreach]
iter_var is the iterator variable - it directly contains the value of the array at the current index.
i is the index variable - it contains the value of the index of the current element in some_array, which would be some numeric value (1, 4, 18, etc).

To say it another way - in the above example, using $iter_var is identical to using $some_array[$i].
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
MoonyDragon
Posts: 149
Joined: November 29th, 2017, 5:46 pm

Re: Help on creating an array of stored units.

Post by MoonyDragon »

Pentarctagon is of course correct - I confused index with iterator. (and an integer is something else entirely...) Other than that, my explanation should be mostly correct, I hope. :whistle:
Default L0 Era - Level 1 leaders with level 0 recruits!
User avatar
Legend_Rider
Posts: 35
Joined: June 20th, 2018, 4:51 pm

Re: Help on creating an array of stored units.

Post by Legend_Rider »

@MoonyDragon

How would I go about "simplifying" the transport macro?
Right now it contains a lot of code that filters locations and units and such, if it could somehow be trimmed down to the core "storing function" that would be awesome!
Idk if the transport macro is currently dependent on using the locations / current unit filtering and such to work... (I haven't yet been able to fully wrap my head around it... :P )

I really appreciate you sharing this example, your transport macro has certainly helped me understand WML a lot better. 8)
User avatar
MoonyDragon
Posts: 149
Joined: November 29th, 2017, 5:46 pm

Re: Help on creating an array of stored units.

Post by MoonyDragon »

Legend_Rider wrote: February 10th, 2021, 5:58 pm How would I go about "simplifying" the transport macro?
Right now it contains a lot of code that filters locations and units and such, if it could somehow be trimmed down to the core "storing function" that would be awesome!
Idk if the transport macro is currently dependent on using the locations / current unit filtering and such to work... (I haven't yet been able to fully wrap my head around it... :P )
The simplest way to store a unit as variable in another unit is probably the following:
1. You first store a unit, which converts all of the unit data into wml variables. For example, if the container variable of our ship unit is called vessel, then $vessel.x = x coordinate, $vessel.hitpoints = hitpoints, $vessel.id = id, and so on.
2. Then, you store the passenger unit not into some independent variable, but a variable inside our stored vessel unit. We creatively call it vessel.variables.passengers, because our container variable is called passengers, and it's a variable inside vessel. The append=yes and the plural of passengers is because we might store more than one unit. The kill=yes is self-explanatory.
3. After thus having manipulated our vessel's wml variables, we unstore them back into the battlefield. Because $vessel.x and $vessel.y are still the same, our unit is unstored on top of the old unit, which is promptly deleted.
4. At the end, we clean up the vessel variable.

Code: Select all

[store_unit]
    # {FILTER_VESSEL}
    # or
    # [filter]
    #     # stuff...
    # [/filter]

    variable=vessel
[/store_unit]

[store_unit]
    # {FILTER_PASSENGER}
    # or
    # [filter]
    #     # stuff...
    # [/filter]

    variable=vessel.variables.passengers
    mode=append
    kill=yes
[/store_unit]

[unstore_unit]
    variable=vessel
    find_vacant=no
[/unstore_unit]

[clear_variable]
    name=vessel
[/clear_variable]
To unstore a unit from our vessel onto the map, do the following...
1. Store our vessel unit as the container variable vessel. Same as above.
2. After that, unstore the unit from vessel.variables.passengers to some specified location. If we happen to have more than one stored unit, we can access them as vessel.variables.passengers[$i]. I used $vessel.variables.passengers.length-1 as index, in order to always unstore the latest stored unit.
3. We then clear the container variable of the unit we just unstored, so that we do not duplicate any units.
4. is the same as 3. in the code above
5. is the same as 4. in the code above

Code: Select all

[store_unit]
    # {FILTER_VESSEL}
    # or
    # [filter]
    #     # stuff...
    # [/filter]

    variable=vessel
[/store_unit]

[unstore_unit]
    variable=vessel.variables.passengers[$vessel.variables.passengers.length-1]

    # {FILTER_LOCATIONS} but without [filter]
[/unstore_unit]

[clear_variable]
    name=vessel.variables.passengers[$vessel.variables.passengers.length-1]
[/clear_variable]

[unstore_unit]
    variable=vessel
    find_vacant=no
[/unstore_unit]

[clear_variable]
    name=vessel
[/clear_variable]
Default L0 Era - Level 1 leaders with level 0 recruits!
User avatar
Legend_Rider
Posts: 35
Joined: June 20th, 2018, 4:51 pm

Re: Help on creating an array of stored units.

Post by Legend_Rider »

These answers, examples and explanations have been absolutely brilliant! :D :eng:
Thanks a bundle! :)

But, throughout the progression on this mod, there have been multiple differing methods, processes and examples given.

Wading through a whole bunch of jumbled code, which doesn't all play well together, (due to differing methods used), has left me somewhat confused.

I've attempted to either write up new code using the examples, or patch in various tid bits, to try and get the desired result.
None of my attempts have worked out so far, with errors, blank options, empty menus and units disappearing into the abyss.

Not sure how to go about understanding the code, I have perused the wiki to no avail, because, even with great explanations of specific tags/keys and values, it can't quite explain a chunk of functions/variables and what it's all doing together.

Many thanks, for any light that can be shed on the subject.
Last edited by Legend_Rider on February 13th, 2021, 7:39 am, edited 1 time in total.
User avatar
MoonyDragon
Posts: 149
Joined: November 29th, 2017, 5:46 pm

Re: Help on creating an array of stored units.

Post by MoonyDragon »

I think your main problem is that every part of the code examples was written with a specific purpose in mind, which may differ from yours.

Code frankensteining tends to fail for the same reason, so I'd recommend you to write completely new code, based on what kind of functionality your mod should have.

If you could tell us in detail, what exactly your mod should do and for what purpose, then I'm sure you'd receive more specific help/advice from the forums.

This help might not be finished code, but instead in the form of flowcharts/descriptions/ideas that explain what your code should do and "what it's all doing together".
Default L0 Era - Level 1 leaders with level 0 recruits!
User avatar
Legend_Rider
Posts: 35
Joined: June 20th, 2018, 4:51 pm

Re: Help on creating an array of stored units.

Post by Legend_Rider »

MoonyDragon wrote: February 12th, 2021, 7:32 pm If you could tell us in detail, what exactly your mod should do and for what purpose, then I'm sure you'd receive more specific help/advice from the forums.
Firstly, all of these things have actually been addressed, the only issue being that they don't all work together (due to differing methods)

1. Create a unit that looks like a ship and call it Transport Ship (This has been completed)
__1a. Give the Transport Ship a dummy ability which tells the player that it can carry x number of units. (This has been completed)

2. Create any number of unique arrays which store units that can be accessed later via an option menu.
__2a. The array(s) need to allow you to choose specific units to unstore.
Edit:__2b. Each unique array needs to be associated with the given Transport Ship.
Edit:__2c. Limit the number of units that can be stored in each unique array.
__2d. Add a context menu item to access the unit storing array(s).
__2e. Access the menu item on hexes with units that are adjacent to the Transport Ship, on the same side as the Transport Ship, and have at least 1MP.
__2f. The "menu item" should allow you to either store the unit or Exit the menu.

3. Add context menu item that shows up adjacent to Transport Ships allowing you to access the array menu.
__3a. This menu lets you choose specific units to unstore onto the location that the player has accessed via the right click menu.

I hope this covered all of the details...
User avatar
beetlenaut
Developer
Posts: 2824
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: Help on creating an array of stored units.

Post by beetlenaut »

I haven't read this whole thread, but it looks like you might be starting over anyway, so this could be relevant. If I were doing this, most of it would be done in one LONG event. This is what I would do.

Make a unit_placed event for when a transport ship is added to the map in any way. It needs to do several things:
1) Add the ship to an array of ships.
2) Create a unit array where part of its name is the id of the new ship.
3) Create a "Load" menu item where part of the menu item's id is also the id of the ship. The show_if section checks to see that the user is on a unit that matches the ship's side, this ship is adjacent, the array.length is not too high already, and the unit has MP. The [command] section removes the unit from the map and stores it in the unit array that has a name matching this ship's id.
4) Create a similar "Unload" menu item that checks for an empty hex. The [command] section of this one uses a [message] with [option] for the user to choose the unit by its name and sprite. (You'll need a loop here eventually, but start with just one unit.) The [command] section of each option removes that unit from the array.
(To handle the case where the cursor is next to two ships, the menu item will have to indicate which ship it means in some way.)

There are ways to improve this, and I'm sure I've missed some "gotchas," but that's how I would start. In case it needs to be said, I would write it one little piece at a time.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
Celtic_Minstrel
Developer
Posts: 2195
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Help on creating an array of stored units.

Post by Celtic_Minstrel »

beetlenaut wrote: February 15th, 2021, 9:43 am Create a unit array where part of its name is the id of the new ship.
Why do this when you can store the array in the ship itself as a unit variable?
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply