Accuracy does not work
Moderator: Forum Moderators
Accuracy does not work
This code gets called whenever attack starts (to turn on) and ends (to turn off):
The problem: During the attck, unit will still use their normal accuracy, meaning they miss attacks, AND after the attack it says on their weapon "-100%", so they can not hit anything.
The intention is that the code guarantees hits by turning it up by 100, then back to normal after the attack.
It would seem like this line is never run..
But when I print the variable after this is run, accuracy is 100.
So what is going on? It seems like the variable is not saved onto the unit if it runs from the attack event.
Code: Select all
local function guarantee_hit(unit, guarantee)
for w =1, #unit.attacks do
if guarantee == true then
unit.attacks[w].accuracy = unit.attacks[w].accuracy + 100
wesnoth.interface.add_chat_message("attack accuracy start" .. unit.attacks[w].accuracy)
else
unit.attacks[w].accuracy = unit.attacks[w].accuracy - 100
wesnoth.interface.add_chat_message("attack accuracy end " .. unit.attacks[w].accuracy)
end
end
end
The intention is that the code guarantees hits by turning it up by 100, then back to normal after the attack.
It would seem like this line is never run..
Code: Select all
unit.attacks[w].accuracy = unit.attacks[w].accuracy + 100
So what is going on? It seems like the variable is not saved onto the unit if it runs from the attack event.
-
- Posts: 1440
- Joined: August 26th, 2018, 11:46 pm
- Location: A country place, far outside the Wire
Re: Accuracy does not work
Yes, it does.
Where are is unit getting set?
After you change unit, are you pushing the changes back to the actual unit (e.g. wesnoth.units.to_map()) ?
Speak softly, and carry Doombringer.
- Celtic_Minstrel
- Developer
- Posts: 2332
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Accuracy does not work
That's only necessary if you serialized the unit (usingwhite_haired_uncle wrote: ↑September 9th, 2024, 7:44 pm After you change unit, are you pushing the changes back to the actual unit (e.g. wesnoth.units.to_map()) ?
[store_unit]
or __cfg
).Where is this code running? You said "whenever attack starts and ends", but exactly what does that mean? From your description, it sounds like
guarantee(u, true)
is never called, but guarantee(u, false)
is.Re: Accuracy does not work
I have tried wesnoth.units.to_map. guarantee(u, true) does get run, I can see the variable get printed correctly too.
But suddenly... when guarantee(u, false) is run, it goes from 100 to -100.. which should not be possible.
Here is my code (in order of execution):
But suddenly... when guarantee(u, false) is run, it goes from 100 to -100.. which should not be possible.
Here is my code (in order of execution):
Code: Select all
1. on_event("attack", attack_start_event)
2. local function attack_start_event(ctx)
local unit1 = wesnoth.get_unit(ctx.x1, ctx.y1)
local unit2 = wesnoth.get_unit(ctx.x2, ctx.y2)
[...]
guarantee_hit(unit1, true)
guarantee_hit(unit2, true)
end
3. local function guarantee_hit(unit, guarantee)
for w =1, #unit.attacks do
if guarantee == true then
unit.attacks[w].accuracy = unit.attacks[w].accuracy + 100
wesnoth.interface.add_chat_message("attack accuracy start" .. unit.attacks[w].accuracy)
else
unit.attacks[w].accuracy = unit.attacks[w].accuracy - 100
wesnoth.interface.add_chat_message("attack accuracy end " .. unit.attacks[w].accuracy)
end
end
end
4. on_event("attack end", remove_tmp_hits)
5. local function remove_tmp_hits(ctx)
local unit1 = wesnoth.get_unit(ctx.x1, ctx.y1)
local unit2 = wesnoth.get_unit(ctx.x2, ctx.y2)
[...]
guarantee_hit(unit1, false)
guarantee_hit(unit2, false)
end
6 (see 3)
WHY DOESNT DOES THIS WORK?!! IS WML EVIL?!!
-
- Posts: 1440
- Joined: August 26th, 2018, 11:46 pm
- Location: A country place, far outside the Wire
Re: Accuracy does not work
Just thinking out loud here...
(or 0 of course, but for some reason I keep looking at that line like it might be equivalent to:)
what happens if unit.attacks[w].accuracy is nil here?But suddenly... when guarantee(u, false) is run, it goes from 100 to -100.. which should not be possible.
Code: Select all
unit.attacks[w].accuracy = unit.attacks[w].accuracy - 100
Code: Select all
unit.attacks[w].accuracy = - 100
Speak softly, and carry Doombringer.
Re: Accuracy does not work
I have added the following, but it does NO THING!!!white_haired_uncle wrote: ↑September 12th, 2024, 4:22 pm Just thinking out loud here...
what happens if unit.attacks[w].accuracy is nil here?But suddenly... when guarantee(u, false) is run, it goes from 100 to -100.. which should not be possible.
(or 0 of course, but for some reason I keep looking at that line like it might be equivalent to:)Code: Select all
unit.attacks[w].accuracy = unit.attacks[w].accuracy - 100
Code: Select all
unit.attacks[w].accuracy = - 100
Code: Select all
if unit.attacks[w].accuracy == nil then
unit.attacks[w].accuracy = 0
end
-
- Posts: 1440
- Joined: August 26th, 2018, 11:46 pm
- Location: A country place, far outside the Wire
Re: Accuracy does not work
To clarify my last comment, you do this AFTER you change the accuracy:
Why not check to make sure accuracy is what you think it should be BEFORE you make a change as well? From a couple of your comments, I'm not sure it is (it sounds like it's always initially 0 or nil, at least for you).
Code: Select all
wesnoth.interface.add_chat_message("attack accuracy start" .. unit.attacks[w].accuracy)
Speak softly, and carry Doombringer.
Re: Accuracy does not work
Hi, I have done so.white_haired_uncle wrote: ↑September 13th, 2024, 6:32 pm To clarify my last comment, you do this AFTER you change the accuracy:
Why not check to make sure accuracy is what you think it should be BEFORE you make a change as well? From a couple of your comments, I'm not sure it is (it sounds like it's always initially 0 or nil, at least for you).Code: Select all
wesnoth.interface.add_chat_message("attack accuracy start" .. unit.attacks[w].accuracy)
In the first phase, the value is 0 before being run. It is 100 after being run.
In the second phase, it is 0 after being run. It is -100 after being on run.
That is, is that accuracy is not being saved. It seems like unit is suddenly a local variable? Is there a method to use pointers in Lua?
-
- Posts: 1440
- Joined: August 26th, 2018, 11:46 pm
- Location: A country place, far outside the Wire
Re: Accuracy does not work
I'm not very good with lua, and worse with the unit type, but...
Assuming that's pass by value, "unit" is local to guarantee_hit, is it not?
I think I would take the contents of guarantee_hit() and move it directly into attack_start_event/remove_tmp_hits temporarily. This may be completely wrong and a waste of time for someone who knows lua, but it's what I would do in trying to isolate the issue. If that works, maybe you need to wesnoth.get_unit(unit) in guarantee_hit????
Assuming that's pass by value, "unit" is local to guarantee_hit, is it not?
I think I would take the contents of guarantee_hit() and move it directly into attack_start_event/remove_tmp_hits temporarily. This may be completely wrong and a waste of time for someone who knows lua, but it's what I would do in trying to isolate the issue. If that works, maybe you need to wesnoth.get_unit(unit) in guarantee_hit????
Speak softly, and carry Doombringer.
- Celtic_Minstrel
- Developer
- Posts: 2332
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Accuracy does not work
The unit is a (non-light) userdata, so it's passed by reference.
Re: Accuracy does not work
Omg, this is crazy. Why does it not work? Is accuracy function broken? I remember it worked well the first time I used it
I have done ALL OPTIONS
I have done ALL OPTIONS
Re: Accuracy does not work
Post a complete test case that shows the issue and then someone can probably explain.
"If gameplay requires it, they can be made to live on Venus." -- scott
Re: Accuracy does not work
It turns out that the problem was a part of another code I made which nullified damage, making it look like it missed.
Everybody, thanks to your advice and courageous encouragment, I now bear witness to the world to the add-on to define an era:
viewtopic.php?t=58727
Everybody, thanks to your advice and courageous encouragment, I now bear witness to the world to the add-on to define an era:
viewtopic.php?t=58727