3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1] 2

Author Topic: Same Name Variable  (Read 747 times)

« on: October 17, 2013, 09:19:07 AM »
Hi.
I have 4 Script object,that exist a one variable in them.mean 4 variable name is same. I think only 1 section indicate for this variable in memory right? or 4 section?

Thanks.
My Game : Star Wars 1.0

http://behdadgame.com/index.php?topic=3.0

possible way, if we found will make a way.
« Reply #1 on: October 17, 2013, 10:02:28 AM »
If I understand what you mean, than no. You can't 'share' variables by just giving them the same name.

However, you can use iGlobalStringSet/Get to share data between scripts.
Rocket Rumble, a 3D Rad puzzle game:
http://www.3drad.com/forum/index.php?topic=9896.0
« Reply #2 on: October 17, 2013, 10:21:30 AM »
my variable is integer and how can use iGlobalStringSet/Get Function?
My Game : Star Wars 1.0

http://behdadgame.com/index.php?topic=3.0

possible way, if we found will make a way.
« Reply #3 on: October 17, 2013, 11:12:23 AM »
In one script:

void Main()
{
   int Var = 10; //Your variable.
   iGlobalStringSet(""+Var,0); //Pretend like your variable is a string.
}

In the other script:

void Main()
{
   string temp; //Create a variable to stock the incoming data.
   iGlobalStringGet(temp,0); //Get the data.
   int Var = iStringVal(temp); //Convert the string back to an 'int'.
}

As (I think) you understood correctly, iGlobalStringSet/Get can only send and receive strings, so you'll have to convert your data to strings first.
Rocket Rumble, a 3D Rad puzzle game:
http://www.3drad.com/forum/index.php?topic=9896.0
« Reply #4 on: October 17, 2013, 12:16:09 PM »
Thanks Roberto, I Understood. ;)
mean with this way my Var only dedicate 1 section in memory?
my Variable value is different but are same name.this is not a problem?
and string variable (temp) only dedicate 1 section in memory? because I should define it in 4 script object.
« Last Edit: October 17, 2013, 12:26:41 PM by behdadsoft »
My Game : Star Wars 1.0

http://behdadgame.com/index.php?topic=3.0

possible way, if we found will make a way.
« Reply #5 on: October 17, 2013, 12:51:07 PM »
I have no clue what you mean with one memory section. The 3D Rad script is way above the memory level.

You can have only one variable with a certain name per script, but in multiple scripts you can have a variable with that name in every script object.
Rocket Rumble, a 3D Rad puzzle game:
http://www.3drad.com/forum/index.php?topic=9896.0
« Reply #6 on: October 17, 2013, 04:00:58 PM »
Thanks Roberto. ;)
My Game : Star Wars 1.0

http://behdadgame.com/index.php?topic=3.0

possible way, if we found will make a way.
« Reply #7 on: October 17, 2013, 07:19:35 PM »
Hi, Robertoo is right when he says you can
Quote
but in multiple scripts you can have a variable with that name in every script object.
but this statement should be made a little more clear
Quote
You can have only one variable with a certain name per script
The global variable is declared outside the Main(){} structure and must be unique, that is true form what he said above. This can been seen anywhere in the script and doesn't looses its value when the processor leaves the script. It then carries the value back to the script when the processor returns.   

If you declare a variable inside a Main(){int i=0;} that variable can only be used in the Main() and must be unique. It dies when the processor leaves Main() and is renewed when the processor returns to the Main() and the value is lost. If you declare a variable in a function you can use the same name as in a Main() as it unique to that function and dies when the processor leaves the function. It will not influence the value in the Main().

There are other methods where you can declare a variable in a loop for example for(int i = 0; i < 10; i++){'i' can only be seen inside these braces }. This is also unique to that and can't been seen outside the loop. Other methods also operate this way. If you declare a variable inside braces it is also unique to them, example if(a < b ){ int i += b; move( b );} 'i' can't be seen outside the braces.
 
