i’m trying to access a int value that i’m saving to my game slot at the beginning of my game (without any problem) and then display that value at the UI, but for some reason when i try to cast my load game from slot to my custom PlayerStage_BP (parent class SaveGame) the cast always failing and i don’t know why. But for some reason i can save/get strings without any problem.
Note: My custom class contains only a integer variable named currentStage
Here i’m trying to get the value:
And here i’m saving the value (without any problem, the slot is creating without problems)
Related
I Have a variable that updates every time i move my cube in the level blueprint , now i want to access this variable from multiple class blueprints , what do I do , I tried casting to gamestate but didn't succeed , I am really new to ue4 if you could explain in details please
edit: sorry for not adding details ,
The var I want to access is an integer named cube_side that tells me what side the cube is on every time I move , all of this happens in the level bp , I want to access this variable to see what side the cube is on from other class blueprints ->
here are some details in a picture
I know it's not good to code everything in the level blueprint , but it's too late now , I only need to transfer the var cube_side to other class blueprints so the other object can change depending what side the cube is on.
Create an Actor Class for your logic/functionality. Then use Get all actors of class (choose your class) -> Get a copy -> get variable
Communication with the level blueprint is rather tricky in UE4, since they are not as persistent as e.g. the GameMode, and therefore shouldn't be accessed directly (Imagine older games like Final Fantasy where a new level was loaded every time you stepped outside a boundary, so relying on it could potentially break your actors or crash the game due to nullptrs).
It's a little hacky, but works:
Move the variable inside the cube-blueprint. Add an event dispatcher to the cube, if it is moved, call it and pass the variable in.
Select the cube in the editor, open the level blueprint, right-click, "add reference to selected actor" (the cube must be part of a blueprintclass, not only a static mesh dragged in, though), and bind the event dispatcher inside the Level BP.
Create a function inside every blueprint that needs access to the variable, which does whatever it should do, depending on the variable.
The custom event of the Level Bp (that was bound to the Event Dispatcher of the cube), needs references to all actors that have to work, when the variable changes and call each Actors function (you can get the references like you got the one from the cube)
Then, every time the variable changes, the Level BP is notified, the custom Event is executed and this custom event calls all the actor's functions.
Event Dispatchers explained
This is a huge wastage of functions/code, since you only need it for this one level and may never use it again. Avoid this in the future, by not relying on the level BP so much.
You can use GameStateBP to create and store all variables that you need in game, in GameModeBP create functions to get and set this variables via Get Game State function and then function Cast To GameState and then logic. After that from any blueprint access it using Get Game Mode -> Cast to you GameMode -> use your function to set or get data from GameState.
After realizing that you can't really use UnityEvents in Scriptable Objects (they don't read global variable values correctly). I had to move to another solution for where to store my Dialogs.
The best solution I could find was to just store them in the corresponding NPC-prefab. This is really convenient. However this leaves a bad taste in my mouth, it just feels wrong to store data in a prefab like this. Is it a bad practice?
For example if I were to refactor something in the DialogObject, everything would be lost.
Since I can't seem to successfully store UnityEvents anywhere (can't serialize them as Json and as mentioned Scriptable Objects don't seem to handle them well) I feel like this is the only solution if I want to be able to use the Editor to create Dialogs.
Just checking here first, is this stupid? is there another way?
I am trying to save a List of this:
[System.Serializable]
public class DialogObject {
public List<PageData> conversations = new List<PageData>();
public UnityEvent postEvent; //Invoked on last page of Dialog
}
I would say yes, prefabs are meant to be a template to create new items of a type. You are trying to save data states.
Even though you cannot serialize the UnityEvent, you could serialize its content.
If you assign via inspector, you can use https://docs.unity3d.com/ScriptReference/Events.UnityEventBase.GetPersistentMethodName.html
But then you would not have problem of storage if you know from the start where it goes.
Second solution is when you assign the method to the UnityEvent, store it.
void AddListener (UnityAction ev)
{
MethodInfo mi = ev.Method;
// Use mi
postEvent.AddListener(ev);
}
With the mi reference, you have the name of the method, its type, the object it belongs to and anything you need.
I was trying to follow instructions from Learning Swift book (creating note taking app) by B.A. Paris & Co, but faced with the following problem with binding. I am mostly practicing iOS programming, so binding concept is new for me.
Steps I made (tried both xcode 9 beta 5 and 8.3.3):
Create OSX Cocoa App (not using storyboard, document based app – on, document
extension “test”, don’t use core data)
Add “var text = NSAttributedString()” to Document.swift
Add a NSTextView to Document.xib
In Bindings inspector of NSTextView setting “Attributed String” to File’s owner “self.text” (Model Key Path)
And I see exclamation mark with notion “Xcode cannot resolve the entered key path”
Build is successful, but when I run it says “2017-09-03 22:17:40.739643+0200 test3[6017:424072] [<test3.Document 0x6180000c3410> valueForUndefinedKey:]: this class is not key value coding-compliant for the key text.”
I tried to control drag from Xib to Swift, it warns that “Xcode cannot locate the class Document in the current workspace”.
I tried to convert to workspace instead of proj, checked the file owner, checked the stackoverflow threads witch relate to the error – but they mostly concerned about some connection made by mistake or non actual connections (I can delete the connection, I know what connection is wrong, the question is how to make it right). So far could not find solution.
Thanks in advance
You need to declare the text property with the #objc attribute to make it accessible via dynamic dispatch like Key-Value Coding.
Also, because you want modifications of the property to be observable via Key-Value Observing (for Bindings), you need to tell Swift to always dispatch modifications of it dynamically. So, you need to use the dynamic modifier on the declaration, too:
#objc dynamic var text = NSAttributedString()
This question already has answers here:
How to find inactive objects using GameObject.Find(" ") in Unity3D?
(7 answers)
Closed 5 years ago.
I have a game object that at the beginning of the game is disabled. Then I want to enable it later via script. So far I have tried to use the following code:
GameObject.Find("name").SetActive (true);
The problem is that as the game object at the beginning is disabled when I do
GameObject.Find("name")
I get null. So I don't know how to activate it. It's a canvas.
You can create a class variable in your script, and then in the Unity scene editor assign your disabled object to that class variable.
For example:
In your script file, create the class variable:
public GameObject objectToEnable;
Then, in the scene editor, the "objectToEnable" variable will appear on the Script component of the object containing your script. Simply drag your disabled object to that variable (in the scene editor) to set the reference.
Once you do that, at runtime you can just do:
objectToEnable.SetActive(true);
The reference should still be valid, even if the object is disabled.
You could first save the object in a variable, for example you first declare a variable that will hold the object
GameObject tempVar; //this line goes outside any function, not in Start or Update
Then in the Start or Awake function you find the object and save it
tempVar = GameObject.Find("name"); // this line goes in Start, just before you disable the object
tempVar.SetActive(false); //now the object is disabled but saved in a variable with which you can access it at any time
Of course, this code assumes the object is NOT disabled before the game starts.
I have a problem with Lua and I don't know if I going in the right direction. In C++ I have a dictionary that I use to pass parameter to a resource manager. This dictionary is really similar to a map of hash and string.
In Lua I want to access to these resource so I need a representation of hashes. Also hashes must be unique cause are used as index in a table. Our hash function is 64bit and I'm working on 32bit enviroment (PS3).
C++ I have somethings like that:
paramMap.insert(std::make_pair(hash64("vehicleId"), std::string("004")));
resourceManager.createResource(ResourceType("Car"), paramMap);
In Lua want use these resources to create a factory for other userdata.
I do stuff like:
function findBike(carId)
bikeParam = { vehicleId = carId }
return ResourceManager.findResource('car', bikeParam)
end
So, sometime parameter are created by Lua, sometime parameter are created by C++.
Cause my hashkey ('vehicleId') is an index of a table it need to be unique.
I have used lightuserdata to implement uint64_t, but cause I'm in a 32bit enviroment I can't simply store int64 in pointer. :(
I have to create a table to store all int64 used by the program and save a reference in userdata.
void pushUInt64(lua_State *L, GEM::GUInt64 v)
{
Int64Ref::Handle handle = Int64Ref::getInstance().allocateSlot(v);
lua_pushlightuserdata(L, reinterpret_cast<void*>(handle));
luaL_setmetatable(L, s_UInt64LuaName);
}
but userdata are never garbage collected. Then my int64 are never released and my table will grow forever.
Also lightuserdata don't keep reference to metadata so they interfere with other light userdata. Checking the implementation the metadata table is added in L->G_->mt_[2].
doing that
a = createLightUserDataType1()
b = createLightUserDataType2()
a:someFunction()
will use the metatable of b.
I thought that metatable where bounded to type.
I'm pretty confused, with the current implementation lightuserdata have a really limited use case.
With Python you have a hash metafunction that is called anytime the type is used as index for a dictionary. It's possible to do something similar?
Sorry for my english, I'm from Italy. :-/