Is it possible to create a function that returns a different struct based on an enum input in Unreal? - unreal-engine4

So I am working on a data driven game in Unreal and essentially I am working more on the back end trying to make it easier for the objects to get all of the relevant data. I was really trying to create a singular function that by passing in a type I could return a different output but, from what I've tried in the editor this doesn't seem possible.
I get the error: The type of Out Struct is undetermined. Connect something to Return Node to imply a specific type. I was trying to use a wildcard as an output but, it doesn't seem to be able to do that. Any insight on this problem would be greatly appreciated.

As mentioned in comments, just make base struct and inherit your *Config structs from it. In return you will send base struct.
You can read more about this solution here:
https://en.wikipedia.org/wiki/Factory_method_pattern

Related

Swift: Access struct/class variables by string name

I would like to access a variables by its literal name in string form.
So far I've found two ways:
https://stackoverflow.com/a/51090177/2330482 which doesn't let me write to the variables
Doing https://stackoverflow.com/a/44461705/2330482 which crashes the app if the key is not found (I don't want to run that risk). If there is a way to check if the key exists first, that would likely resolve my problem. I could combine this with Mirror in the first linked answer to get all keys (and therefore know they exist) before accessing them via .value(forKey) but this seems a bit unsafe. Is there another way to catch the crash if there is no key, or another way for NSObject to first make sure the key doesn't exist?
The reason why I want to do this is to make a struct fallback to default values if an incomplete JSON is fed. If I'm able to safely access variables by their name I could probably solve this problem without having to write Codable implementations for each variable in the struct.

Postico GUI: How to setup an enum data type

I'm using Postico's GUI interface to define a table, but I need an enum data type for a column. I'm not finding anything in the documentation or online that clearly denotes how I setup the enum data type for use. What I do find are various changelist statements about improved support for enums, but no details on how to actually set up that type.
I've tried putting in the Type column both of the following, hoping it would "trigger" or define it automatically...
enum
enum ('Choice1', 'Choice2')
... but both were rejected as invalid. I know that theoretically, I should be able to name and define the enum somewhere else and then use the name I defined in my data type here. But where/how do I define that in Postico?
So I have not found a way to directly define it in the GUI (which is what I was wanting to know). If someone knows how, post here and I'll accept that answer. In the mean time, execute a SQL Query in Postico's query interface
to define the type, like so:
CREATE TYPE your_type_name AS ENUM ('Choice1', 'Choice2');
After that, then the GUI interface allows you to set the type on a column.

SwiftUI Control flow build elements

I recently started playing with SwiftUI and though there are some good things too it, so far I am very disappointed. Especially by the fact that it makes it really easy to create large, messy looking code and reminds me of XML ( or HTML ) structure of sorts.
Anyhow, I am running into the following problem that I can't find a solution for anywhere.
I need to draw certain rectangles ( or shapes ) at run time, think about it as generating a horizontal bar-chart in a way. Because the number of rects is dynamic, I need to generate this in a method, however I am getting an error from the compiler with any method I've tried so far.
Whenever I try and use some sort of flow control, like 'for' or 'while' in order to generate elements inside the:
struct ContentView: View {
or :
var body: some View {
I either get:
Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type
or
Closure containing control flow statement cannot be used with function builder 'ViewBuilder'
What would be a better approach for drawing dynamic elements with SwiftUI.
I've seen a ton of examples on dynamic lists, even though that was covered by Apple already on day 1, but nothing that would be about custom elements or if you wanted to create a dynamically set number of HStack or Vstack, that are not formatted as a List.
Thank you!
ViewBuilders are special things and you cannot use certain control flow statements in them.
You can call out to separate (private) functions to do the appropriate control flow and produce the appropriate views.
See this blog post by John Sundell on the Swift 5.1 features that power SwiftUI: https://www.swiftbysundell.com/articles/the-swift-51-features-that-power-swiftuis-api/

Structure in a single Swift file?

It might be basic and wrong but after understanding structures I can't understand where to practically put it.
Inside some class call it Main, I would like to encapsulate a set of variables for dimensions.
I know I can just do :
struct Dimensions{
var w:Int
var h:Int
}
class Main
{
//do things using the structure
}
But since i have a lot of variables and i want it clean , I would like to create a new Swift file and put it inside
So inside a file called Dimensions or else :
import Foundation
struct Dimensions{
var w:Int
var h:Int
}
then the structure is visible to anyone, without even using the Swift file name.
A few questions to ask :
It seems like a bad idea - why ?
How is it different from a Singeltone to share data between classes? (value type?)
What is the right place to put the structure outside the Main class to get some clear code ?
Should I make one file with many not related Structs ?
then the structure is visible to anyone
That is not true. Since your struct is not marked public, only code in your module can access it. Even if you write it in one single file, it is still accessible anywhere in your module.
without even using the Swift file name.
The reason why you are saying this might be because in other languages, you need to import a header file or something like that if you want to use something from another file (I'm not an expert in "other languages"). But Swift organises its code in units of modules, not files.
It seems like a bad idea - why ?
It is not a bad idea. Putting different types in different files is a good way to organise your code. When I go to Car.swift I wouldn't expect to see the class Game.
How is it different from a Singeltone to share data between classes? (value type?)
Here you are just writing things in different files. As far as the compiler is concerned, this is not much different from writing everything in a single file because Swift organises code in modules, not files. The Singleton pattern is something completely different. It is when you only have one shared instance of a type.
What is the right place to put the structure outside the Main class to get some clear code ?
In another file, because Main should really be in its own file.
Should I make one file with many not related Structs ?
No. That is a bad way of organising your code. When you want to find a particular struct, how do you know which file it is in?

How to convert Objective C textual (po) representation to real object

In xcode you can use po object to see a textual representation of a given object. Is it possible to convert from this textual representation to a real objective c object?
Thanks
I suppose you could parse out the address, assign it to a pointer, and retrieve the object from memory that way, but that IS A HORRIBLY BAD IDEA AND YOU SHOULD NEVER DO THAT.
Real question: what are you trying to do?
I have a project that may inspire you, its on GitHub and its called NDJSON. Basically it uses runtime introspection to get properties an object has and set those properties using a json key of the same value. You could do something like that but get the property values instead of set. To help in situation where the type can not be determined like the type of objects to go in a collection or to map a json key to differently named property I have defined an informal protocol that has some methods that return dictionary mapping json key to property names or class to use for property name, there are also method to return a list of json keys to ignore or alternatively the only keys to accept. If you look at my code on git hub there is a class called NDJSONDeserializer which contains this logic. There is a function called getTypeNameFromPropertyAttributes() which parses out the string result of Apples runtime method property_getAttributes() which you will want to look at.
No, the representation you see from po <instance> is the result of -[<instance> debugDescription], which by default returns -[<instance> description], as described in http://developer.apple.com/mac/library/technotes/tn2004/tn2124.html.
Unless the instance you're dealing with just happens to provide a description which is a serialized form of itself, you're SOL. Most objects don't.
The real question, as Dave points out is, what are you trying to do? po only works in the gdb console, so I assume this is a debugging issue. In that case, do you know that the gdb console supports sending messages to instances? So you can do:
po [<myInstance> methodReturningAnOtherObject]
or
po [<myInstance> valueForKeyPath:#"some.long.key.path"]
to follow the object graph from within the debugger console.