May 01, 2024, 02:02:14 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


How to access a global variable from in a class?

Started by Shannara, December 21, 2006, 08:16:47 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Shannara

December 21, 2006, 08:16:47 PM Last Edit: December 21, 2006, 08:39:52 PM by Sync
Ref: http://www.ionicwind.com/forums/index.php/topic,748.0.html

Unfortunately, the above didnt answer this :( The documentation doesnt mention anything on how to do this. Only that once a variable is declared global, then it is accessable from all sources ... but what about from with-in classes? When referencing a global variable from with-in a class, nothing happens or tons of errors. Is there a snippit somewhere on how to do this?

Example:


Global Quit;
Byte Quit = 0;

ClassTest : BaseClase
{
  // Whatever

}

ClassTest :: OnCreate()
{
  Quit = 1;
}

Love is staying up all night with a sick child, or a healthy adult.

Ionic Wind Support Team

Ummm.  Back at you.  There is nothing special about accessing global variables.  They are global to the file they are defined in, or global to the program if specifically marked using the global keyword.  You access them like any other variable so how about showing me some source ;)
Ionic Wind Support Team

Shannara

I love playing ball :) I have to snip, unless you want all 6 files of code :) If you do, I'll post, no problemo.

here are some code :)

Main.src

global Quit;
byte Quit = 0;

#include "Windows.src"

/* Main routine */
global sub main()
{
    Quit = 0;
mMainMenu frmMainMenu;
frmMainMenu.CreateEx("Main Menu");

do{ wait();} until Quit = 1;
}



Windows.src

mMainMenu :: OnControl(int nID, int nNotifyCode, unsigned int hControl)
{

if (nNotifyCode == 0)
{
select(nID)
{
case 1:
// do nothing

case 2:
// do nothing

case 3:
// do nothing

case 4:
// do nothing

case 5:
// do nothing

case 6:
// quit out of program!
Quit = 1;
}
}
return true;
}



Errors returned
Quote
Compiling Resources
Compiling...
Main.src
File: Windows.src (58) invalid assignment
Error(s) in compiling "C:\Projects\Online RPG  Maker (Aurora)\Client\Main.src"
Base Windows.src
Base Controls.src
Windows.src
File: C:\Projects\Online RPG  Maker (Aurora)\Client\Windows.src (58) invalid assignment
Error(s) in compiling "C:\Projects\Online RPG  Maker (Aurora)\Client\Windows.src"

It has an error compiling Main.src, however the error is on line 58 of Windows.src. Which is the following line:


Quit = 1;

Love is staying up all night with a sick child, or a healthy adult.

Ionic Wind Support Team

#1. you are including the source, and also have it listed in the project.  That will cause strange errors so don't do that.

The number one rule I always stress is don't #include code.  Include files are meant for defintions and constants.  As your program is there will be two variables named 'quit'.  The one created when you #included "windows.src" and the one create when windows.src is compiled by the project.  There will also be two duplicate methods named mMainMenu::OnControl, which the linker will allow but only the first reference will be linked.  Think of it this way.  Every source file creates an object file.  Those object files are linked together to create the executable.

main.src -> main.o : generates  mMainMenu::OnControl, Quit, main.
windows.src -> windows.o : generates mMainMenu::OnControl, Quit.

The second Quit variable is generated since you have autodefine turned on and dont have an EXTERN statement to inform the compiler that that variable is located in a different object file.

#2.  If you have a variable that is defined as global in one file you have to declare it as extern in any other file that wants access to it.

//file1.src
global Quit;
byte Quit = 0;

//file2.src
extern Quit as byte;

#3.  Use #autodefine "off" while you are learning.  It will force you to define your variables before using, and also give you a more appropriate error message.  When windows.src is compiled by the project you would get an "undefined" error on Quit. 

If you want to send me your project I can fix it for you.  Or you can attempt to do so on your own ;)

Paul.
Ionic Wind Support Team

