Save current state at run time [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I have built a game in Unity3D where the player can place models, rotate them and scale them.
Now what I want is to add an option to save the current state.
What I need is to save for every model is simply the scale, position, rotation and the model's mesh.
I know how to save all the properties except for the Mesh. I planned to save the data in JSON file but it can't save the Mesh nor the GameObject.
I have reference to all the instanced models so there is no need to find them.
Edit:
To sum up, I am looking for a way to save the selected models so I would know which models to load when the user wants to load the file.
But I can't serialize the GameObject nor the mesh, and therefore using JSON or binary file doesn't work. All I have been able to save so far is the position, rotation and scale of each models.
How can I save the selected models¿

The mesh is not generated on runtime right? He has a collection of models that you have as prefabs that he can place. So just save as a string or enum which model each position has, then to load the saved scene you instantiate the correct prefab of the model according to the save at the saved position and rotation.
public class PrefabHolder : MonoBehaviour //assign all necessary prefabs here, also used for logging on mobileDebugText
{
public static PrefabHolder instance;
public GameObject prefabXYZ;
private void Awake()
{
instance = this;
}
Acess any model you want from any script with Prefabholder.instance.prefabXYZ

Related

What's the "OOP way" to access data in a contained class object, from another contained class object? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to create a replica of the card game "Monopoly Deal" in C# Console, for educational purposes. I have a problem with my class design that I'm not sure how to resolve. I have two classes, Game and Player. Game controls the state of the game, and contains a list of Player objects.
Currently the way it works is that the Game class controls everything. The Player is basically a container class: it has all of the cards in the player's collections. The decision-making is done by the Game class because it can see all the players, and therefore knows which moves are possible.
For example, if Player 1 wanted to steal a card from Player 2, my program would have to first verify that Player 2 has any cards to steal. Player 1 doesn't know that, because it's just a single object in a list of Player objects. So the Game class has a method which checks all possible actions that a player could make, asks the player to choose which action they want to make, and then performs that action for them.
My problem: I don't think that this is good OOP class-design. I think that I should be delegating the decision-making to the Player class, not the Game class - otherwise my Game class will become bloated with lots of methods that relate to player actions and decisions, and not the game state itself. I don't know how to achieve this, however.
Possible solutions:
Create a GameState object containing just a limited view of other players' cards, and pass that to the Player object (Seems the easiest way but I guess it's creating redundant data which is a waste of memory.)
Pass a reference to the whole Game object to the Player class (I could do - it's not like I'm going to program by ComputerPlayers to cheat - but doesn't feel like good OOP design either)
Give up, and put all of the game logic (including the computer AI) in the Game class, and accept that Player will just be a data container, not a decision maker.
Redesign my classes a different way.
Something else I've missed?
Is there any obvious solution here? Thank you.
EDIT: I've attempted to trim down excessive detail in the question as it was closed for "Not enough focus". If further revisions are needed, please let me know.
How about creating an action-class that represents an action that you can take in the game. The Game then just has two methods Game.verifyAction(action) and Game.executeAction(action). The Game contains all the data necessary to verify and execute actions.
Next, treat players just as data containers, meaning a player does not think it's just contained in the state. A player might contain data like it's score, it's id, etc. To actually think you have an agent class that is responsible for choosing actions. So Agent.think(Game) returns an action that the agent wants to take in this state.
Now it's easy to move the main-loop outside the Game-class, you can also replace an Agent easily with an GUI that returns a valid action once the human chooses an action.

how do I use an object in all scenes in UNITY2D or how do I research it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am working on a project in UNITY 2D. In my Shop Scene there is a money bar that shows the money how much i have and also there is a counter that count the time. When I change the scene from here to another scene, also there should be a time counter and money bar. I can add manually. However, when I change the scene counter starts from 0 again. For money bar, I can pull the money from another scene. However, there will be more scene. It should be more complex. Is there a way to do a general object that runs in every scene without interrupt. Also, how can I research that more clearly?
Try adding DontDestroyOnLoad
after variables write
//Your variables here
public (classname) instance;
void Awake(){
if(instance == null){
instance = gameObject;
}else{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
}
You don't create objects for use to same static work process in different scenes.
If you want to use same variable value for different scenes just simply use PlayerPreferences class. This class allows you to access the variables you have saved in the whole project regardless of the scenes.
Please check instructions...
Example:
Imagine your method needs an integer value and you want to use different scenes same method.
At scene1 =>
PlayerPrefs.SetInt("variableName",(int)value)
At scene2 =>
Get this value with PlayerPrefs.GetInt("variableName") and use it for your method.

Best practice for house floorplan scene changing [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a house floorplan in scene A. Players can click a room on the plan and go into that specific room. What they find there will be an image of that specific room and some character stats about that rooms current occupant (not the player).
At the moment, I handle this with a set of public variables in a gameobject instance "target room 1" "current occupant Henry" and use those to build a single scene.
I was wondering how you would go about this. Is there a better way? Is it better to have one scene for each room? How would you get players to a situation that they see the room and the specific occupant?
Well, that depends a lot on the game you have and how would it be easier for you (and/or your team) to manage it.
You can make each room a different scene and just reference it and load it on click. That would work best if each room is complex. Programming wise - If it has whole set of information and states to take care of it would be harder to manage those systems for every room in a single scene. Design-wise - if it's a complex room with lots of object making it a different scene would be easier and cleaner for you to work with. Drawback is, well the frequent scene loading. That also means that if you need some state to persist across different rooms you'd have to figure out how to do it across scenes.
You can make each room a game object and just enable it and move it in the field of view of the camera. That would work best for simple scenarios where there isn't too much complexity about the functionality and design of every room. Also you get the benefit that you can easily just have something like Room Manager that can make interaction between rooms easier.
You can make a single room object in the scene and populate it with data at runtime when you select a room from the plan. That is a very good option if all the rooms are sort of homogeneous and just have some variables that need to be changed - perhaps an image, a name or certain prefab that has to be instantiated. A nice structure for that would be to have perhaps a RoomData scriptable object with all the data in it and a RoomObject prefab that you put in the scene. The RoomObject will take in the RoomData and display it. That way you can quickly make different rooms by just creating new data files. You can also make different types of prefabs that handle the data a little diffrenetly - perhaps if you want a BedroomObject that has a different layout or something like that

Randomly generating obstacles in Unity [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Currently I'm creating game in which whole level is generated when player moves. I have working script for that, but now I want to generate obstacles in front of the player. My idea is to add empty object to my terrain segment(these are dynamically generated) and then instantiate random obstacles at their position at runtime. The only thing i don't know is how this script should pick random empty object(empty object is child of the segment) to instantiate this obstacle? The script which generates level is attached to player.
This gives you an idea of how to do it.
Spawns a random game object to one of the empty gameobjects around your terrain.
//Add the random objects in the inspector
public List<GameObject> gameObjects;
//Spawn points around the terrain
public List<Transform> emptyTransforms;
foreach(var item in emptyTransforms){
var objToSpawn = gameObjects[Random.Range(0, gameObjects.Length)];
var objSpawned = Instantiate(objToSpawn, emptyTransforms[Random.Range(0, emptyTransforms.Length)].position, Quaternion.identity);
objSpawned.transform.SetParent(newParent);
}
Use the Random.Range function to calculate random values. Then make the values within bounds of where you want obstacles to spawn and create a Vector3 with those values. Now the obstacles are spawned in a random area.
If you have a fixed list of possible positions, just store them in a public variable in the script. If your empty gameobjects are the positions, create a public GameObject[] positions;, populate it and use something like Random.Range to pick one of it.
Also I suggest you have a look at item pooling and don't instantiate new objects all the time (and probably destroy "old" ones). That is better for your performance.

Best way to attach a string variable onto a moving texture, and have it drawn? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Currently working on a game (similar to a space defender game with random enemy spawns) and need the enemies to have a random.next attached to label the enemy.
However I'm having trouble figuring a way to do so, since the enemy is also moving on its Y axis at a speed of 4; "position.Y = position.Y + 4. Also whats making this difficult is the fact that i need my bullets that collide with the enemy to somehow also delete the label, and refreshing the label with a new random number on the next spawned enemy.
Sadly I'm currently away from my work station, so i can't present any code, but as of this moment, the way I'm managing my enemy spawning is through a list, it loops to check if the list has < 5 in its .count, and if so, add another enemy with a random Y, and X position. My method of reseting an enemy after it reaches the bottom of the screen, is that once it hits Y == 950, the enemy is set to "isvisible = false", the list is then looped in a check ,where if an enemy is found with isvisible = false, it is removed from the list.
You can say that the list is slightly useless, but I'm currently using it to limit the amount of spawns on screen.
see Drawing Text with a Sprite, I would just create a Label string property in your enemy class set it to whatever you need to in its constructor, and then when you loop over your list it to draw it also draw the text that you assign when you create the enemy at whatever offset from the sprites location.

Categories