ghype's Problems:

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
ghype
Posts: 1069
Joined: December 13th, 2016, 4:43 pm
Location: Berlin, Germany

Re: ghype's Problems: {DROP_GOLD_ON_KILL}

Post by ghype »

So as announced, I played further around with the ability and tried to expand it. I wanted to add another variable. Not only should it be a weighted percentage how much gold is dropped, but whether or not gold drops at all should be also decided by a variable. For that I defined this.

Code: Select all

#define DROP_GOLD_ON_KILL
    [event]
        name=last breath
        first_time_only=no
        [filter]
                side=2
        [/filter]

        [if]
            [variable]
                name=$second_unit.level
                less_than_equal_to=1
            [/variable]    
            [then]
                {VARIABLE_OP coins_prob rand (0,0,0,1)}

                [if]
                    [variable]
                        name=coins_prob
                        equals=1
                    [/variable]  

                    [then]
                        {DROP_GOLD_ON_KILL_EVENT}
                    [/then]
                [/if]
            [/then]
        [/if]

        [if]
            [variable]
                name=$second_unit.level
                equals=2
            [/variable]    
            [then]
                {VARIABLE_OP coins_prob rand (0,0,1)}

                [if]
                    [variable]
                        name=coins_prob
                        equals=1
                    [/variable]  

                    [then]
                        {DROP_GOLD_ON_KILL_EVENT}
                    [/then]
                [/if]
            [/then]
        [/if]

        [if]
            [variable]
                name=$second_unit.level
                greater_than_equal_to=3
            [/variable]    
            [then]
                {VARIABLE_OP coins_prob rand (0,1)}

                [if]
                    [variable]
                        name=coins_prob
                        equals=1
                    [/variable]  

                    [then]
                        {DROP_GOLD_ON_KILL_EVENT}
                    [/then]
                [/if]
            [/then]
        [/if]
        {CLEAR_VARIABLE coins_prob}

    [/event]
Here is also the ability which WhiteWolf contributed to. The only things I changed is:
  • the name - DROP_GOLD_ON_KILL is now calledDROP_GOLD_ON_KILL_EVENT
  • name is now last breathinstead ofdie so the new one triggers before this one
  • added the missing | in all the $coins

Code: Select all

#define DROP_GOLD_ON_KILL_EVENT
    [event]
        name=die
        first_time_only=no
        [filter]
            side=2
            # note: $x1,$y1 doesn't do anything here
        [/filter]
        
        [item]
            image=items/gold-coins-small.png
            x,y=$x1,$y1
        [/item]

        [event]
            name=moveto
            delayed_variable_substitution=no # has to be explicitly set to no to substitute value of x y at creation time (not trigger time!)
            [filter]
                x,y=$x1,$y1
            [/filter]
            
            {VARIABLE_OP coins rand (1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,6)}
            
            [gold]
                side=$|unit.side # the | is important after the $ sign, because this is substituted in trigger time.
                amount=$|coins
            [/gold]
            [floating_text] # using floating_text instead of an unstore_unit might be a better idea
                x,y=$x1,$y1
                text=_ "<span color='yellow'>+ $|coins Gold</span>" # it does require pango to get a red+green text though.
            [/floating_text]
            [sound]
                name=gold.ogg
            [/sound]
            
            {CLEAR_VARIABLE coins}

            [remove_item]
                x=$x1
                y=$y1
            [/remove_item]
        [/event]
    [/event]    

#enddef
as simple as this one seems, It still drops gold on every kill (tried it multiple times to exclude the probability that it still drops every time against the odds). I suppose something is wrong with my conditional structure.

Should the multiple [if]-tags I have be defined in each other and [elseif] used?
Or is just becoming "too nested" ?
User avatar
Ravana
Forum Moderator
Posts: 3002
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: ghype's Problems: {DROP_GOLD_ON_KILL}

Post by Ravana »

I suspect number-only variable names are not supported.

name=$second_unit.level
User avatar
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Re: ghype's Problems: {DROP_GOLD_ON_KILL}

