Any reason to NOT use byref?

Get Help on scripting in POL with configurations, config setups, script trouble shooting, etc.
Post Reply
User avatar
tekproxy
Forum Regular
Posts: 352
Joined: Thu Apr 06, 2006 5:11 pm

Any reason to NOT use byref?

Post by tekproxy »

I remember reading a few threads on the old forums about using byref variables. I understand that it's better sometimes to pass a refrence instead of a large structure, but what about object or character refrences? Should I always pass those byref? What is to keep me from passing most variables byref? When does it not matter?

By the way, anyone that doesn't know what this means, here's an example:

Code: Select all

function MakeBigStruct()
  var big_huge_struct := {};

  big_huge_struct.+everything := some_big_variable;
  big_huge_struct.+something := "42";

  SomeFunction(big_huge_struct);

  // This will print thursday instead of 42 since it was passed byref
  // to SomeFunction()
  print("Never could get the hang of this day: " + big_huge_struct.something)
endfunction


function SomeFunction(byref big_huge_struct)
  big_huge_struct.something := "thursday";
endfunction
Normally when you pass a variable to another function it copies that variable for use locally within that function. However, when it is passed byref only a "refrence" to the variable is passed, so when you act upon that refrence, you act directly opon the origional variable.

Please excuse my code if it doesn't make sense. I really never could quite get the hang of Thursdays and it's 4:32 am...
User avatar
CWO
POL Expert
Posts: 1160
Joined: Sat Feb 04, 2006 5:49 pm

Post by CWO »

Object and character references are always byref.
Lagoon
Grandmaster Poster
Posts: 118
Joined: Sun Mar 05, 2006 7:25 am

Post by Lagoon »

As a general rule I use byref in three cases

1) when I have to modify the passed variable in the function
2) when I pass a variable just to read it and not to modify it and its memory use is considerable, so I avoid creating a local copy
3) when I need a function which returns more than 1 value, so I pass empty variable by reference (I prefere it rather than returning a structure)

But please don't take it as "official" because I don't know how pol works internally, so I have no idea about the performance of variables by reference vs. by value
User avatar
OldnGrey
POL Expert
Posts: 657
Joined: Sat Feb 04, 2006 6:26 pm

Post by OldnGrey »

CWO - do you mean that they are byref even if you don't say it in the function? For example:

Functiony FnName(byref character)
is the same as
Function FnName(character)

or

did you mean that you always use byref in the function for objects and characters. (assume you could mean mobiles here)


I must admit that even after 4 years of eScript, this is still an area I don't claim to understand perfectly.


OldnGrey
Lagoon
Grandmaster Poster
Posts: 118
Joined: Sun Mar 05, 2006 7:25 am

Post by Lagoon »

OldnGrey wrote:CWO - do you mean that they are byref even if you don't say it in the function? For example:

Functiony FnName(byref character)
is the same as
Function FnName(character)
It can't be, impossible. Try passing a mobile ference by value and not by reference an try changing it.... it won't change in the calling function
I see no sense in what CWO sayd, no offense :P
Being an object reference has nothing to do with being a variable passed by refence rather than by value
User avatar
tekproxy
Forum Regular
Posts: 352
Joined: Thu Apr 06, 2006 5:11 pm

Post by tekproxy »

Lagoon wrote:1) when I have to modify the passed variable in the function
2) when I pass a variable just to read it and not to modify it and its memory use is considerable, so I avoid creating a local copy
3) when I need a function which returns more than 1 value, so I pass empty variable by reference (I prefere it rather than returning a structure)
These are good general rules that I've considered (from reading various other posts here and there). I've never used a byref like the way you describe in the 3rd variable though. That should make things interesting.

To both of you, thank you.
Yukiko
Distro Developer
Posts: 2826
Joined: Thu Feb 02, 2006 1:41 pm

Post by Yukiko »

I am thinking over what CWO said and I think he's correct. Take a look at the following pseudo code or meta code or whatever you want to call my paraphrase of a script.

*smiles*

Code: Select all

program does_stuff (who)

...
// proggy does something to determine whether to change who's graphic
...

if (we want to change graphic of who)
     change_graphic(who);
endif

endprogram

function change_graphic(who)

