array not arraying [SOLVED]

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
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: array not arraying

Post by Helmet »

Ravana wrote: November 21st, 2022, 9:58 pm You forgot .variables
Thanks. I had put it in there first, but when it didn't work, I took it back out.

That's the challenge with debugging. While trying to find an error, proper but non-functional code may be viewed with suspicion and removed. Sigh.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
beetlenaut
Developer
Posts: 2813
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: array not arraying

Post by beetlenaut »

Ravana wrote: November 21st, 2022, 9:47 pm Unit will unpetrify next turn" part does not work because you set it as if it is normal variable, while it needs to go inside unit like you do with attacker hits event.
This was the one that took me the longest to notice, too. I actually had to try it on my machine before I saw it. That's the one you are still missing: the variable you set needs to be the one inside the unit.

This is also going to last a turn longer than you want because you are using a new_turn event which happens right before side 1's turn, not side 2's. You need a side turn event of some kind. (I gave two options earlier depending on what you want.)
Helmet wrote: November 21st, 2022, 10:07 pm proper but non-functional code may be viewed with suspicion and removed. Sigh.
This is why changing lines by trial and error almost never works. You should know why ".variables" has to be there. You showed a screenshot of that container in the stored unit yourself. When you change a line, it should be because you say, "Oh, of course! I see what I did wrong there," not, "Heck if I know...I might as well try this next." (If you still don't know why ".variables" has to be there, you should say so.)
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: array not arraying

Post by Helmet »

beetlenaut wrote: November 21st, 2022, 10:26 pm...That's the one you are still missing...
How am I supposed to know whenever I use a variable correctly if nothing works properly?

Learning WML in this manner is like trying to learn cow anatomy from a cow corpse that has been mutilated and may be missing 1 to 3 organs. Sure it's possible to learn something about cows, but everything you think you know might also be wrong.

You indicated that you expect me to think, "Oh, of course! I see what I did wrong there" whenever I arrive at a logical solution. Well, I actually think those thoughts every ten minutes while debugging. The problem is, I'm almost always wrong.

Why am I wrong so often? Probably because I am learning advanced WML off code that doesn't work.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Ravana
Forum Moderator
Posts: 2933
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: array not arraying

Post by Ravana »

What you most need for debugging WML is realizing that you need to test smaller part at once. Some examples

Event does not work
-> test if event is fired at all
-> if filters are involved disable filters and check their condition values with [if] or display them
-> test first few statements of event do what you expect

Loop does not work -> remove loop, run it with first value only.

Conditional does not do what it is supposed to
-> separate testing of condition by displaying used values
-> separate testing of one branch statements (move them outside [if])
-> separate testing of another branch statements
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: array not arraying

Post by Helmet »

Ravana wrote: November 21st, 2022, 11:24 pm What you most need for debugging WML is realizing that you need to test smaller part at once. Some examples

Event does not work
-> test if event is fired at all
-> if filters are involved disable filters and check their condition values with [if] or display them
-> test first few statements of event do what you expect

Loop does not work -> remove loop, run it with first value only.

Conditional does not do what it is supposed to
-> separate testing of condition by displaying used values
-> separate testing of one branch statements (move them outside [if])
-> separate testing of another branch statements
That's good advice. I do those things a lot whenever I write all the WML code that I never ask anybody to help me debug.
I posted a new scenario 2 days ago without asking for help from the forum.

I think I gave you guys the impression I don't know anything. If you look through my contributions to the add-on server, I have some complex things in there. But I have only used one foreach loop and I've never been proficient with the variety of variables, so I'm adding to my knowledge.

If you want to be extra-helpful, show me the correct code and I'll study it, and if I have questions, I'll ask.

Or I can keep annoying beetlenaut with my mistakes.

But I'd prefer studying the correct code. :)

Here is the latest non-functional version...

Code: Select all

	[event] #                                                       T H A W I N G     E V E N T
		name=new turn
        first_time_only=no
        [store_unit]
            [filter]
                side=2  # This will store all of the side 2 units in an array.
                status=petrified
            [/filter]
            variable=target_units  # "unit" as a variable name is reserved for the unit that triggers an event, so try not to use it for other things.  It could get confusing.
        [/store_unit]
        [foreach]
            array=target_units
            [do]
                # [inspect][/inspect]
                [if]
                    [variable]
                        name=this_item.variables.how_frozen #              how frozen is the unit on a scale of 0-2       0=not frozen    1=thawing     2=frozen
                        greater_than=0
                    [/variable]
                    [then]
                        [modify_unit]
                            [filter]
                                id=$this_item  # 		an id stores a specific unit in an array.   
                            [/filter]
                            [set_variable]
                                name=variables.how_frozen #              how frozen is the unit on a scale of 0-2       0=not frozen    1=thawing     2=frozen
                                sub=1 #                               unit was frozen, but is now thawing. Unit will unpetrify next turn.
                            [/set_variable]
                        [/modify_unit]
                        #  [store_unit]
                        #      [filter]
                        #          id=$this_item.id
                        #      [/filter]
                        #      variable=this_item
                        #      id=$second_unit.id
                        #  [/store_unit]
                    [/then]
                    [else]
                        [unpetrify] #                               any unit that is not frozen or thawing will be unpetrified
                            id=$this_item.id
                        [/unpetrify]
                    [/else]
                [/if]
            [/do]
        [/foreach]
    [/event]

[event]
    name=attacker hits
    first_time_only=no
    [filter_attack]
        special=freezes
    [/filter_attack]

    [modify_unit]
        [filter]
            id=$second_unit.id  # 		an id stores a specific unit in an array.   
        [/filter]
        [set_variable]
            name=how_frozen #              how frozen is the unit on a scale of 0-2       0=not frozen    1=thawing     2=frozen
            value=2
        [/set_variable]
    [/modify_unit]

    # $second_unit is just a copy of the unit, not the actual unit itself. However, when you use [modify_unit] you update the actual unit, 
    # not the copy, so the copy is now out-of-date. If you want to keep $second_unit accurate you'll need to re-store the unit after modifying it:
    [store_unit]
        [filter]
            id=$second_unit.id
        [/filter]
        variable=second_unit
        id=$second_unit.id
    [/store_unit]

    [message]
        speaker=MyLeader
        message="Modify unit just changed a variable to: " + $second_unit.variables.how_frozen + "."
    [/message]
[/event]
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Celtic_Minstrel
Developer
Posts: 2158
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: array not arraying

Post by Celtic_Minstrel »

Helmet wrote: November 21st, 2022, 5:00 pm With all due respect, I tried what beetlenaut said and it didn't work, and it didn't work when you repeated it.
Why not show what you tried so we can see what you did wrong?
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Ravana
Forum Moderator
Posts: 2933
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: array not arraying

Post by Ravana »

> If you want to be extra-helpful, show me the correct code and I'll study it, and if I have questions, I'll ask.
So that you can copy-paste it without understanding why it works.

You are making good progress here. I see visible mistakes in [modify_unit]. In this case it makes sense to separately test filter, and once you know filter is correct, the modification. For filter testing you can use store_unit+inspect.
User avatar
beetlenaut
Developer
Posts: 2813
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: array not arraying

Post by beetlenaut »

You figured out that you needed [modify_unit] in your [foreach] loop. Good job.
Helmet wrote: November 21st, 2022, 11:40 pm show me the correct code and I'll study it
I am very skeptical of this claim. Two of us already posted working code that worked in a similar way, and you wrote something that works in a completely different way. That's not bad, it just doesn't make me confident that you studied it. You seem not to have even studied your own code: You know that you have correct, working code in the attacker_hits event that modifies a unit variable. When you needed to do the same job in the loop, did you study the working code first? I don't think so because the two mistakes left there are just careless.
Helmet wrote: November 21st, 2022, 11:40 pm I think I gave you guys the impression I don't know anything. If you look through my contributions to the add-on server, I have some complex things in there.
On the contrary! I played one of your campaigns, and found it entertaining. That's why I actually do think you can do this without us writing every line of code for you.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: array not arraying

Post by Helmet »

beetlenaut wrote: November 22nd, 2022, 12:31 amTwo of us already posted working code that worked in a similar way, and you wrote something that works in a completely different way. That's not bad, it just doesn't make me confident that you studied it.
With all due respect, the "working code" you shared a few times was incomplete, and therefore would never have made my events work anyway, and the scant explanations you provided for using your code fragments were rendered almost meaningless by jargon. If I have to guess at what I'm supposed to do with your replies (which annoys you, as you've made clear time and time again), I am not entirely to blame.

