#Double Invocation Problem
1 messages · Page 1 of 1 (latest)
Still have no idea what is causing the nextQuestion to run twice
And it's only subscribed once
But once I click the button, it runs it twice
These are all of the references
Those two other subscribed members only print printlines:
private void CorrectText() => print("Hooray you got the correct answer!");
private void IncorrectText() => print("You got the wrong answer :(");
That's all, any ideas are greatly appreciated!
Please, anyone
When do you unsubscribe? Also can I see the debug output?
It double prints on even the first question answered?
In the ondisable
Yes
In doing so it skips the 2nd question
Sorry for the late response I was gone for a bit
private void OnDisable()
{
Qs.OnQUIActive -= QUIActive;
Qs.OnIncorrect -= IncorrectText;
Qs.OnCorrect -= CorrectText;
Qs.OnCorrect -= nextQuestion;
for (int i = 0; i < aButtons.Length; i++)
aButtons[i].onClick.RemoveAllListeners();
}
Debug output from a single click on the correct button
@grizzled roost 👀
Hmmm, can you check the length of listeners for the button? Is it more than 2?
What was the syntax for checking length of listeners again?
I think I found it
Not really
Got it
So no, just like the breakpoint told me
Okay, what about the button that is pressed. Can we check how many listeners it has?
We probably can just have to figure out how
I can't actually figure it out how
Some posts say it's not possible for the specific onclick listener
Regardless, these are all
Each button should have one listener
Unless something is wrong with my loop
My train is leaving in like 9 minutes so after that I'll not be available for 2 hours cuz travel time
Also, you shouldn't need () =>, you can just put the method . . .
Not really sure, unless there's another script adding it. I'm stumped . . . 🤷🏼♀️
If you mean that for the 2nd screenshot then no. I need to fill in a parameter and that method does not accept parameters of such kind
No everything happens with this script
Yup I’m also stumped with how it invokes twice
I’ll look at it some more tomorrow
I’ll keep you posted.
I mean, you can just do cs AddListener(CheckA(aButtons[j]));
Both variations work though. At least it has for me . . .
I've no idea how you got that to work
I don't think this is right
@fervent stirrup are you sure that aButtons doesn't include duplicate buttons?
I honestly wish that was the problem though
Man I can't figure it out. Maybe you have two EventSystems. probably not. Good luck :(
:(
Thanks for trying though :)
Definitely not the event system, if you add another one it completely spams you with you fucking did what
kek
Lol i forgot about that megaspam
It's Dalphat 👀
I do not see where aButton is declared (the array) but perhaps you've got two references to the same button in the array 🤷♂️
It's declared within the inspector
@burnt oyster
How are you verifying that it's running twice?
Can you show us the console log?
- It skips to the 3rd question from the 1st
- it prints two printlines instead of one
Place a log before and after the statements inside the CheckA method.. preferably something that's unique and differentiable:
$"{name} started CheckA with {button.name}"
$"... stopped ..."
After which statement in CheckA specifically
If you get two print outs between the logs, the problem may lie elsewhere.
statements
//Started
//Your statements
//Stopped```
What name do you want in the first part?
a.k.a. gameObject.name
ah right
i mean script's name
Component should have access to inherited member name
Implies this has access to name
Doesn't seem off, does it?
Yeah I get it thanks
As expected
Yeah so that part is fine right?
The issue isn't in this script
So where would the issue be then?
Whatever those lines are doing.
I'm on Mobile and it's difficult to fully analyze properly.
That's fair
this
Are you sure that the button click happens only once per click?
No.
on click had been confirmed to be only happening once
That was what my test from above proved
Righty
The print out is being done twice
Likely from the Ext One Line statement or whatever's inside of it that's calling Debug Log
Though the print doesn't use it
Yet that prints twice as well
Only thing I'm aware of is that button on click is only occurring once.
This is all the Ext.OneLine does btw
So you may have two print actions?
What?
I mean.. is you had 10 print actions in that array, you'd get 10 print outs
Where a() would have been called 10 times...
You're doing something x amount of times
Where likely a() is print
And length of action is not 1
Verify yourself
log the length of action before calling a()
Debug the bug
yeah ^
are you sure that there's nothing subscribed at the start of OnEnable()?
is this how you would check the count?
https://docs.unity3d.com/ScriptReference/Events.UnityEventBase.GetPersistentEventCount.html
Debug.Log($"Length of action: {action.Length}");
foreach(...)
a();```
if length is one, then action is likely the next possible culprit.
I removed the Extension everywhere
I'm assuming the problem (for the most part) has been solved.
I'm extremely confused now though
so was the problem just the printing or is it still skipping questions?
What did length print?
It ran twice that's why it also skipped the question
It's fixed now though
Don't know, first thing I did is remove the entire extension to see if that did anything
Which apparently it's the culprit
Problem solved, goodbye thread.
I'm real quick gonna make a temp branch of this version, I kinda need to/want to know why the extension method breaks the fuck out of everything
Thanks Dalphat!
Oh my god I get it
Of course, the fucking answer statement also invokes the actions
So it mimics whatever the fucking checkA statement does
By god I'm a moron
For some reason I thought those weren't coupled to eachother
Still not sure why that ExtensionMethods invokes it twice though
Next on my list of what the fuck is happening
based on this, your code solves and assigns checkA, then uses checkA again for the second item in params . . .
btw. Why is it called 'OneLine'. It confused me so hard
Because it's on one line
it doesn't use ; if you use that
Which means you don't have to use a body
Who'd have thunked extension methods woulda been my downfall
I'm testing stuff with it to see if I think I know what it does
it's how you used it. params means you list each item separated by comma . . .
you solve for and assign checkA then use a comma with checkA again . . .
so you basically did: Ext.OneLine(checkA, checkA) . . .
omg I didnt even notice that , checkA)
I see OneLine(checkA, checkA)
i was looking for the method signature but couldn't find it. assumed it was either the answer = allQs line or the Ext.OneLine. good thing Dalphat saw it . . .
I'm sorry but I'm really not understanding what's going wrong
Well, I partially understand
I know why it skipped the question as well as did two prints
the issue stems from using the ternary within the argument which happens to be a params: a comma separated list . . .
params is a comma separated list. After each comma is the next item used in the list . . .
it might be easier to figure out if you take that ternary operator mess out of the brackets.
in Ext.OneLine(...);
It has to do with the Action being returned in a foreach Action from what I understand?
I'm very confused
Cuz I'm testing this but this works fine
Totally not losing my sanity
Well what does Test() print
I swear I'm not ignoring you by the way, I'm just extremely confused
What I initally thought of being wrong with Ext.OneLine is that I thought for each element added, it'll execute each element the length of that
But that's not true
So now I'm back to being full on confused kek
OH
I GET IT
FUCK ME
Now it makes sense
Sort of
So
The reason why it's being called twice
Because it calls the assignment as well as the actual 2nd call
Which I didn't realise It'd execute the assignment and then call it
I thought it'd only assign it
well
yes, bc the assignment is the first item of params
Hence I called it afterward
that's what i was explaining . . .
you should only use a variable for the parameter instead of solving and/or accessing the variable you need for the parameter . . .
this stems from using an extension method that you don't fully understand, just to save 1 line of code
It stems from me being stupid
If you split it, it does work
I just didn't realise that if you assign an action, and it foreaches through those actions, it doesn't only assign it, but also call it
yeah just like i said before
anyway glad you found the issue.
here's an example ```cs
// Instead of this
var dist = Vector3.Distance(transform.position, enemy.transform.position)
// Do this
var posA = transform.position;
var posB = enemy.transform.position;
var dist = Vector3.Distance(posA, posB);
the second version only passes the necessary data to the method. it's also more performant bc you're only passing a `Vector3` and not a `Transform` component or the `Enemy` component and all of its fields (which are unnecessary) . . .
I don't think you put it in the right thread
Or is this an example of the issue I'm having and I'm just not getting it (once again)
this was to explain why you should place the ternary op outside of the method and use the result checkA in the method . . .
the assignment doesn't matter. it was used within the argument, so the result is part of the argument . . .
if you use an expression for the result of a bool as a method argument, it will use that result as the argument . . .
i think the misunderstanding was just how params works, that's all . . .
Honestly I haven't used params much at all before either so that's definitely part of the problem
But it just made sense in my head of how I thought it worked
But I suppose the other thing also makes sense
It's just still a bit confusing - but I think I get why it went wrong
I'm just doing a bit more testing so I can get it straight in my head
Because I damn well don't want to repeat this mistake
I could say fuck it and move on and not learn - but that'd be stupid, I'd shoot myself in the foot later again