I am making a Multiplayer Game with MLAPI. The problem is, i have a ability system so every ability get executed over an ScritableObject. The problem is, if i have for example five Players in the scene all Players are referenced to the same ScritableObject. And so when somebody execute a ability. The other players are also doing it.
So my Question is can I duplicate my default ability ScritableObject so that every player has its own abilities ScritableObject but with the same Values as the default ability ScritableObject. I must do that over Script.
I know i could create a new ScritableObject and then make a method who paste my values. But maybe somebody has a better idea??????????
Afaik you can use Instantiate also for cloning ScriptableObject on runtime! Only you can of course not use any component taking more then the original parameter (which makes sense).
And the same way as ScriptableObject.CreateInstance this will of course not create an asset but only a runtime instance.
Related
I'm kind off a beginner, I was following the tutorial in the unreal documentation for direct blueprint communication, my problem is the following: when I set up a character blueprint so it accesses another blueprint with an object reference I want it so when my character is spawned that instance of my character is able to access the referenced object, currently it generates an error that pretty much says that that particular instance of my character is not the one that has the object reference variable set up, it only lets me assign it to characters in my scene yet these ones aren't the ones that are used to play when I hit play in the editor, the one used to play is set up so it's spawned by the game mode. thought this is probably not the way I would find it very useful to know a workaround to this.
C++
You can use GetComponentsByClass in BeginPlay(), e.g.:
TArray<UActorComponent*> Comps1 =
GetComponentsByClass(UStaticMeshComponent::StaticClass());
Or you can add tags in specified Component, and use GetComponentsByTag:
TArray<UActorComponent*> Comps2 = GetComponentsByTag(UStaticMeshComponent::StaticClass(), FName(TEXT("t1")));
They worked both for Blueprint Component and Native Component.
Blueprint
If there's only one character in level, you can use Get Actor of Class.
If there'are more than one characters, you can use Get All Actors of Class and foreach them.
I'm trying to Save/Serialize a GameObject and its components (with their current values), but I don't think it's possible, any ideas ???
Keep in mind that I don't wanna use an asset from the assets store, I wanna code it myself.
Thanks in advance :)
Sorry I dont have the rep to make a comment. I use two methods to acheive this save state.
First is what programmer seems to suggest, make a class that hosts the property's needed to recreate the object. Component names can be saved as strings, then fetched using
gameObject.AddComponent(Type.GetType(componentName));
If you are doing this alot it may be worth checking out this forum first.Here
You can serialize these classes using the unity serializer(the string names of types will serialise. You may have to make a sub class to handle the values in each object of different type.
This can be a fairly complicated approach so I prefer a file path serialized for a prefab in the resources folder. Any values that need saving can be saved in a serializable class like first approach which is referenced in a script on the saved prefab. This script has a method to apply all details saved in the deserialized class to the prefab when it is created. The details are captured by the script,added to the class and serialized whenever the object state should be recorded.
The exact way this is managed may vary but I have done this myself with he ability to store any values I could need. Even if you use a third party serializer from the asset store you will still have trouble saving entire objects, but you will be able to get enough info to recreate.
I've managed to save the components I want (custom scripts) by saving them in a json file, then serializing them with binary so they can be more secure.
Thanks to everyone who tried to help tho...
For some reason, I cannot change the name of a game object (an avatar) but I need to have 2 different versions of it and activate them in code.
So, duplicating the avatar is not an option, but I still need to create an identical one and make a change in editor mode, so that based on a condition, whether one or the other is chosen.
It occurred to me, now that changing the name is not possible, perhaps I can give the other a different tag and activate the right one based on the selected scenario.
So, I checked out a few manual docs and some forum posts and came up with the following 3 alternatives to place in my Start() but none works:
void Start()
{
if ( GameObject.FindGameObjectWithTag("John") )
gameObject.FindGameObjectWithTag("John").SetActive(true);
if ( GameObject.FindWithTag("John") )
gameObject.FindWithTag("John").SetActive(true);
}
Can someone please help me understand what I am doing wrong and how I can achieve this: find an object that is not active (from among 2 inactive objects with the same name but different tags: one untagged and the other tagged as, say, John)
Couldn't be easier, just assign them to different variables
public GameObject johnCharacterOnLeft;
public GameObject johnCharacterWhoJoinsLater;
void Start()
{
johnCharacterOnLeft.SetActive(true);
johnCharacterOnLeft.Whatever(true);
johnCharacterOnLeft.Whatever(true);
johnCharacterOnLeft.Whatever(true);
johnCharacterWhoJoinsLater.SetActive(false);
johnCharacterWhoJoinsLater.Whatever(false);
johnCharacterWhoJoinsLater.Whatever(false);
...
}
Note, as a rule when you are learning, do not use Find. It's that simple. Use public variables and drag to connect in the inspector.
(If anyone reading does not yet know how to drag to connect in the inspector, check any basic tutorial.)
If you do want to find all objects with a certain tag:
GameObject[] allCats;
allCats = GameObject.FindGameObjectsWithTag("cat");
foreach (GameObject g in allCats)
Debug.Log("I found one, name is: " +g.name);
If you want to find one of those with a certain name you have to search through them all..
GameObject[] allCats;
allCats = GameObject.FindGameObjectsWithTag("cat");
foreach (GameObject g in allCats)
{
Debug.Log("I found one, name is: " +g.name);
if ( g.name == "Felix" )
{
Debug.Log("I did find Felix the cat");
g.Whatever ...
}
If you do want to find all objects with a certain name...
It's very important to understand that
...you just can't do that in Unity - it simply makes no sense.
Life's life that - Find is a hack you should never really use anyway, other than in rare situations. If you (for some reason?) need to "find" all game objects with a certain name, you have to simply look through every single thing in the scene (which is not particularly easy to do). You can easily google this. But there is, absolutely, no reason you would ever do this.
Note that, just one example .. say you had a number of "cat" thing in your scene. Very simply - nothing more complicated than this - you would have them all in the same "folder". (That is to say, all under the same holder game object.) That is just one incredibly simple approach to utterly avoid the sort of problem you're having.
Again there literally is no way to find "all game objects with the same name" - the Find call is a hack that should never have been in Unity; they explicitly don't have
And most importantly don't forget...Find doesn't work if inactive!
The Find call is a hack that should never have been in Unity: always remember it does not even work on inactive objects. Inasmuch as they threw in a "find name" concept, it's probably for the best that it doesn't work on inactive objects: but the whole thing is a shambles - just don't use "find", ever.
Particularly as a beginner just drag in the inspector - it's honestly that easy. (And again, in almost all cases your stuff will start inactive, so of course you have to do that since you can't "find" inactive stuff.)
(And a note - don't forget that if you're simply finding a "boss" type script, say your Scores or SoundEffects, never use find. Just do this easy idiom. )
Ive seen a few questions similar to mine but I can't get it working for my particular need.
I have 2 scripts both in unityscript, EnemyHealth.js and BasicAI.js . In EnemyHealth.js I have an int variable called Health. I want to pass this over as a variable within BasicAI.js so that I can check whether the enemies health is below a certain range. Both scripts are attached to a game object 'Enemy'.
The second part of my question is, if var Health is below a certain amount (say, 20) I want a function flee() to be called. How would I make the enemy turn away from the player and proceed to move away ?
Thanks.
To answer the first part of your question, you'd want to use (GetComponent)[http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html]. What this does is lets you get other scripts on a GameObject of a particular type. In your case, you'd be doing something like:
var enemyHealth = gameObject.GetComponent(EnemyHealth);
Then you'd just need a public way to access the variable called Health, whether that variable is public or there is some sort of getter that is public (if you don't want BasicAI altering health, then you'd want to go the getter route instead of just making the variable public).
Now, as to make the enemy flee, that's a pretty complicated question that involves a number of things and I think it might be a bit too broad to answer in general. Might want to look up some basic AI scripting to get a handle on those concepts.
There is various ways to call another script in Unity. Either GetComponent as Brett said but I would use FindObjectsOfType (http://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html) in your case.
The AI question is pretty unclear. Do you already have a AI that does something? Just to call the flee method just type what you said in your question (probably in Update(http://docs.unity3d.com/ScriptReference/MonoBehaviour.Update.html).
if(Health < 20){
flee();
}
I am making a graphically simple 3D game in C++ using DirectX. The main problem I am having is with how to structure some things efficiently. Right now I know my goal for certain areas but not how to ideally perform them.
For instance, right now I am storing all meshes and textures in an Asset class with enumerated definitions pointing to each asset. All meshes and textures are loaded when the game starts by creating an Asset object and initializing it. From there I load meshes and textures to objects by assigning the pointer given by the Asset object. Is this the best way to go about this?
A harder topic is that of items. For the sake of argument I am going to say we are dealing with scenery. Right now my scenery needs the following:
A name
A mesh
Textures (optional)
Flags (such as Destructible, Flammable, etc.)
Status (such as Burning, Locked, Cursed, etc.)
Abilities (things the object actually does, such as becoming damaged when burning)
The first 5 things on this list are simply variables. But abilities are bothering me. The first 5 can all exist in one item class. But what I do not know how to do is somehow attach abilities to the object.
For example; I want the object to have the "Fire Nova" ability. At the start of each turn, the object damages anything near it.
For me, this means each object would need a "Trigger_Phase_TurnStart()" or some similarly named method and would then need a new "Fire Nova" class with its own unique action for that trigger and its own extra variables (Damage, Range, etc). So now I have the base object class and a new Fire Nova class that inherits from it.
But now what if I needed an object that has Fire Nova and Frost Nova and also Slowing Aura and other such abilities. Basically I need a way to add triggered effects to an object without needing a new object class for each of them.
So by the end I would be able to have say:
(pseudo code of the object's components)
name = Elemental Orb
mesh = "Mesh_Sphere"
textures[] = "Tex_Ice", "Tex_Fire"
flags = OF_Destructible
status = SF_Cursed | SF_Burning
abilities[] = Fire_Nova, Frost_Nova, Slowing_Aura
And this orb would simply be the object class with all these attributes. The object would then activate each stored ability's trigger at the appropriate turn phase for any actions to perform.
Right now I am thinking I might need to make a class for each ability possessing every turn-phase or action trigger (inherited from a base ability class) and have them perform the appropriate action when the object calls them from it's array of abilities. Would this be the best way to do this?
As a separate issue. Some flags would require additional variables that would otherwise be unnecessary. For example, Destructible would mean the object would have health whereas without the flag it wouldn't need it, or an Openable item would need an array of contents. What would be a good way to ration these variables to when they are needed? I do not need every wall to have health and an empty contents array for example.
Finally. Assuming the bullet-listed attributes above are all an item needs, how might you suggest I best store them? To clarify, I would like to be able to write:
CreateItem(ITEM_CHAIR);
and have this return the created object with name, mesh, textures, flags, status and abilities. What structure might be suitable for achieving such an end effect?
To summarise:
Does my current Asset storage seem feasible?
What is the best way to create abilities to attach to the object class without making numerous separate object classes?
Is there a way to limit the variables (or at least memory usage) when the associated flag is not present?
What structure or format would be best for storing fixed item definitions?
Sorry if this is a little long winded. If you cannot answer all the questions then an answer to one would still be appreciated. Question two is probably the largest priority for me right now.
Thanks for your time :)
I want only answer question 2 and it looks like you need a decorator pattern. In OOP a decorator pattern is useful when you have many ingredients and wants to decorate an object, for example a pizza. Unfortunately you need to create for each ingredient a separate class hence I think you have the right approach. The good thing is that with the decorator pattern you can wrap the object over and over with abilites classes and call only one method at the end to do all the stuff.