Need help with local variables

Started by Imerion, September 02, 2009, 05:12:18 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Imerion

I have tried to get a program working for some time now but I can't seem to get it right. If someone here could help me it would really be appreciated.

I have a process that starts like this :

process Process1(x,y,z);
local
int Variable1;


From that process, I call another process, which looks like this :

process Process2(x,y,z);
private
 int Variable2;
begin
 Variable2=20;
 loop
   x=father.x; y=father.y;
   if (father.Variable1==Variable2) Variable2--; end


When trying to compile this, I get the error "error: Unknown identifier ("Variable1")", pointing to the father.Variable1-line.
I have checked the wiki's and I cannot understand why this does not work. If father does contain the ProcessID of Process1 and Variable1 is local, then I should be able to access it the same way I access father.x or father.y, right? I tried using public instead of local, but that didn't work either.
Try my games : Neotron Games

josebita

Locals should be declared outside of process code, and you probably want to declare them at the top of your program, certainly before usage. Something like this:
[code language="Bennu"]
Local
int variable1=0;
End;

Process robot()
Begin
   while(variable1==0)
       FRAME;
   End;
End;

Process main()
Private
   int pid=0;

Begin
   pid=robot();
   pid.variable1=2;
End;[/code]

osk

Remember that a local variable means that belongs to all processes. So it´s logical that its declaration goes out of any process code.

josebita

Just in case it's not obvious, process robot dies when variable1 is modified from the main code.
If it weren't, the process would loop forever

Windgate

It is best to use PUBLIC... Almost always is a better solution, but the code will be a little more complex especially if you have never used PUBLIC before...

I avoid to use LOCAL anymore since I know about the existence of PUBLIC.

QuoteSo it´s logical that its declaration goes out of any process code.

Yes, it is logical, I was programming with Fenix years ago and because of the position of the LOCAL sentence below the PROCESS sentence I didn't notice that that variables were common for al type of processes... :P
Iván García Subero. Programador, profesor de informática, monitor de actividades culturales y presidente de TRINIT Asociación de Informáticos de Zaragoza. http://trinit.es

Imerion

Ah, now I get it! If I declare a local variable in the beginning of my program, that variable will exist as a local in every process created. I think I had the idea wrong. I thought it was like a private, but which could also be read by subsequently called processes. Thanks for the help!

Out of curiosity though, how would I do it with a public instead?
Try my games : Neotron Games

Windgate

#6
First of all you have to pre-declare the PROCESS with PUBLIC variables like this

DECLARE
  PROCESS foo ( int param1, float param2, ... )
  PUBLIC
     int public_var1;
     float public_var2;
     ...
  END
END

You don't have to write de BEGIN-END block and dont't have to write other PRIVATE variables on the DECLARE sentence.

Once you do this, you have to declare the full PROCESS, for example:

PROCESS foo ( int param1, float param2, ... )   //Yes, the same header again
PRIVATE
   int priv_var1;
   float priv_var2;
   ...
BEGIN
  ...
END

First at all you must try to compile this.

¿Compiler says ok???

Whell, now comes the rare thing...

If you use father, you can only acces the LOCAL x,y,flags,angle, etc. In order to acces the PUBLIC variables, yo need to create a special variable where load the process ID.

¿Remember that you DECLARE PROCESS foo? Well, foo is the name of the process, but with DECLARE foo is a new type of data too!!!

You can define a variable like:

foo identifier;   //Where foo is the type of the data and identifier is the name of the variable

Now identifier CAN get the ID of any process foo and give you acces to any PUBLIC fields. For example:

identifier = foo ( 1 , 1.0 , ... );

identifier.public_var1 = 10;
identifier.x = 200; //Yes, also gives acces to the local data

At the first time it's difficult to understand how does it work... But once you understand it... It's basic!!!

If you have anymore handicaps with this I can pass you a block of code of my project Animalicos, where I used PUBLIC for first time (Thanks to SplinterGU xD).

Well, I hope this will help you... Im going to bed now lol

EDIT: Sorry, my english is a bit... Dirty, I'm improving it here a little bit xD
And I think this explanation can be helpfull for the PUBLIC part of my Bennu tutorial (In spanish...)
Iván García Subero. Programador, profesor de informática, monitor de actividades culturales y presidente de TRINIT Asociación de Informáticos de Zaragoza. http://trinit.es

Imerion

Very interesting! It is a bit more complex, but I think I get the idea. For my current project, I believe local will do. But Ill keep this in mind for my next project. If I use it from the beginning it could certainly be very useful. Thanks for the help!
Try my games : Neotron Games