Scolring - Forum

Entraides et échanges autour de la technologie Scol - Informations and exchanges on the Scol technology

Vous pouvez changer la langue de l'interface une fois inscrit - You can change the language once registered

You are not logged in.

#1 22-Dec-2014 23:26:25

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,541
Website

can a channel be passed as an argument in a function

i am trying to pass a channel object  as an argument in a function call, is that permitted?
thx!
-h

Offline

#2 22-Dec-2014 23:58:49

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: can a channel be passed as an argument in a function

Like any other object ...

fun getChannel (chn)=
    // do somehing

fun main ()=
    getChannel _channel;
    ...

Offline

#3 23-Dec-2014 00:22:57

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,541
Website

Re: can a channel be passed as an argument in a function

thx that's what i thought...i'm dealing with a large function and i've lost track of the ()))))))...bluefish helps i'm pasting code in from another function.

Offline

#4 23-Dec-2014 10:51:34

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: can a channel be passed as an argument in a function

The best method : one function = one action smile

Offline

#5 23-Dec-2014 13:22:21

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,541
Website

Re: can a channel be passed as an argument in a function

iri wrote:

The best method : one function = one action smile


Yes , thats my plan

Offline

#6 23-Dec-2014 14:52:27

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,541
Website

Re: can a channel be passed as an argument in a function

Looking at the code that you guys write, it seems the best method is pass a local variable from one function to another until its needed, and once you have done what you have to do as a prerequisite for using the variable, use it when you are ready, in a simpler function.  bluefish helps with understanding parenthesis problems, but only up to a certain point, for me at least.

Offline

#7 23-Dec-2014 16:34:50

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: can a channel be passed as an argument in a function

A global variable can seem easier but it can be modified easily and independantly anywhere in the code. It is always dangerous. A local variable passed from a function to another function is usually safer.
In pratice, try to keep the same and explicit name for your local variables in each function. It helps you and heps a future developer to understand what you did.
I already wrote you something about that in the forum (local variable vs global variable).

Offline

#8 23-Dec-2014 16:38:21

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: can a channel be passed as an argument in a function

About channels, you work with 3 channels at least :
- the channel of eyecandy,
- the channel of the player,
- the channel between the eyecandy and the player.

These 3 channels are differents. They must not be confused.

Offline

#9 23-Dec-2014 16:49:24

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,541
Website

Re: can a channel be passed as an argument in a function

iri wrote:

A global variable can seem easier but it can be modified easily and independantly anywhere in the code. It is always dangerous. A local variable passed from a function to another function is usually safer.
In pratice, try to keep the same and explicit name for your local variables in each function. It helps you and heps a future developer to understand what you did.
I already wrote you something about that in the forum (local variable vs global variable).

thx, i have everything working now, i can pass a channel between functions in one package and between packages in the same environment as a local variable

i did however have problems in passing a defcom variable (of the form defcomvariablename S) from one function to another and between packages. but since i am using only one channel between all the .pkgs of the vm using the defcom variable, i don't think there is any great harm done in leaving it as a global variable...although can this be done?

in english there is a phrase, 'hot potato', which means passing a hot potato from one person to another if you catch it, you throw it to someone else, since it's hot.

that's what i try to do with variables now. if i need the same variable in function 1 and 3 but not in function 2, i pass it through function 2 when function 1 calls function 2 which calls function 3, like a hot potato.

Offline

#10 23-Dec-2014 16:50:53

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,541
Website

Re: can a channel be passed as an argument in a function

iri wrote:

About channels, you work with 3 channels at least :
- the channel of eyecandy,
- the channel of the player,
- the channel between the eyecandy and the player.

These 3 channels are differents. They must not be confused.

one thing i don't quite understand is that the channel from the player to eyecandy is created on the port that eyecandy is using for eyecandy's server, but eyecandy's channel to the player uses the eycandy's implicitly created _channel.

but i understand it enough to make it work.

Offline

#11 23-Dec-2014 17:19:46

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: can a channel be passed as an argument in a function

That's why i had preferred to point out !

Be careful, _channel is not a variable, it is a function. It returns the current channel, one that is used when this function is called.

When you create a VM, a new channel is always automatically created.
A reminder, a channel is a couple : a network connection (in this case, a local connection) + an environment. An environment is all Scol api (if you prefer, all Scol language) + all loaded packages.

When a VM is conencting to another VM, another channel is also created : it is different (another network connection, local or distant) and the environment is often different too.

When you call _openchannel, you can give an environment (the third argument) :
usually it is either the current environment (= the same environment that the VM at this moment, and you add other packages later if you want) or you set "nil" and the new channel has the Scol apis only (minimal environment).

So, according the context, _channel returns the "linked" channel or the "VM" channel.

If you are not sure, define a variable for each one.

In the first VM :

typeof ChnVM = Chn;;
typeof ChnLinked = Chn;;

fun linkOtherVm ()=
    set ChnLinked = _openchannel pif paf pof;
    //...

fun main ()=
    set ChnVM = _channel;
    // ...

In the other VM, use the _connected function. This function is AUTOMATICALLY called by Scol when a connection is doing. If it exists, it executed (else it is ignored) :

typeof ChnFromAndToOtherVM = Chn;;

fun _connected ()=
    set ChnFromAndToOtherVM = _channel;
    0;;

and use the _closed function to set to nil when the connection is lost / killed / closed / ...

fun _closed ()=
    set ChnFromAndToOtherVM = nil;
    0;;

Thus, you should not be used _channel after that !

Offline

#12 23-Dec-2014 17:21:46

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: can a channel be passed as an argument in a function

hebdemnobad wrote:

that's what i try to do with variables now. if i need the same variable in function 1 and 3 but not in function 2, i pass it through function 2 when function 1 calls function 2 which calls function 3, like a hot potato.

Yes.

Offline

#13 23-Dec-2014 17:55:01

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,541
Website

Re: can a channel be passed as an argument in a function

thx for your explanation iri

Offline

Board footer

Powered by FluxBB