Your heart seems to be in the right place. You want me to learn WML, so you won't simply tell me the solutions to my WML problems; I have to work for it. I get that. But I didn't ask to be taught WML. I feel like I'm back in school and you and I are taking a test, and you have all the answers on your worksheet but you won't let me see it. :lol:

By the way, I find it somewhat insulting whenever you accuse me of not trying to comprehend your replies.

Oh well. The forum isn't as active as it used to be, and at least you're here, trying to help people. So thanks. I truly appreciate your effort to help me. I just wish my code worked.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: array not arraying

Post by Helmet »

Celtic_Minstrel wrote: November 22nd, 2022, 12:23 am Why not show what you tried so we can see what you did wrong?
Thanks for asking. This is the latest version that doesn't work.

Code: Select all

	[event] #                                                       T H A W I N G     E V E N T
		name=new turn
        first_time_only=no
        [store_unit]
            [filter]
                side=2  # This will store all of the side 2 units in an array.
                status=petrified
            [/filter]
            variable=target_units  # "unit" as a variable name is reserved for the unit that triggers an event, so try not to use it for other things.  It could get confusing.
        [/store_unit]
        [foreach]
            array=target_units
            [do]
                # [inspect][/inspect]
                [if]
                    [variable]
                        name=this_item.variables.how_frozen #              how frozen is the unit on a scale of 0-2       0=not frozen    1=thawing     2=frozen
                        greater_than=0
                    [/variable]
                    [then]
                        [modify_unit]
                            [filter]
                                id=$this_item  # 		an id stores a specific unit in an array.   
                            [/filter]
                            [set_variable]
                                name=variables.how_frozen #              how frozen is the unit on a scale of 0-2       0=not frozen    1=thawing     2=frozen
                                sub=1 #                               unit was frozen, but is now thawing. Unit will unpetrify next turn.
                            [/set_variable]
                        [/modify_unit]
                        #  [store_unit]
                        #      [filter]
                        #          id=$this_item.id
                        #      [/filter]
                        #      variable=this_item
                        #      id=$second_unit.id
                        #  [/store_unit]
                    [/then]
                    [else]
                        [unpetrify] #                               any unit that is not frozen or thawing will be unpetrified
                            id=$this_item.id
                        [/unpetrify]
                    [/else]
                [/if]
            [/do]
        [/foreach]
    [/event]