who.graphic := <some strange graphic>;

endfunction
The graphic will always get changed on the original because it is a byref passage. Same holds true for all objects too I believe.
User avatar
CWO
POL Expert
Posts: 1160
Joined: Sat Feb 04, 2006 5:49 pm

Post by CWO »

I meant that object and character references are automatically byref.

Code: Select all

Program myprogram(who)
  who.frozen := 0;
  freezeme(who);
  if (who.frozen == 1)
    SendSysMessage(who, "You are frozen");
  endif
  who.frozen := 0;
endprogram

function freezeme(who)
  who.frozen := 1;
endfunction
no need to put byref before who since its a char reference it will still modify the char. It will do the same thing for objects too. Try that code above, the function will make sure you're not frozen first of all, then pass you to a function without byref, the function will still be able to modify you even without the byref and the program will see the change and say "You are frozen".
Yukiko
Distro Developer
Posts: 2826
Joined: Thu Feb 02, 2006 1:41 pm

Post by Yukiko »

ha ha ha ha you were just a few minutes too slow CWO.

*grins*

But you are right. Never really thought that through before. One of those things you know happens a certain way but never give it a moments thought.
Lagoon
Grandmaster Poster
Posts: 118
Joined: Sun Mar 05, 2006 7:25 am

Post by Lagoon »

You are explaining what an object reference is, but saying "object and character references are automatically byref" is wrong. What is true is that "object and character references are automatically REFERENCES", they aren't actually byref.

Code: Select all

program textcmd_test(parms)
  var first_character, first_character_bis;
  FirstCharacterOnlineByReference(first_character);
  FirstCharacterOnline(first_character_bis);
endprogram

function FirstCharacterOnlineByReference(byref who)
  who := EnumerateOnlineCharacters()[1];
endfunction

function FirstCharacterOnline(who)
  who := EnumerateOnlineCharacters()[1];
endfunction
The two functions are deeply different, the first call works and sets first_character with a mobile reference to the first onliune character, the second call does not work because the variable first_character_bis is passed by value, so first_character_bis will continue to be uninitialized.
You cannot call an object reference "a by reference variable"... if you want to give it a different name call it "a pointer to an instance of an object", which is how it should work internally... but it has nothing to do with how the variable is seen in the script
Yukiko
Distro Developer
Posts: 2826
Joined: Thu Feb 02, 2006 1:41 pm

Post by Yukiko »

I think you stated what CWO was saying. Maybe your explanation is phrased with proper nomemclature though.

Basically objects passed in function calls are by their nature "byref" or if you prefer, pointers to the instance of an object. Either way isn't the functionality the same?

I have to admit it took me a long time to understand byrefs and pointers when I first got into procedural languages so I may be ignorant here.
Lagoon
Grandmaster Poster
Posts: 118
Joined: Sun Mar 05, 2006 7:25 am

Post by Lagoon »

They are similar things but at two different "layers" of the program.
POL has a sort of virtual machine which processes scripts. when you pass a variable by value or by reference, you are doing it in the script, which is the code in the outmost layer. Then when you use methods or read/modify values of an object trought its reference it's the innermost layer, the core, which manages this. Now, how does it work? well, in general you can see as if there are variables passed by reference (but in the core compiled code, nothing to do with the script), but in the specific case of pol I think it's made with classes (pol core is written in object oriented c++, isn't it? not sure on this...). So... well... they might be seen as a similar thing, but I'd avoid telling it in a job interview for a software house :P. What is sure is that they even if similar they act at two completely different levels, and I wanted this to be clear because from the first posts I had the feeling this was not. Of course I may be wrong :)
Yukiko
Distro Developer
Posts: 2826
Joined: Thu Feb 02, 2006 1:41 pm

Post by Yukiko »

I am somewhat familiar with the virtual machine system as I was first introduced to structured programming with UCSD Pascal which used a compiled pseudo code (P-code). The "core" for USCD executed the P-code just like POL does. That was back in the days before we ever knew what a virtual machine was.

What you are saying about the object reference is that POL sees those as a special condition and handles that before the P-code level gets it. Essentially the core sends it as a reference before the code ever sees it.

Anyway, I am off to wok soon so have fun.
Post Reply