Post by WhiteWolf »

I sense a bit of confusion about what's a nested event and what's a fired event.
Because {DROP_GOLD_ON_KILL_EVENT} cannot be called like that. In this context it nests the events at that point, but I think you mean to fire it instead.

How to explain while staying concise... A nested event, which this is, gets created at the point when the code gets there, but is not executed there! To execute another event at one point, you need a [fire_event] call. You can think of this as a proper function call, it's somewhat analogous.

This:

Code: Select all

[event]
    name=turn 10
    [event] # this event is created here. It will fire the next time a unit moves somewhere, but not immediately.
        name=moveto
        # stuff
    [/event]
    
    # here at this point "stuff" is NOT EXECUTED YET
[/event]
Is a nested event.
and is (in functionality) equivalent to this:
This:

Code: Select all

[event]
    name=turn 10
    [fire_event]
        id=trigger
    [/fire_event]
    # by this point here, "stuff" is ALREADY EXECUTED!
[/event]

[event]
    name=COULD BE ANYTHING
    id=trigger
    
    #stuff
[/event]
is a fired event. The turn 10 event immediately fires and executes the #stuff in trigger, so it's almost like a function call.

In the coin dropping, I used a nested event, because after the unit dies, I wanted to create an event that will fire later, when a unit moves there. That's what it's good for.

So the variables and changes should be implemented in the first outer event. But this event should be [scenario]-level, it shouldn't be nested into yet another layer because that won't work.
To add your conditions is just something like this:

Code: Select all

#define DROP_GOLD_ON_KILL
    [event]
        name=die
        first_time_only=no
        [filter]
            side=2
        [/filter]
        
        # CONDITIONS added here:
        [switch] # switch may be better here than if-elseif-elseif... chains, but it's just aesthetics.
            variable=second_unit.level
            [case]
                value=1
                 {VARIABLE_OP coins_prob rand (0,0,0,1)}
            [/case]
            [case]
                value=2
                 {VARIABLE_OP coins_prob rand (0,0,1)}
             [/case]
             [else]
                 {VARIABLE_OP coins_prob rand (0,1)}
             [/else]
         [/switch]
                
        [if]
            [variable]
                name=coins_prob
                equals=0
            [/variable]  
            [then]
                [return] # immediately exit this event, nothing below is executed - no coin is dropped.
                [/return]
            [/then]
        [/if]
                                
        # if we got to this point, we know that couns_prob must be 1.                        
        # So just carry on with the coin dropping, which is the code from before.
        [item]
            image=items/gold-coins-small.png
            x,y=$x1,$y1
        [/item]

        [event]
            name=moveto
            delayed_variable_substitution=no # has to be explicitly set to no to substitute value of x y at creation time (not trigger time!)
            [filter]
                x,y=$x1,$y1
            [/filter]
            
            {VARIABLE_OP coins rand (1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,6)}
            
            [gold]
                side=$|unit.side # the | is important after the $ sign, because this is substituted in trigger time.
                amount=$|coins
            [/gold]
            [floating_text] # using floating_text instead of an unstore_unit might be a better idea
                x,y=$x1,$y1
                text=_ "<span color='yellow'>+ $|coins Gold</span>" # it does require pango to get a red+green text though.
            [/floating_text]
            [sound]
                name=gold.ogg
            [/sound]
            
            {CLEAR_VARIABLE coins}

            [remove_item]
                x=$x1
                y=$y1
            [/remove_item]
        [/event]
    [/event]    

#enddef

I didn't make it concise did I. Oh well.
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
ghype
Posts: 1069
Joined: December 13th, 2016, 4:43 pm
Location: Berlin, Germany

Re: ghype's Problems: {DROP_GOLD_ON_KILL}

Post by ghype »

WhiteWolf wrote: December 2nd, 2020, 10:39 pm
Hey man, I just wanted to tell you again, thank you for your support helping me through this (WML). You last version of code works! I am working on something big (but not overwhelming) this was a keypart for that.
Post Reply