[event]
    name=attacker hits
    first_time_only=no
    [filter_attack]
        special=freezes
    [/filter_attack]

    [modify_unit]
        [filter]
            id=$second_unit.id  # 		an id stores a specific unit in an array.   
        [/filter]
        [set_variable]
            name=how_frozen #              how frozen is the unit on a scale of 0-2       0=not frozen    1=thawing     2=frozen
            value=2
        [/set_variable]
    [/modify_unit]

    # $second_unit is just a copy of the unit, not the actual unit itself. However, when you use [modify_unit] you update the actual unit, 
    # not the copy, so the copy is now out-of-date. If you want to keep $second_unit accurate you'll need to re-store the unit after modifying it:
    [store_unit]
        [filter]
            id=$second_unit.id
        [/filter]
        variable=second_unit
        id=$second_unit.id
    [/store_unit]

    [message]
        speaker=MyLeader
        message="Modify unit just changed a variable to: " + $second_unit.variables.how_frozen + "."
    [/message]
[/event]
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
beetlenaut
Developer
Posts: 2813
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: array not arraying

Post by beetlenaut »

Helmet wrote: November 22nd, 2022, 1:46 amI feel like I'm back in school and you and I are taking a test, and you have all the answers on your worksheet but you won't let me see it.
And you would deserve to copy my worksheet because...? I am a teacher in real life, so this analogy is not motivating me the way you want it to! :lol:

I think you have learned some more WML, but fine. I don't want you to go away angry, so add .id, and remove variables.:

Code: Select all

[modify_unit]
    [filter]
        id=$this_item.id    # Here
    [/filter]
    [set_variable]
        name=how_frozen    # And here
        sub=1
    [/set_variable]
[/modify_unit]
That will make it look like the working code below it, and I think that's all you need to do for this to work. You still need to change it to a side turn event for it to take the right number of turns like I previously explained, but how to do that exactly depends on your goals, as I also explained.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: array not arraying

Post by Helmet »

I suspected you were a teacher! Your approach reminded me of a BCBA I once knew. Oh boy, I respected her immensely...but I also pitied her students. :lol:
beetlenaut wrote: November 22nd, 2022, 2:06 am I think you have learned some more WML, but fine. I don't want you to go away angry, so ...
Thanks for the code!

Alas, when I play-tested it, the Giant Rat never unpetrifies. But it's nice to know that at least this modify_unit code is correct, so I don't change it while trying to find the other problem.
beetlenaut wrote: November 22nd, 2022, 2:06 am ...You still need to change it to a side turn event for it to take the right number of turns like I previously explained, but how to do that exactly depends on your goals, as I also explained.
I thought I would get the Giant Rat to unpetrify first, and then I would work on the side turn event. I agree with you that it should probably unpetrify at the beginning of the turn for side 2, that way it can run away from the Frostbite.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Ravana
Forum Moderator
Posts: 2933
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: array not arraying

Post by Ravana »

> Alas, when I play-tested it, the Giant Rat never unpetrifies.
Debug it then. What happens with each turn, how how_frozen value behaves.
User avatar
beetlenaut
Developer
Posts: 2813
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: array not arraying

Post by beetlenaut »

Helmet wrote: November 22nd, 2022, 2:45 pm Alas, when I play-tested it, the Giant Rat never unpetrifies.
I must not have the same code that you do anymore, because I tried it and it works on my computer. Post what you have now.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: array not arraying

Post by Helmet »

beetlenaut wrote: November 22nd, 2022, 3:36 pm I must not have the same code that you do anymore, because I tried it and it works on my computer. Post what you have now.
Whoa! The rat unpetrified!

I forgot to click "save" after I had added your code, so...it wasn't used in the play-test. :oops:

Thanks again!
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
Post Reply