FOREACH code help!!!!! Cant get it right...

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
zengetsu88
Posts: 72
Joined: March 29th, 2010, 7:25 pm
Location: This Universe

FOREACH code help!!!!! Cant get it right...

Post by zengetsu88 »

Here is some foreach code that I am using... the problem are is the "if" section...

Code: Select all

    {FOREACH units i}
        {VARIABLE_OP units[$i].hitpoints add -10}
        [if]
            units[$i].hitpoints=<1
                [then]
                    kill=yes
                    unit.experience=+4
                [/then]
                [else]
                    kill=no
                    unit.experience=+1
                    units[$i].experience=+1
                [/else]
        [/if]

        [unstore_unit]
            variable=units[$i]
            find_vacant=no
            text= _ "10"
            {COLOR_HARM}
        [/unstore_unit]
    {NEXT i}
   
    {CLEAR_VARIABLE units}
I want it so that if unit[$i] hitpoints are less than one they die and the unit that this code is on gets 4 experience for that kill... else if unit[$i] hitpoints are not below 1... then unit[$i] and the unit this code is on gets 1 experience... make sense?

if there is anything unclear just say so... (i know I suck at explaining)

thanks for any help!!!
my logic is sound
User avatar
Ken_Oh
Moderator Emeritus
Posts: 2178
Joined: February 6th, 2006, 4:03 am
Location: Baltimore, Maryland, USA

Re: FOREACH code help!!!!! Cant get it right...

Post by Ken_Oh »

You're just making up your own WML rules. Don't do that. I feel like saving this one for the scrapbook...

I'm feeling generous, so here's what it should look like. I think that will work, however you should double check the usage of [kill]: http://wiki.wesnoth.org/ReferenceWML

Also, use that link to write your code next time.

You're also going to need to unstore unit, in case you weren't aware of that.

Code: Select all

[if]
	[variable]
		name=units[$i].hitpoints
		less_than_equal_to=1
	[/variable]
	[then]
		[kill]
			x,y=$units[$i].x,$units[$i].y
		[/kill]		
		{VARIABLE_OP unit.experience add 4}
	[/then]
	[else]
		{VARIABLE_OP unit.experience add 1}
		{VARIABLE_OP units[$i].experience add 1}
	[/else]
[/if]
User avatar
zengetsu88
Posts: 72
Joined: March 29th, 2010, 7:25 pm
Location: This Universe

Re: FOREACH code help!!!!! Cant get it right...

Post by zengetsu88 »

THank you for the help... but let me post my full code for you... because I have already defined units[$i]... why do I have to define it again? (legitimate question... not trying to be a smart butt)...

and with the reference wml thing... I have been researching for a month to figure out this code.... literally...

and I already have an unstore unit... so why do I need another one?
(another legitimate question...)

sry i'm really new to events... and wml... thank you for your help!

Code: Select all

[event]
    name=attacker_hits
    first_time_only=no
   
    [filter_attack]
        name=grid break
    [/filter_attack]
   
    [store_unit]
        [filter]
            [filter_adjacent]
                x,y=$x1,$y1
                radius=2
                is_enemy=yes
            [/filter_adjacent]
        [/filter]
       
        variable=units
    [/store_unit]
   
    [store_unit]
        [filter]
            x,y=$x1,$y1
        [/filter]
       
        variable=self
    [/store_unit]

    {FOREACH units i}
        {VARIABLE_OP units[$i].hitpoints add -10}
        [unstore_unit]
            [if]
                [variable]
                    name=units[$i].hitpoints
                    less_than=1
                [/variable]
                [then]
                    [kill]
                        variable=units[$i]
                    [/kill]      
                    {VARIABLE_OP self.experience add 4}
                [/then]
                [else]
                    {VARIABLE_OP self.experience add 1}
                    {VARIABLE_OP units[$i].experience add 1}
                [/else]
            [/if]
        [/unstore_unit]

        [unstore_unit]
            variable=units[$i]
            find_vacant=no
            text= _ "10"
            {COLOR_HARM}
        [/unstore_unit]
    {NEXT i}
   
    {CLEAR_VARIABLE units}
    {CLEAR_VARIABLE self}
[/event]
Last edited by zengetsu88 on April 6th, 2010, 6:51 pm, edited 1 time in total.
my logic is sound
User avatar
Ken_Oh
Moderator Emeritus
Posts: 2178
Joined: February 6th, 2006, 4:03 am
Location: Baltimore, Maryland, USA

Re: FOREACH code help!!!!! Cant get it right...

Post by Ken_Oh »

You're unstoring "units" but you need to unstore "self" as well, or else anything you do to the self variable won't stay. I would suggest putting it after {NEXT i}.

less_than_ -> less_than

The ReferenceWML wiki says [kill] takes Standard Unit Filter. "variable=" isn't listed as a valid Standard Unit Filter.
User avatar
zengetsu88
Posts: 72
Joined: March 29th, 2010, 7:25 pm
Location: This Universe

Re: FOREACH code help!!!!! Cant get it right...

Post by zengetsu88 »

So is this what you mean?
Spoiler:
or should I do this...
Spoiler:
once again thank you for your help... I really appreciate it...
my logic is sound
User avatar
Ken_Oh
Moderator Emeritus
Posts: 2178
Joined: February 6th, 2006, 4:03 am
Location: Baltimore, Maryland, USA

Re: FOREACH code help!!!!! Cant get it right...

Post by Ken_Oh »

#2 is what you're looking for.

Glad to help.
User avatar
zengetsu88
Posts: 72
Joined: March 29th, 2010, 7:25 pm
Location: This Universe

Re: FOREACH code help!!!!! Cant get it right...

Post by zengetsu88 »