Shannara

December 21, 2006, 09:20:39 PM #4 Last Edit: December 22, 2006, 01:15:02 PM by Sync
I guess I'll attach the project. From reading about includes and sources on these forums, I was under the impression that variables, structs and classes were to be in inc classes (and not in projects), while sources are just sources. But I couldnt figure out a way reference a class from another project source (it's implimentation) unless I #Include the src file. erk, ah well. I went ahead and attached the files/project/rc. etc. It's all there. Maybe I can learn from the fixes :(

((removed attached file))
Love is staying up all night with a sick child, or a healthy adult.

Ionic Wind Support Team

I've downloaded it and will try an give you an answer by this evening.  Been a bit busy getting ready for the holiday.
Ionic Wind Support Team

Shannara

December 22, 2006, 01:10:56 PM #6 Last Edit: December 22, 2006, 01:16:27 PM by Sync
I believe I have it working correctly now :) The only problem is that the extern command in another file referencing the Quit variable gives the following error:


File: C:\Projects\Mirage (Aurora)\Client\Windows.src (49) invalid assignment
Error(s) in compiling "C:\Projects\Mirage (Aurora)\Client\Windows.src"


This happens when Quit = 1; is called. Even though extern Quit as byte; is in the source :) I'll wait and see what you have done for global variables this evening to see what I missed.
Love is staying up all night with a sick child, or a healthy adult.

Ionic Wind Support Team

Like I said give me some time please.

You have your class definitions in an include file

//stuff.inc
class MyClass
{
     declare mymethod1();
     declare mymethod2();
}


Then any source file knows that class exisits by including it.

//impl.src
#include "stuff.inc"

MyClass::mymethod1()
{
print("oh...");
}

//main.src
#include "stuff.src"

MyClass mc;
mc.mymethod1();

It is really quite simple.  I can't beleive C# is that much more different than C/C++ that you wouldn't have seen standard include files before.

Paul.
Ionic Wind Support Team

Shannara

Yeppers :) Got the bugger working except for that global variable is still giving issues :) Don't worry it's christmas weekend, lots and lots of time :) C# does not have any include files at all. And everything is completely class based. Thus if you want to access something in C#, you have to first encapsolated it into a class, create an instance of the class, before even using that function. Really weird, round-about way, but works of sorts :)
Love is staying up all night with a sick child, or a healthy adult.

Ionic Wind Support Team

Also be sure you are using the latest version of the compiler.  I wasn't able to reproduce the error in my code before you gave me the project. 

Second question is why a byte?  Use a bool or int if it is only a true/false operation ;)
Ionic Wind Support Team

Shannara

I think I have the latest installed, 1.0 (Beta 1 Rev 3) :) I guess it's a size thing. I wanted to keep the exe as small as possible since OOP compilers produce huge exes. I wanted trim down as much of that as possible. I believed I read somewhere that a boolean in aurora is 4 bytes (or was it 2?) instead of one due to speed. So I choose a byte :)
Love is staying up all night with a sick child, or a healthy adult.

Ionic Wind Support Team

The answer is:

You have a class variable named 'Quit' defined in your mMainMenu defintion.  Member variables and local variables take precedence over global ones.  So when you have Quit = 1 it is invalid since the compiler knows you can't assign a number to a class ;)

Anyway here is a fixed project.  Only one include file needed, variable renamed to QuitProgram.  Compiled and tested with executable included.

Nice graphics BTW ;)
Ionic Wind Support Team

Shannara

Ah, stupid I. oh man, Stupid I. As you said, Quit was a member name, heehehe.. man that was stupid of me.. Thank you Paul.

The graphics was from a game I bought back in 2001 called Mirage Online. Im recoding the bugger in Aurora since it's faster to code in Aurora then other compilers I've came across (with the exception of VB). And ... future 64-bit support, hehehe. I'm already drooling.
Love is staying up all night with a sick child, or a healthy adult.