Storing turn number in an array, modifying it and using it as condition

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
PapaSmurfReloaded
Posts: 819
Joined: November 17th, 2007, 1:10 pm
Location: Argentina

Storing turn number in an array, modifying it and using it as condition

Post by PapaSmurfReloaded »

I am in need of some help. :hmm:

In my Bloody Mod add-on, I wanted to make it so that blood stains and corpses dissapears x turns (decay time) after they are created (lets say 3 turns.)

Such action requires me to store the location of the remains when they are created, I have managed to get it working this far.

I guess I have to create another array which stores the turn when each stain is created, add 3 to its value, then put that value as the condition for erasing of that individual stain.

However, for some reason thinking about how to code the second part confuses me. :doh:

Here is a short version of the code I am currently using.

Code: Select all

[event]
name=last breath
first_time_only=no
[filter]
type=Skeleton,Skeleton Archer,Revenant,Deathblade,Bone Shooter,Death Knight,Draug,Banebow,Chocobone,Lich,Ancient Lich
[/filter]
{PLACE_IMAGE items/bones.png $x1 $y1}
[store_locations]
variable=bm_bones_loc
mode=append
x,y=$x1,$y1
[/store_locations]
[/event]

Code: Select all

[event]
name=turn end
first_time_only=no

{FOREACH bm_bones_loc i}
[remove_item]
x,y=$bm_bones_loc[$i].x,$bm_bones_loc[$i].y
image=items/bones.png
[/remove_item]
{NEXT i}
{CLEAR_VARIABLE i}
[redraw]
[/redraw]

[/event]
Last edited by Pentarctagon on October 28th, 2018, 3:14 am, edited 1 time in total.
Reason: [c] -> [code]
User avatar
Pentarctagon
Project Manager
Posts: 5496
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Storing turn number in an array, modifying it and using it as condition

Post by Pentarctagon »

Not really related to your question, but as an fyi, NEXT already clears the index variable, so you don't need to be doing that yourself.

As for your actual question, you don't need to use a separate array - you could do something like this:
{VARIABLE bm_bones_loc["$($bm_bones_loc.length-1)"].turn "$($turn_number+3)"}

Or alternatively, if all you need is the x and y values from the location:

Code: Select all

[set_variables]
name=bm_bones_loc
mode=append
[value]
x=$x1
y=$y1
turn="$($turn_number+3)"
[/value]
[/set_variables]
Then in your second code block, check if bm_bones_loc[$i].turn equals $turn_number, and then use [remove_item] if it is.
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
skeptical_troll
Posts: 498
Joined: August 31st, 2015, 11:06 pm

Re: Storing turn number in an array, modifying it and using it as condition

Post by skeptical_troll »

It looks like a nested event would work better in this case. Inside your first event, create the event name =turn $($turn_number +3) where you remove the image. Make sure you set delayed_variable_substitition = no to get the right coordinates
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: Storing turn number in an array, modifying it and using it as condition

Post by enclave »

y i think i agree with troll, not saying it's better, but i think it's much more simple..

Code: Select all

[event]
name=last breath
first_time_only=no
[filter]
type=Skeleton,Skeleton Archer,Revenant,Deathblade,Bone Shooter,Death Knight,Draug,Banebow,Chocobone,Lich,Ancient Lich
[/filter]
{PLACE_IMAGE items/bones.png $x1 $y1}
{VARIABLE temp_turn_number $turn_number}
{VARIABLE_OP temp_turn_number add 3}
[event] 
name=turn $temp_turn_number
first_time_only=yes
delayed_variable_substitition=no ## if it doesn't work this way then try to remove it.. I'm not sure if it's 100% needed, but it's highly likely 
### (otherwise $temp_turn_number MAY not be converted into anything.. it will stay with $ sign and never work as intended.
## other variables may also have hard times to be converted.. when it's event inside event.. magic may start happening.. may not;) depends on situation.
[remove_item]
x,y=$x1,$y1 ### if it doesnt work try to use $|x1 and $|y1 instead.. event inside event may cause differences..
### if you dont put delay variable substitution $x1 $y1 would probably work as it is, but event itself probably wouldnt... need testing. not 100% sure.
image=items/bones.png
[/remove_item]
[/event]
{CLEAR_VARIABLE temp_turn_number} ## idk if this needed or not.. don't think it will break anything with event inside event if you clear it.
[/event]
i wonder what will happen if 3 units die on same hex, one at turn 1, other at turn 2 and other on turn 3, would remove_item clear all 3 images or just one of them.. ? ;)
User avatar
PapaSmurfReloaded
Posts: 819
Joined: November 17th, 2007, 1:10 pm
Location: Argentina

Re: Storing turn number in an array, modifying it and using it as condition

Post by PapaSmurfReloaded »

enclave wrote: October 28th, 2018, 1:45 pm y i think i agree with troll, not saying it's better, but i think it's much more simple..

Code: Select all

[event]
name=last breath
first_time_only=no
[filter]
type=Skeleton,Skeleton Archer,Revenant,Deathblade,Bone Shooter,Death Knight,Draug,Banebow,Chocobone,Lich,Ancient Lich
[/filter]
{PLACE_IMAGE items/bones.png $x1 $y1}
{VARIABLE temp_turn_number $turn_number}
{VARIABLE_OP temp_turn_number add 3}
[event] 
name=turn $temp_turn_number
first_time_only=yes
delayed_variable_substitition=no ## if it doesn't work this way then try to remove it.. I'm not sure if it's 100% needed, but it's highly likely 
### (otherwise $temp_turn_number MAY not be converted into anything.. it will stay with $ sign and never work as intended.
## other variables may also have hard times to be converted.. when it's event inside event.. magic may start happening.. may not;) depends on situation.
[remove_item]
x,y=$x1,$y1 ### if it doesnt work try to use $|x1 and $|y1 instead.. event inside event may cause differences..
### if you dont put delay variable substitution $x1 $y1 would probably work as it is, but event itself probably wouldnt... need testing. not 100% sure.
image=items/bones.png
[/remove_item]
[/event]
{CLEAR_VARIABLE temp_turn_number} ## idk if this needed or not.. don't think it will break anything with event inside event if you clear it.
[/event]
i wonder what will happen if 3 units die on same hex, one at turn 1, other at turn 2 and other on turn 3, would remove_item clear all 3 images or just one of them.. ? ;)
Well I guess all of the images with the same name and path would be deleted, I thought about that too but it would just be a minor issue.

EDIt: I tried to do what troll and enclave suggest, and for some reason these nested events don't work. :augh:
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: Storing turn number in an array, modifying it and using it as condition

Post by enclave »

y if u wanted to create it to perfection you could probably then use it in some kind of arrays and recreate the needed amount.. but it's probbaly overcomplicating things.. plus im not sure if all 3 will be removed to be honest;)

Ok, then try to put [chat] message="x1 $x1 y1 $y1 x1 $|x1 y1 $|y1 into last breath and turn number event" (change turn number into turn end, so it will display message straight away maybe.. without waiting 3 turns..) we will see if variables are nil or have anything there..
( side X turn Y end ) the event name

I dont even know if last breath will have $x1, $y1 or it will be $unit.x $unit.y instead... so it just need to be tested with chat message to make sure.
Last edited by enclave on October 28th, 2018, 2:38 pm, edited 1 time in total.
User avatar
skeptical_troll
Posts: 498
Joined: August 31st, 2015, 11:06 pm

Re: Storing turn number in an array, modifying it and using it as condition

Post by skeptical_troll »

I remember having some headache with nested events of this kind. I think the syntax requires you to put quotes, i.e. name="turn $(3 + $turn_number)" and you need the delayed substitution set to no.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: Storing turn number in an array, modifying it and using it as condition

Post by enclave »

PapaSmurfReloaded wrote: October 28th, 2018, 3:19 pm I give up for today :annoyed:
Thats below is tested and 100% working (at least on Skeleton)

Code: Select all

[event]
name=last breath
first_time_only=no
[filter]
type=Skeleton,Skeleton Archer,Revenant,Deathblade,Bone Shooter,Death Knight,Draug,Banebow,Chocobone,Lich,Ancient Lich
[/filter]
{PLACE_IMAGE items/bones.png $x1 $y1}
{VARIABLE temp_turn_number $turn_number}
{VARIABLE_OP temp_turn_number add 1}
[event] 
name=turn $temp_turn_number
first_time_only=yes
delayed_variable_substitution=no ## if it doesn't work this way then try to remove it.. I'm not sure if it's 100% needed, but it's highly likely 
### (otherwise $temp_turn_number MAY not be converted into anything.. it will stay with $ sign and never work as intended.
## other variables may also have hard times to be converted.. when it's event inside event.. magic may start happening.. may not;) depends on situation.
[remove_item]
x,y=$x1,$y1 ### if it doesnt work try to use $|x1 and $|y1 instead.. event inside event may cause differences..
### if you dont put delay variable substitution $x1 $y1 would probably work as it is, but event itself probably wouldnt... need testing. not 100% sure.
image=items/bones.png
[/remove_item]
[/event]
{CLEAR_VARIABLE temp_turn_number} ## idk if this needed or not.. don't think it will break anything with event inside event if you clear it.
[/event]
The reason why it wasnt working before was this "delayed_variable_substitition=no" (should be delayed_variable_substitution=no )
User avatar
PapaSmurfReloaded
Posts: 819
Joined: November 17th, 2007, 1:10 pm
Location: Argentina

Re: Storing turn number in an array, modifying it and using it as condition

Post by PapaSmurfReloaded »

enclave wrote: October 29th, 2018, 7:05 amThe reason why it wasnt working before was this "delayed_variable_substitition=no" (should be delayed_variable_substitution=no )
Happens. :whistle:

Thank you, I'll see how it fares.
User avatar
PapaSmurfReloaded
Posts: 819
Joined: November 17th, 2007, 1:10 pm
Location: Argentina

Re: Storing turn number in an array, modifying it and using it as condition

Post by PapaSmurfReloaded »

Well enclave, it works wonderfully. Thank you.

Bloody Mod updated, now stains and remains decay over time and you set how fast with a slider. :)

EDIT: Thanks everybody else for the ideas too. :mrgreen:

/topic :eng:
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: Storing turn number in an array, modifying it and using it as condition

Post by enclave »

very welcome :) sorry for mistype.. could save u tons of time..
Post Reply