A Procedure is Called When I Run my App, Without Me Setting It this way

Hi all

I am building an app,
and 2 buttons in that app need to call the same code, but with a different parameter.

So I created a Procedure that gets 1 parameter,
put the code inside it,
and then made a call to that procedure from each of the 2 buttons.

The 2 buttons and the procedure all work very well,
but there’s only 1 problem:
When I run my app, for some reason the procedure is automatically called,
eventho I did not tap any button yet,
and eventho I did not create a Screen_Initialize event..

How can I fix that?

The procedure should only run when it is called, from the 2 buttons..
And not automatically when the program starts to run..

Thank you

It would really help if you provided a screenshot your relevant blocks, so we can see what you are trying to do, and where the problem may be.

To get an image of your blocks, right click in the Blocks Editor and select “Download Blocks as Image”. You might want to use an image editor to crop etc. if required. Then post it here in the community.

Taifun

1 Like

Sure

As can be seen,
on the bottom there’s a Procedure called “Function_Up”.

t is called from 2 points:

  1. Button_Up.Click
  2. Screen1.GotReceivedShared

Both callers work great - no problem there.

The problem is that whenever I run the app,
the procedure is also called..

Please tell me why this is happening,
and how I can fix this..

Thank you

Hi dear,

Got Received Shared - Kodular Docs

0 = nothing shared

so

RaYzZz,
Thank you so much.

Indeed, the app gets a type 0 every time it is opened..

Filtering for 0, solved this.

BTW,
I am curious: do you know what it happens?
Why does an app receive a share with Nothing whenever it is run?

I think it’s because when you share something with the app, there are two possibilities,

  • either the app is already open and gets called with the data,
  • or the app is closed and is opened while receiving the shared data.
    In both cases, the app triggers the event so that it’s always handled correctly.

Thank you for all your help.

If I may ask one last thing about it please

The condition I used, which is what you wrote, works.

Currently, the If-block is:

If type != 0
Then (PerformTheOperations)

Can I change the logic to be the opposite?

If type == 0
Then ExitTheEventHandler
------------
(and now the code that needs to be performed - we only get here if type was not 0)
(PerformTheOperations)

Is that possible in Kodular?

I need to “break” out of the event handler,
but not outof the whole app.

Is there a way to do that?

The idea was that if you receive data type 1-2-3-4 it does something, while if it’s 0 the condition is ignored.
By doing if type = 0 inside the condition, you need to specify a behavior; in this case, the app was opened without receiving any shared data.

Yes, I know,
and that’s what I did with my code, exactly as you showed in your sscreenshot..

But my wish is to have the code do:
If type = 0, Then break out of the Event Handler
and then, after it, I will have all my commands for the ReceivedShared event handler.

The behavior with the second way is the same as yours - we still don’t do anything for type = 0,
and only do the commands for type != 0,
but the code is just arranged differently..

That’s my question.

In any case, I opened a separate thread for that If-block question,
to see if it’s possible to do it.

The example I gave does exactly this.

If type = 0, nothing happens, precisely because it isn’t included in the condition type != 0.
The code is executed only if it is not equal to 0.
Of course, the code must be inside the condition, otherwise it will be executed anyway.

If you can, please explain more clearly what you’re trying to verify, maybe I’m not fully understanding what you want to achieve.

The best way to explain it is using Textual Code.

Here is an example in C/C++/C#/JavaScript syntax:

To achieve what I want, the screenshot you gave does this:

Function EventHandler_ScreenReceivedShared()
{
_ if (type != 0)
_ {
___ commands
___ commands
___ commands
_ }
}

This indeed works, and it is what I am currently using.

But I would like to change the writing style, to a different style (for the If part),
while still achieving the same behavior - not changing the behavior.

How:

Like this:

Function EventHandler_ScreenReceivedShared()
{
_ if (type = 0) return //This will exit the EventHandler

_ //so the next code will only run when type != 0
_ commands
_ commands
_ commands
}

You see what I mean?

Those are 2 options, to write code that achieves the same result.

The only difference between them is that
the first options (yours) puts the “good” commands (to run) inside the if block,
where in the second option the if only “breaks” out of the current function,
which then enables the “Good” commands to be outside the If-block,
without the indentation.

I hope you now see what I mean..

So to achieve this,
the only thing I am missing is some block in Kodular, that “Returns”/“Breaks” out of the current “Function”,
which in this case, is the EventHandler for ScreenReceivedShared.

Is there such a block?

I’m really sorry, but I don’t understand, I can’t get my head around this logic :face_exhaling:
Why would you need to specify if type = 0 to exit the event?
This case is already handled with != 0, which automatically exit the event as soon as it detects 0.


The only thing I can think of is this,
if it detects a 0, it doesn’t do anything and therefore exits the condition, but that doesn’t make much sense.

Yes, and this is the correct solution

Which does not really make any sense, but you can do it as @RaYzZz has shown in the last screenshot

Taifun

2 Likes

OK
Thank you RaYzZz and Taifun

I guess there’s no “Return” block in Kodular (yet),
so the second way cannot be implemented.

So as you guys said, I will stick with the current one.

BTW,
If you’re curious why I am so inclined towards the 2nd writing style and not the first one,
the answer is:

When writing code, If you can make the If do a “return”/“break” right on the start,
and then put the commands after it, without indentation,
it’s always better than having an indentation.

Now you wonder:
Why?
Why is it so better?

I’ll tell you.

When you have 1 If-block,
the difference between style 1 and style 2 is not that pronounce.

but when you have several If blocks,
then writing style #1 gets you indentation inside indentation inside indentation,
which makes the code harder to read.

and on the other hand writing style #2 gives you 0 indentation.

If you still don’t understand what I mean,
then don’t worry about it.

Thank you for all your help.

BTW:
Would’ve been nice If Kodular had a “Return” block, that can be used in both these Function types:

  1. Event Handlers
  2. Procedures
1 Like

I was watching it from the start :smiling_face::smiling_face:

Can I ask what kind of functions will this app do? I mean main function not from coding but what the main core.

Am thinking of creating a simple extension for that function, but I still even don’t get what exactly you want.

1 Like

Now I understand perfectly, thank you very much for the explanation.
Anyway, I think it would be more correct to call it a guard clause or early return.

But i’m sorry that it isn’t applicable to your situation :confused:

Nice to know there is naming for that :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.