Now back to the original thought you can declare variables with the same name in a Functions or a Methods or Braces {} because they die when completed.

But it is always good practice to have different names for easy recondition of events. like 'int saveCount' for a counter in a file handling function or mouseX for a mouse handling routine etc.

Hope this is of some help.

n_iron
« Reply #8 on: October 17, 2013, 11:24:27 PM »
N_Iron, what I tried to say that variables from different script objects can overlap, so to speak. You can have variables with the same name in two different script objects.
Rocket Rumble, a 3D Rad puzzle game:
http://www.3drad.com/forum/index.php?topic=9896.0
« Reply #9 on: October 18, 2013, 03:55:51 AM »
Quote
In one script:

void Main()
{
   int Var = 10; //Your variable.
   iGlobalStringSet(""+Var,0); //Pretend like your variable is a string.
}

In the other script:

void Main()
{
   string temp; //Create a variable to stock the incoming data.
   iGlobalStringGet(temp,0); //Get the data.
   int Var = iStringVal(temp); //Convert the string back to an 'int'.
}

As (I think) you understood correctly, iGlobalStringSet/Get can only send and receive strings, so you'll have to convert your data to strings first.

I think this way don't work for me. I have 2 Script file.
one script is select car. in this script I can change the cars with right and left arrow keys. when change the cars, SelectedCar variable change Between 1-8.(the Initializing SelecterCar value is 1). Now I add this line to void main like this:

Code: [Select]
int SelectedCar = 1;
/// void Main

void Main()
{
iGlobalStringSet ("" + SelectedCar, 0);
}

in next script I add this code:
Code: [Select]
void Main()
{
   string car;
   iGlobalStringGet (car,0);
   int SelectedCar = iStringVal (car);
}
then when I press a button, save this value. but in the game show only car 1  even if I selected car 5.
My save function work and I test it with other script and can save current selectedcar. but when I use iGlobalStringSet/Get Function don't work good. because I think iGlobalStringGet can't receive data from one script.
My Game : Star Wars 1.0

http://behdadgame.com/index.php?topic=3.0

possible way, if we found will make a way.
« Reply #10 on: October 18, 2013, 07:06:02 AM »
 iGlobalStringSet/Get() does work...
 show your save function...

--Mike
« Reply #11 on: October 18, 2013, 07:12:37 AM »
Code: [Select]
   
   float CarCounter = IN_66;
   int handle = iFileWriteOpen(".\\save\\Save.dat");
   iFileValueWrite (handle, CarCounter, true);
   iFileClose (handle);
   iScriptStop();
My Game : Star Wars 1.0

http://behdadgame.com/index.php?topic=3.0

possible way, if we found will make a way.
« Reply #12 on: October 18, 2013, 07:17:10 AM »
where is SelectCar... i don't see it... you just got SelectedCar from the global string array... where is it being saved...

also...

in addition to showing your dave function i should've asked to see your load function as well...

(i just got your email... please B,  discuss the coding questions here on the forum... i can't respond to your emails for this sorta stuff... i've got some custom security stuff running that'll usually delete this stuff as spam before it ever gets to me...  ok)


--Mike
« Last Edit: October 18, 2013, 07:28:36 AM by Mike Hense »
« Reply #13 on: October 18, 2013, 07:50:10 AM »
OK, Thanks Mike.

I send my email for you.
« Last Edit: October 18, 2013, 08:11:49 AM by behdadsoft »
My Game : Star Wars 1.0

http://behdadgame.com/index.php?topic=3.0

possible way, if we found will make a way.
« Reply #14 on: October 18, 2013, 10:42:40 AM »
I could solve the Problem. Thanks All for help.

But I have a question: how can encrypt save file? because when I save my variable with iFileWriteOpen () and iFileValueWrite () Functions, any use can edit it with notepad and make all efforts are useless.

Thanks.
My Game : Star Wars 1.0

http://behdadgame.com/index.php?topic=3.0

possible way, if we found will make a way.
Pages: [1] 2