#Double Invocation Problem

1 messages · Page 1 of 1 (latest)

fervent stirrup
#

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

grizzled roost
#

When do you unsubscribe? Also can I see the debug output?

#

It double prints on even the first question answered?

fervent stirrup
fervent stirrup
#

In doing so it skips the 2nd question

#

Sorry for the late response I was gone for a bit

fervent stirrup
#

Debug output from a single click on the correct button

fervent stirrup
#

@grizzled roost 👀

young dirge
#

Hmmm, can you check the length of listeners for the button? Is it more than 2?

fervent stirrup
#

What was the syntax for checking length of listeners again?

#

I think I found it

#

Not really

fervent stirrup
young dirge
#

Okay, what about the button that is pressed. Can we check how many listeners it has?

fervent stirrup
#

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

young dirge
#

Also, you shouldn't need () =>, you can just put the method . . .

#

Not really sure, unless there's another script adding it. I'm stumped . . . 🤷🏼‍♀️

fervent stirrup
fervent stirrup
fervent stirrup
#

I’ll look at it some more tomorrow

#

I’ll keep you posted.

young dirge
#

I mean, you can just do cs AddListener(CheckA(aButtons[j]));

#

Both variations work though. At least it has for me . . .

fervent stirrup
fervent stirrup
#

I don't think this is right

knotty rock
fervent stirrup
#

Even if it would, that wouldn't make it act the way it does. But no.

fervent stirrup
knotty rock
#

Man I can't figure it out. Maybe you have two EventSystems. probably not. Good luck :(

fervent stirrup
#

Thanks for trying though :)

fervent stirrup
knotty rock
#

Lol i forgot about that megaspam

fervent stirrup
#

It's Dalphat 👀

burnt oyster
#

I do not see where aButton is declared (the array) but perhaps you've got two references to the same button in the array 🤷‍♂️

fervent stirrup
#

@burnt oyster

burnt oyster
#

Can you show us the console log?

fervent stirrup
#
  1. It skips to the 3rd question from the 1st
  2. it prints two printlines instead of one
burnt oyster
#

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 ..."

fervent stirrup
burnt oyster
#

If you get two print outs between the logs, the problem may lie elsewhere.

#

statements

#
//Started

//Your statements

//Stopped```
fervent stirrup
burnt oyster
#

name

#

Try it

knotty rock
fervent stirrup
#

ah right

knotty rock
#

i mean script's name

burnt oyster
#

Component should have access to inherited member name

#

Implies this has access to name

fervent stirrup
#

Doesn't seem off, does it?

fervent stirrup
burnt oyster
#

As expected

fervent stirrup
#

Yeah so that part is fine right?

burnt oyster
#

The issue isn't in this script

fervent stirrup
#

So where would the issue be then?

burnt oyster
#

Whatever those lines are doing.

fervent stirrup
#

Only one instance of the script exist

#

Notably this prints twice as well

burnt oyster
#

I'm on Mobile and it's difficult to fully analyze properly.

fervent stirrup
#

That's fair

burnt oyster
#

What is answer?

#

It's being assigned something.

fervent stirrup
knotty rock
#

Are you sure that the button click happens only once per click?

fervent stirrup
#

answer is an enum\

burnt oyster
#

on click had been confirmed to be only happening once

fervent stirrup
#

Okay

#

Apparantly yes

burnt oyster
#

That was what my test from above proved

fervent stirrup
#

Righty

burnt oyster
#

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

fervent stirrup
#

Yet that prints twice as well

burnt oyster
#

Only thing I'm aware of is that button on click is only occurring once.

fervent stirrup
#

This is all the Ext.OneLine does btw

burnt oyster
#

So you may have two print actions?

fervent stirrup
burnt oyster
#

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...

fervent stirrup
#

I'm sorry I'm not following

#

Still not sure what you mean

burnt oyster
#

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

knotty rock
#

yeah ^

#

are you sure that there's nothing subscribed at the start of OnEnable()?

burnt oyster
#
Debug.Log($"Length of action: {action.Length}");
foreach(...)
    a();```
fervent stirrup
#

What the fuck

#

Why was that it

burnt oyster
#

if length is one, then action is likely the next possible culprit.

fervent stirrup
#

I removed the Extension everywhere

burnt oyster
#

I'm assuming the problem (for the most part) has been solved.

fervent stirrup
#

I'm extremely confused now though

knotty rock
burnt oyster
#

What did length print?

fervent stirrup
#

It's fixed now though

fervent stirrup
#

Which apparently it's the culprit

burnt oyster
#

Problem solved, goodbye thread.

fervent stirrup
#

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

young dirge
knotty rock
#

btw. Why is it called 'OneLine'. It confused me so hard

fervent stirrup
#

Because it's on one line

#

it doesn't use ; if you use that

#

Which means you don't have to use a body

fervent stirrup
#

Who'd have thunked extension methods woulda been my downfall

fervent stirrup
young dirge
#

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) . . .

knotty rock
#

omg I didnt even notice that , checkA)

burnt oyster
#

I see OneLine(checkA, checkA)

fervent stirrup
#

My brain

#

Is melting

young dirge
#

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 . . .

fervent stirrup
#

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

young dirge
#

the issue stems from using the ternary within the argument which happens to be a params: a comma separated list . . .

fervent stirrup
#

Since it's all bound to the same Action

#

Which I somehow kinda forgot

young dirge
#

params is a comma separated list. After each comma is the next item used in the list . . .

knotty rock
#

it might be easier to figure out if you take that ternary operator mess out of the brackets.

#

in Ext.OneLine(...);

fervent stirrup
#

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

knotty rock
#

Well what does Test() print

fervent stirrup
fervent stirrup
#

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

knotty rock
young dirge
fervent stirrup
#

Hence I called it afterward

young dirge
#

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 . . .

knotty rock
#

this stems from using an extension method that you don't fully understand, just to save 1 line of code

fervent stirrup
#

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

knotty rock
#

anyway glad you found the issue.

young dirge
#

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) . . .
fervent stirrup
#

Or is this an example of the issue I'm having and I'm just not getting it (once again)

young dirge
#

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 . . .

fervent stirrup
#

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