1. scope problem

Hi all, I need help with a scope problem in my Euphoria program, a fantasy combat game that I am making. I have global constants defined in one of my include files to act as symbols for sequence indexes. I have a function in another include file which takes this sequence as a parameter. The problem is that even though the 2nd include includes the first, the function can't "see" them, so I get a undefined symbol type error. The only way I see to fix this is to copy the constants (without global) into the second include file, but this seems like an ugly kludge. There must be a "proper" way to do this.

Thanks.

new topic     » topic index » view message » categorize

2. Re: scope problem

Are you saying that you have a file like this ...


-- a.e -- 
global constant A = 1 

And another file like this ...


-- myapp.ex -- 
include a.e 
sequence s 
s = {1,2,3} 
? s[A] 

But when you run myapp.ex it fails because 'A' is not seen?

Can you reduce your example to the smallest possible that still fails?

new topic     » goto parent     » topic index » view message » categorize

3. Re: scope problem

canadiancoder said...

Hi all, I need help with a scope problem in my Euphoria program, a fantasy combat game that I am making. I have global constants defined in one of my include files to act as symbols for sequence indexes. I have a function in another include file which takes this sequence as a parameter. The problem is that even though the 2nd include includes the first, the function can't "see" them, so I get a undefined symbol type error. The only way I see to fix this is to copy the constants (without global) into the second include file, but this seems like an ugly kludge. There must be a "proper" way to do this.

This wouldn't be a problem in 4.0, but for 3.1 the best solution is to create a third include file. Basically, you need to analyze your code to determine the dependencies. So if both file A and B need functionA, then move functionA to file C and have both A and B include it. Another alternative (for routines, at least) is to assign the routine_id of the routine to a variable defined early enough to be seen where you need it.

It sounds like you simply need to be able to see the sequences, so you could move them to their own file, and call it constants.e, or something obvious like that.

Matt

new topic     » goto parent     » topic index » view message » categorize

4. Re: scope problem

What you posted is almost it, but the difference is symbol 'A' was trying to be accessed from within a procedure. I will post a simple example, since my actual code is too large.

characters.e

sequence Character

Character = { "Lothar the Brave ", 34, 56, 43, "Long Sword", ... etc. }

global constant Name = 1, ...etc... Weapon = 5


items.e

include characters.e

procedure ArmCharacter( sequence chr )

.. some code...

printf(1, "Your current weapon is: %s", { c[Weapon] } )

end procedure

Gives me a "Weapon" not declared error.

new topic     » goto parent     » topic index » view message » categorize

5. Re: scope problem

canadiancoder said...

What you posted is almost it, but the difference is symbol 'A' was trying to be accessed from within a procedure. I will post a simple example, since my actual code is too large.

characters.e 
 
sequence Character 
Character = { "Lothar the Brave ", 34, 56, 43, "Long Sword", ... etc. } 
 
global constant  
Name = 1, ...etc... Weapon = 5 

items.e 
 
include characters.e 
 
procedure ArmCharacter( sequence chr ) 
    .. some code... 
    printf(1, "Your current weapon is: %s", { c[Weapon] } ) 
end procedure 

Gives me a "Weapon" not declared error.

What svn revision is your binary? And which file is included first?

Matt

new topic     » goto parent     » topic index » view message » categorize

6. Re: scope problem

Hi Matt, I am not using SVN, and chars.e and equip.e include each other. All I know is that Euphoria somehow sorts this out. I am new to Euphoria and this is my first real project with it. I am used to C and Object Pascal where you must use #ifdef or #pragma once, and Pascal's 'uses' directive would not alow cyclical references at all.

new topic     » goto parent     » topic index » view message » categorize

7. Re: scope problem

canadiancoder said...

I am not using SVN, and chars.e and equip.e include each other.

Ok, so now we can guess you are using v3.1 and "include each other" is a definite issue which wasn't mentioned before.

In version 3, even though you can have files include each other, you still can't reference something before Euphoria has seen its declaration.

One way to get around this is to extract the stuff that is commonly needed in both files into a third file.


-- constants.e -- 
global constant Name = 1,  Weapon = 5 

-- characters.e -- 
include constants.e 
Character = { "Lothar the Brave ", 34, 56, 43, "Long Sword"} 

-- items.e -- 
include constants.e 
include characters.e 
 
procedure ArmCharacter( sequence chr )  
 
.. some code...  
 
printf(1, "Your current weapon is: %s", { c[Weapon] } )  
 
end procedure 

This issue has been solved in v4.0. With version 4 you can freely include each other without having this problem.

new topic     » goto parent     » topic index » view message » categorize

8. Re: scope problem

Thanks for the info. I know what is going on now and will work around it until Version 4.0 is released.

new topic     » goto parent     » topic index » view message » categorize

9. Re: scope problem

Just FYI, I modified my code to use a third global symbols include file and now everything works fine.

Thanks again for the help.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu