Several questions about variables (SOLVED)

Started by Tamanegi, Fri 14/12/2007 20:37:39

Previous topic - Next topic

Tamanegi

Hi, I'm the new guy  :)

I want to make a door that can be open or closed (obviously), and already figured out that I can't do that with properties as those can't be changed in run-time. So I have to use variables.

Knowing what I need to find out, I discovered the manual to be not very helpful because I don't know where even to start looking.

My questions:
1) Is there a way to access room local variables from another room?
2) What do I have to click to bring up the script for defining room or global variables? I defined them via the interface for events, but the code shows up nowhere I looked for it.
3) Where in the manual is the usage of variables explained? I have some experience with TurboPascal and Basic, but anything C is relatively new to me.

Thanks for helping!
Don't Look! MAGS 12-2010
Mediocre Expectations Current development blog

RickJ

Quote
1) Is there a way to access room local variables from another room?
Nope! 

Quote
2) What do I have to click to bring up the script for defining room or global variables? I defined them via the interface for events, but the code shows up nowhere I looked for it.
I don't know what you have done with events but it sounds like you have defined the event handler function to have a parameter.   You have to type a declaration statement into the script file just like in Pascal and basic.  Ok, so where should you type? 

Defining Variables - variables are defined by a declaration statement appearing a script and having the form of "type  name;".  So to declare an integer called MyVar one  would enter:

int MyVar;   // Static variable delaration

Dynamic Variables - Variables defined within the bounds of a function are defined only whil;e the function is executing.  When the script within the function completes the variable is no longer defined.  Consequently it is only accessable from within the function.  Further, since it is not defined between executions of the function it's value is not retained from one execution of the function to the next.   

Typically dynamic variables such as "int i;" are used for things like looping.  Since it is only defined within the function it is possible to define the sane variable inside other functions.  They are all separate variables even though they have the same names.

By convention, dynamic variables are defined at the beginning of a function.  I have seen examples of people defining dynamic variables all over the place, even inside of conditional statements, and from the compiler's point of view it's not a problem as long as it's defined before it's used.  IMHO, this practice is horribly error prone and naieve.

function MyFunction() {
   int i;   // Dynamic variable declaration
}

Static Variables - Variables defined outside the bounds of a function retain their values through out the duration of the game.  They can be accessed from anywhere within the script file in which they are defined.  By convention static variables are defined nar the beginning of a script file before any functions are defined.  However, if a static variable is meant to be accessed by only one function then it is acceptable to define it just before the function is defined. 

int MyVar;   // Static variable delaration

function MyFunction() {
   int i;   // Dynamic variable declaration

}

Global Variables - Static variables defined in the global script or in a module script can be accessed from any room script  if there definitions are first "imported" into the room script where they are to be used.   The best way of doing this is to put the import statement in the script header rather than in each and every room script. 

// Script Header
import int MyVar;

Quote
3) Where in the manual is the usage of variables explained? I have some experience with TurboPascal and Basic, but anything C is relatively new to me.
Type "Script language keywords" into the help index

I would also suggest checking out DemoQuest for some actual examples and also reading the documentation that comes with it, especially the one on Programming Conventions.

GarageGothic

Quote from: RickJ on Fri 14/12/2007 21:28:35
Quote
1) Is there a way to access room local variables from another room?
Nope! 

As RickJ says, this isn't directly possible, which is why we also have Global variables. However, you CAN access certain local variables such as hotspot status and object positioning using the OtherRoom module.

Tamanegi

Thank you, that really enlightened me  :)

In Basic (and I meant C64 Basic!) you don't need to define variables at all, and in Pascal that goes into a special variable part of the header, which I had been looking for... so I just put it in the main part of the script and it works... wow. Comfortable. I imagined it to be much more strict.

Hm, so I can't just make a room variable with the door status and access it from the other side of the door like "room.variable", that's a pity. I will have to go with a global variable as it looks.

I tried it and everything works as desired!  :D Just finished my first open/close door animation, I feel so mighty...  8)
Don't Look! MAGS 12-2010
Mediocre Expectations Current development blog

Scorpiorus

Yep, many "C++"-style languages allow you to declare variables almost anywhere you need one which makes it more flexible to describe things in the current context.

As for Basic's "not necessary to declare a variable before using it", it mostly works for really simple routines with short variable names where otherwise variable declarations would take more space than the code itself, making things unwieldy than necessary. For more complex codes, however, it is really a must for a language to require all variable identifiers to be defined to prevent potential errors in code's logic. For example:

Code: ags

For i = 1 to 7

    ' most likely a logic mistake here, 
    ' DaysOfWeekCount and DayOfWeekCount are two different variables

    DaysOfWeekCount = DayOfWeekCount + 1

Next i


That's why there is an "option explicit" setting in MS Visual Basic, for instance, to make it check for undeclared names.

Tamanegi

That sort of answers my lingering question about why the variables in C++ have to declared at all in the first place...

Well, C64 Basic allows variable names to only have two characters anyway, two letters or a letter and a number  ;) That somewhat limits the chance of typos.
Don't Look! MAGS 12-2010
Mediocre Expectations Current development blog

SMF spam blocked by CleanTalk