- Home /
Adjusting location on save to avoid infinite loop.
Here's the situation. I have a trigger script that saves your game and loads another map. That works just fine, the problem is that it saves right on the trigger, so when you go back to the previous map, it loads the other map. I wanted to save an offset so you would transform to one or two spaces before the trigger primitive. Only it's not saving for whatever reason.
public void PlayerAdjust(float CoordX, float CoordY, float CoordZ, float amount, string axis) //float Rotation,
{
Debug.Log("X " +CoordX);
Debug.Log("Y " +CoordY);
Debug.Log("Z " +CoordZ);
switch(axis)
{
case "x":
CoordX=CoordX + amount;
Debug.Log("calc X" +CoordX);
break;
case "y":
CoordY=CoordY + amount;
Debug.Log("calc Y" +CoordY);
break;
case "z":
CoordZ=CoordZ + amount;
Debug.Log("calc Z" +CoordZ);
break;
}
Save();
}
Here is the player adjust script. Do I need a return type?
Sorry but i can't understand your question, can you formulate it a little more?
The maps are different scenes ? What does the Save(); do ?
Remove the switch-statement, is it really needed? You didn't provide any info about the player or what's being saved.
public void PlayerAdjust(Vector3 offset)
{
player.transform.localPosition += offset;
Save();
}
Answer by Gilead7 · Aug 14, 2016 at 08:02 PM
Here is the Save function:
public void Save()
{
var Personal=new JSONObject
{
{"Name" , PlayerName},
{"Gender" , Gender },
{"Body Type" , BodyType},
{"Alignment" , Alignment},
{"Guild" , Guild },
{"Rank" , Rank },
{"Form" , Form},
{"Health" , Health},
{"Hunger" , Hunger},
{"Thirst", Thirst},
{"Intoxication" , Intoxication},
{"Fatigue" ,Fatigue},
{"Encumbrance" , Encumbrance},
{"Experience" , Experience}
};
var Stats = new JSONObject
{
{"Constitution" , Constitution},
{"Strength" , Strength},
{"Stamina" , Stamina},
{"Dexterity" , Dexterity},
{"Intelligence" , Intelligence},
{"Wisdom" , Wisdom},
{"Focus" , Focus},
{"BattleSpeed" , Battlespeed}
};
var Vitals = new JSONObject
{
{"Life" , Life},
{"Energy" , Energy},
{"Endurance", Endurance}
};
var Skills = new JSONObject
{
{"Light Melee Weapons" , LightMeleeWeapons},
{"Medium Melee Weapons" , MediumMeleeWeapons},
{"Heavy Melee Weapons" , HeavyMeleeWeapons},
{"Light Ranged Weapons" , LightRangedWeapons},
{"Medium Ranged Weapons" , MediumRangedWeapons},
{"Heavy Ranged Weapons" , HeavyRangedWeapons},
{"Light Armor" ,LightArmor},
{"Medium Armor" ,MediumArmor},
{"Heavy Armor" , HeavyArmor},
{"Holy Armor" , HolyArmor},
{"Holy Weapon",HolyWeapon},
{"Wicked Armor" ,WickedArmor},
{"Wicked Weapon",WickedWeapon},
{"Block", Block},
{"Parry" , Parry},
{"Evade" , Evade},
{"Critical Hit" ,CriticalHit}
};
var Money = new JSONObject
{
{"Copper" , Copper},
{"Silver" , Silver},
{"Gold" , Gold}
};
var Location = new JSONObject
{
{"Map", Application.loadedLevel},
{"X" , CoordX},
{"Y" , CoordY},
{"Z" , CoordZ}
// {"Rotation" , Rotation}
};
Debug.Log("Saved X" + CoordX);
Debug.Log("Saved Y" + CoordY);
Debug.Log("Saved Z" + CoordZ);
var Player = new JSONObject
{
{"Personal" , Personal},
{"Stats" , Stats},
{"Vitals" , Vitals},
{"Skills" , Skills},
{"Money" , Money},
{"Location", Location}
};
Debug.Log(Player);
File.WriteAllText(Application.dataPath + "/Player.json", Player.ToString());
}
I wanted to set it up so that depending on where you were facing it would move you off the trigger on either x, y, or z. That's what the switch/case was all about. Also wanted to swing the player around so they are facing 180 degrees from the trigger(door) as if they came out. If that makes more sense.
Hey Guys! I solved my own problem. What had happened was that the trigger was entered the second before the transform would have sent the player away, thus loading the other map/level. No matter how far away I set the coordinates, it didn't matter because the trigger was tripped.
Solution? I put in a new canvas and panel so that when the trigger goes off it asked you if you want to enter. Click on yes, level loads, click no, window closes.
When you exit the other level and return to where you were, the question pops up again. Just select no and you are golden!