For some reason they are not dying... this code is for attacking all the units surrounding the attacking unit... the problem right now is the fact that the units are still not dying... what can I do to change this code so that they will actually die instead of having their hitpoints go into negative numbers...

I tried

Code: Select all

                [then]
                    [kill]
                        [variable]
                            name=units[$i]
                        [/variable]
                    [/kill]     
                    {VARIABLE_OP self.experience add 4}
                [/then]
but it didn't work... It actually killed my leader every time... which is weird...
my logic is sound
User avatar
Ken_Oh
Moderator Emeritus
Posts: 2178
Joined: February 6th, 2006, 4:03 am
Location: Baltimore, Maryland, USA

Re: FOREACH code help!!!!! Cant get it right...

Post by Ken_Oh »

No, you don't want to do that.

Sorry I didn't catch this before. You want to unstore units[$i] in the [else]. Otherwise, you're killing the unit but then unstoring it's alive instance.
User avatar
zengetsu88
Posts: 72
Joined: March 29th, 2010, 7:25 pm
Location: This Universe

Re: FOREACH code help!!!!! Cant get it right...

Post by zengetsu88 »

YAYYY!!!! it works... I can't believe I didn't think about that.

Thank you so much for your help! This code (if you haven't figured it out... which I doubt you haven't) is for attacking units surrounding the unit that has this code on it...

I was making this one as a prototype and adapt it to what I was wanting to do later on...

This is the full prototype code finished...
Spoiler:

Once again thank you for your help so far... I wish I was as literate with WML as you are but I guess that will come with experience.

Few last minute questions if you are up for answering them (I have been doing research but can't figure a few of them out)
#1 is it possible to put a chance to hit inside this code so that each unit that is surrounding and being attacked has a different chance to hit then the original unit being attacked?
#2 can I go out further than 1 hex tier from my base unit... I tried playing with the radius but that didn't work... all I could think was filtering and storing any unit that is adjacent to the enemies already being store...
#3 Can I make the damage that is being done to each unit vary (in other words make the minus 10 a range of 5 to 15 or something like that)

yada yada yada... I have a thousand questions but I don't want to bug you with them... Just watch for my threads...
I am currently working on creating a new multiplayer race and I just wanted to actually get into the WML instead of just frankensteining and copying what has already been done in the game...

If you have any suggestions for this code or how I should adapt it I am all ears...

Thanks again!
my logic is sound
User avatar
Ken_Oh
Moderator Emeritus
Posts: 2178
Joined: February 6th, 2006, 4:03 am
Location: Baltimore, Maryland, USA

Re: FOREACH code help!!!!! Cant get it right...

Post by Ken_Oh »

You can create a random number with {VARIABLE_OP variable rand 1...100} or {RANDOM 1..100} will put the random number into $random. From there, you can use your if/then statements to vary the damage and the chance to hit.

So, I looked up [filter_adjacent] and the radius you have in there isn't doing anything, as it is not an accepted key. [filter_adjacent] only takes the units adjacent to the primary unit. You'll want to use [filter_location].
User avatar
zengetsu88
Posts: 72
Joined: March 29th, 2010, 7:25 pm
Location: This Universe

Re: FOREACH code help!!!!! Cant get it right...

Post by zengetsu88 »

I was able to get the damage out further by using filter_location... but since Radius is counted last... I cant figure out how to get it discern from friend or foe.... the attack hits EVERYTHING in the given radius and not just enemies... I have tried so many sub tags but I think that is_enemy can only show up in the filter_adjacent tag.... how do I solve this problem?
my logic is sound
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: FOREACH code help!!!!! Cant get it right...

Post by Sapient »

The is_enemy check only works for adjacent units; if someone is several hexes away from you, they are certainly not adjacent.

In this situation, you could use another trick:
1) store your unit's team_name in a variable first
2) check in the unit filter for a team_name that is [not] equal to your own.

(units with different team_names are, by definition, enemies)
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
zengetsu88
Posts: 72
Joined: March 29th, 2010, 7:25 pm
Location: This Universe

Re: FOREACH code help!!!!! Cant get it right...

Post by zengetsu88 »

I have thought about doing that but...

#1 I'm not sure how to code that...
#2 does storing units in order in your code effect anything (does the storing team name have to be before or after storing enemies)
#3 will this storing team name even be on the unit or in the same code as the event attack?
my logic is sound
User avatar
Luke the Flaming
Posts: 215
Joined: October 18th, 2006, 6:25 pm

Re: FOREACH code help!!!!! Cant get it right...

Post by Luke the Flaming »

My attempt... (untested)

Code: Select all

[event]
    name=attack
    first_time_only=no
   
    [filter_attack]
        name=grid break
    [/filter_attack]

    [store_unit]
        [filter]
            x,y=$x1,$y1
        [/filter]
        variable=self
    [/store_unit]

    [store_side]
         side=$self.side
         variable=my_side
     [/store_side]

    [store_unit]
        [filter]
            [filter_location]
                x,y=$x1,$y1
                radius=3   # Or whatever is the radius of the special attack.
            [/filter_location]
        [/filter]
        variable=potential_target
    [/store_unit]

    {FOREACH potential_target i}
        [store_side]
            side=$potential_target[$i].side
            variable=his_side
        [/store_side]
        [if]
            [variable]
                name=his_side.team_name
                not_equals_to=$my_side.team_name
            [/variable]
        [then]
            # The stuff about damage goes here 
            # (roll a die, compare defense, apply damage or kill, etc.).
        [/then]
        [/if]
    {NEXT i}

    {CLEAR_VARIABLE self}
    {CLEAR_VARIABLE potential_target}
    {CLEAR_VARIABLE my_side}
    {CLEAR_VARIABLE his_side}
[/event]
O, Wind, if Winter comes, can Spring be far behind?
Post Reply