• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[SOLVED]Variable Dependencies

C

Cooltrain

Guest
Hey all,

So I've been working on a character change system and have hit (what seems like) a simple issue that I just can't seem to get around. I'll go ahead and give the code first before explaining my issue.

Obj_player_controller : create (this is the object in the room)
Code:
startChar=asset_get_index("obj_Char1");
currentChar=instance_create(x,y,startChar);
currentCharObj=asset_get_index(object_get_name(currentChar.object_index))
The character that is then created would run two scripts, one in the create and one in the step
Obj_Char1 or Obj_Char2, ect create and step
Code:
//create
scr_playerCharInt();
//step
scr_playerCharMovment();
These scripts would then get which character was created ( and is active) in the create event, this is the same for both the create and step

scr_playerCharInt
Code:
/*Char1*/

if (obj_player_controller.currentCharObj=obj_Char1){

//Char1 init code

/*Char2*/

}else if (obj_player_controller.currentCharObj=obj_Char2){

//Char2 init code
}
This is the main area of focus though
Code:
startChar=asset_get_index("obj_Char1");
currentChar=instance_create(x,y,startChar);
currentCharObj=asset_get_index(object_get_name(currentChar.object_index))
My problem is that in the controller create event, the instance of the character is created before currentCharObj has been set. The scripts for the created character create event then check currentCharObj to see which the current character is but it still hasn't been set because both create events are triggering at the same time. Ideally I would just switch the lines so that instance create is last but I need it to be able to call it in the next line for the currentCharObj ? I think I need a way of the scripts not running until the create event in the controller has finished maybe ? For example if i manually set currentCharObj = 1 or 2 before the instance create then it solves the issue (1 or 2 is the object Index for the two current characters ).


Thanks for any help or advice.
 

FrostyCat

Redemption Seeker
The variable dependency here isn't necessary. The best way to deal with it is to get rid of it.

In the setup code, get rid of all of those asset_get_index() calls. If you traced out your code, you can easily see that it is really just this:
Code:
startChar = obj_Char1;
currentChar = instance_create(x, y, startChar);
currentCharObj = startChar;
Same with scr_playerCharInt. obj_player_controller.currentCharObj would have been equivalent to object_index, so just use object_index instead of creating a dependency.
Code:
switch (object_index) {
  case obj_Char1:
    //...
  break;
  case obj_Char2:
    //...
  break;
}
 
C

Cooltrain

Guest
Sorry for the really late reply on this thread. That did the trick. Thanks for the help.
 
Top