Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Declare methods in scripts #72

Open
lofcz opened this issue Jan 4, 2021 · 6 comments
Open

[question] Declare methods in scripts #72

lofcz opened this issue Jan 4, 2021 · 6 comments

Comments

@lofcz
Copy link
Contributor

lofcz commented Jan 4, 2021

Hi, I'm not quite sure if I missed this in the docs, but is something like this possible?

[void] myMethod([int] a) {

}

myMethod(1);

running the above from new EE().ScriptEvaluate(), myMethod is defined inside of script, not on input / context, [] means optional.

@lofcz lofcz changed the title [question] ad hoc method definitions [question] Declare methods in scripts Jan 4, 2021
@codingseb
Copy link
Owner

codingseb commented Jan 4, 2021

It's not directly possible with the standard syntax of a method for now. But it's possible to declare some multiline lambda like function. I know it's not well documented.
But you can write something like :

var myMethod = (a) => 
{
    //Do something with a
};

myMethod(1);

I will add some docs about this.

@codingseb
Copy link
Owner

codingseb commented Jan 4, 2021

Also take note that variables declared in a lambda are scoped (only available) in the lambda block.
But for now variables declared in other types of blocks are not scoped to their block.

var x = 1;

var myMethod = () => 
{
    var a = x + 1;
};

myMethod();

// x will be available here but not a.

But

var x = 1;

if(x == 1)
{
    var a = x+1;
}

// both x and a will be available here.

It's in my todo list to scope variables in each blocks to behave more like in C# but in current version (1.4.18.0) it's not done.

@codingseb codingseb self-assigned this Jan 4, 2021
@lofcz
Copy link
Contributor Author

lofcz commented Jan 4, 2021

Thanks for the reply, multiline lambda will do for now. For the sake of backwards compatibility it would be nice to have another setting to toggle this.

It's in my todo list to scope variables in each blocks to behave more like in C# but in current version (1.4.18.0) it's not done.

If possible it would be fine to leave this open for now, for other newcomers around, I guess this would be a frequent question.

@codingseb
Copy link
Owner

codingseb commented Jan 4, 2021

it would be nice to have another setting to toggle this.

Yes good idea. I will add an option for this.

If possible it would be fine to leave this open for now, for other newcomers around, I guess this would be a frequent question.

Yes I leave this open.

@codingseb
Copy link
Owner

codingseb commented Feb 23, 2021

I added a small documentation about using lambda for method simulation here

I always keep this issue open

@lofcz
Copy link
Contributor Author

lofcz commented Feb 23, 2021

Great job, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants