PDA

View Full Version : Data Sharing between classes



beeph
10-05-2010, 08:50 PM
Was wondering if there was a way to share data between one class and another, both custom classes.

Obviously writing to a file then reading from that same file could work. But is there a faster way? Maybe creating a static object somehow with static methods?

Also whats a best way to get the locally controlled pawn reference.

a_karimi
10-05-2010, 11:06 PM
Was wondering if there was a way to share data between one class and another, both custom classes.
Obviously writing to a file then reading from that same file could work. But is there a faster way? Maybe creating a static object somehow with static methods?

I may not have been understood you correctly, but sharing behavior exists already! It's not actually called sharing but you can overwrite/read any variable you want from where ever you want (Unless you have restrict access to it via modifiers like private or const or ...). Sharing is mostly used when you got multiple threads or mostly when you got multiple processes.


Also whats a best way to get the locally controlled pawn reference.

If you want to call it from an Actor class:
getALocalPlayerController().pawn

I hope it would help :)

FlowState
10-06-2010, 01:20 AM
You can also use a "blackboard" approach. Say, for instance, that you want 4 pawns in a squad to share how many enemies they see.

Create myBlackboard.uc with:



var int totalEnemiesSeen;

function reportEnemies(int enemies)
{
totalEnemiesSeen += enemies;
}


Then make sure every member of the squad has access to this blackboard.

Obviously this code isn't even nearly the best way to do that, but it should give you the idea.

beeph
10-06-2010, 08:04 AM
i like the blackboard.uc approach. The problem with writing directly to the world info is im at a menu screen and it seems that the world gets reinitialized when i enter the map level, and loses all the weapons/info i tried to pass to it. So I need something persistent. I will try this.

also the local player controller is handy thank you.

I assume i can isntantiate this class using the java method myBlackboard = new blackboard()? Then all the pawns can refence myBlackboard? Or does the engine automatically instantiate all your /Class/blah.u files on startup?

I guess I'm a little confused on what class model utscript uses, unless it really is just straight javascript.

Blade[UG]
10-06-2010, 12:41 PM
http://udn.epicgames.com/Three/UnrealScriptReference.html

beeph
10-06-2010, 03:57 PM
Then make sure every member of the squad has access to this blackboard.

Obviously this code isn't even nearly the best way to do that, but it should give you the idea.

how do u make sure every pawn has access to the blackboard? That's kind of what i'm asking with data-sharing. The blackboard needs to be one instance, and global. I tried using a static function to access it, but it can only access default values says the compiler. I tried putting Default properties globalBlackboard = new Blackboard() in the class definition, but my static getGlobalBlackboard method still wont return that particular instance.

If i spawn blackboards in all my pawns, then they each have their own instance of the blackboard, and arent sharing data.

What i need is something like what class'WorldInfo'.static.getWorldInfo() does, but i looked at the implementation and it was native and undefined.

I look at the UDK notes, and saw a flag that would allow a class to be global, but it says its broken cuz of 64 bits.

Do you think I can just extend WorldInfo and add in my blackboard there?

Blade[UG]
10-07-2010, 01:07 AM
make an Info class, and keep track of it in your GameInfo

Blade[UG]
10-07-2010, 07:20 AM
;27689102']make an Info class, and keep track of it in your GameInfo

... and always put your defaultproperties at the BOTTOM of a class. And NEVER put the opening "{" in defaultproperties on the same line as the defaultproperties keyword

beeph
10-07-2010, 07:39 AM
class charData extends Object;

var int charDefaultWeapon;

function static bool setDefaultWeapon ( int weaponID )
{
default.charDefaultWeapon= weaponID;
return true;
}

function static int getDefaultWeapon ( )
{
return default.charDefaultWeapon;
}

DefaultProperties
{
charDefaultWeapon=0
}

Error, 'charDefaultWeapon': Can't change default values of non-config properties

Failure - 1 error(s), 0 warning(s)

I just dont think the 'static function/default variables' approach works. I will try loading my class in GameInfo but I'm at a menu screen, with no active game (hud disabled), and I'm trying to pass various options from a menu screen into the game instance. I'm not quite sure I can get a gameinfo reference from my gfxMoviePlayer class and even if i can, im worried that it will be destroyed once the real game begins.

Wizzard~Of~Ozz
10-07-2010, 01:28 PM
function static bool setDefaultWeapon ( int weaponID )
{
default.charDefaultWeapon= weaponID;
return true;
}


You cannot change a value in the class using a static call. Static can only read default values, because the item is assumed not to be instanced ( so read only, alter variables in another class ). If you want to alter/store values, you must spawn || New the class to an instance.

FlowState
10-07-2010, 06:50 PM
;27689102']make an Info class, and keep track of it in your GameInfo

Okay, this is something I'm having issues with, and I think OP and I are asking the same question (as in a post I recently made).

So I make an Info. How do I instantiate that within my GameInfo subclass so that some controllers can grab a reference to it?

Is there some global location (per map? per game instance) where all classes are instantiated and registered?

Or is it as simple as


class MyGameInfo extends GameInfo;

var CustomClass myClass;

event BeginPlay()
{
myClass = new CustomClass();
}

?

Blade[UG]
10-07-2010, 06:54 PM
When you are calling something that is static, you are calling it IN the class, and not in an instance of the class. When operating static functions, you can only access default values of config variables. You will not likely use too many static functions in the course of your time using UnrealScript, because it is somewhat limiting in what you can do with it. The static function system does work fine, but is not at all the type of approach you would want to take here.

Yes, your GameInfo will be destroyed when you open a new level, and a new GameInfo will be created. You could easily pass your options on the command line to your new map ( "open map?myvariable=whatever?myothervariable=somethinge lse") and parse them in your InitGame().

FlowState:

spawn() for a subclass of Info (which is an Actor), new() for an item that is not a subclass of Actor, only of Object.

FlowState
10-07-2010, 06:56 PM
So if one were not to take the static approach, and just create a normal class, then you could go ahead and code it similarly to what I have above?

Also, probably worth a quick look into InitGame(), because understanding the flow of initialization is so important.

Blade[UG]
10-08-2010, 03:58 AM
yeah, create the instance on game startup, and keep track of it. InitGame(), I believe is called prior to the rest of the world initialization, and is mostly used in the default code to parse command line options for the game. PostBeginPlay() is called after the world is created, and such.