Jump to content


Photo

Dealing with String/Real values and variables.


  • Please log in to reply
6 replies to this topic

#1 NikaB

NikaB

    GMC Member

  • New Member
  • 111 posts
  • Version:GM8

Posted 13 September 2011 - 05:50 AM

Hi, GMC !
The main question in this topic is Can a string-containing variable be equal to the string NAME of another variable ?

I explain :

At the beginning of the game, I've got global.BGM1=sound_add(...). So it sets to global.BGM1 a real value, changing every time you boot the game again.
In the game rooms, obj_music has a mus variable which contains the music to play. (eg. mus=global.BGM1; or mus=global.BGM2 etc)
At checkpoints, every object's data is saved to a file (not built-in function). So the current music is saved as obj_music.mus, which equals global.BGM1 (or global.BGM2 or something) which equals to a value which, as said before, changes when I boot the game again. So when I rebooted the game and loaded the data saved previously, I obtain the value saved in the last built. So Values do not match. So nothing is played.

Now I was talking about strings and reals because the first idea the came to me was to set the variable which had to be saved as the STRING NAME OF THE VAR, not its real value.
And then load it as a string and transform it into a real number so it can recognize the var.

Please suppose me another way to do if this one is impossible. Otherwise, explain how can I do this.
Thanks !
  • 0

#2 zehevi

zehevi

    GMC Member

  • GMC Member
  • 637 posts
  • Version:GM8

Posted 13 September 2011 - 06:06 AM

Not sure what you mean by "STRING NAME OF THE VAR" but since you are using 'sound_add' the value of the global variable changes, but not the name of the global variable.
This means that everytime you load up the game it should rewrite the global variable with the new sound (using sound_add) and so, the obj_music will try to play sound "name as id" stored in the global variable.

Hope this helps.
  • 0

#3 NikaB

NikaB

    GMC Member

  • New Member
  • 111 posts
  • Version:GM8

Posted 13 September 2011 - 10:46 AM

It was my basic idea but I don't know how can I make it.
Basically, I need something like pointers in C++.
Or another way ?... :confused:

Edited by NikaB, 13 September 2011 - 10:53 AM.

  • 0

#4 NikaB

NikaB

    GMC Member

  • New Member
  • 111 posts
  • Version:GM8

Posted 13 September 2011 - 08:04 PM

Bump.
Anyone ?
  • 0

#5 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 14 September 2011 - 01:18 AM

You'd better define your own set of constants those never change. For instance:


MUSIC_ID_1 = 1
MUSIC_ID_2 = 2
MUSIC_ID_3 = 3
...


Use those values to indicate which music to play, and turn the value into actual sound index when necessary.
mus = MUSIC_ID_1;
switch (mus) {
    case MUSIC_ID_1: sound_loop(global.BGM1); break;
    case MUSIC_ID_2: sound_loop(global.BGM2); break;
    ...
}

Or you can just store sound indices into an array and use "mus" for the array index.
global.BGM[1] = sound_add(...);
global.BGM[2] = sound_add(...);
...
mus = 1; // to play BGM1
sound_loop(global.BGM[mus]);
In either way, you can simply save and load the value of mus and it will play the correct sound no matter if the value of global.BGMx changes.
  • 0

#6 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9259 posts
  • Version:Unknown

Posted 14 September 2011 - 03:22 AM

I suggest the array method torigara proposed, but if you really want to use the name of the variable to get its value, check out the variable_local_get(...) and variable_global_get(...) functions.

-IMP
  • 0

#7 NikaB

NikaB

    GMC Member

  • New Member
  • 111 posts
  • Version:GM8

Posted 14 September 2011 - 05:27 AM

Thank you, I'll check all this out !
It seems very promising.
Thanks !
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users