Struct

Discussions about Coding and Scripting
Post Reply
User avatar
Rakiayn
Masterful
Posts: 550
Joined: Fri Aug 28, 2009 3:33 pm

Struct

Post by Rakiayn »

Anyone can explain to me what exectly a struct is? maybe with a simple example?
I already read unrealwiki, but that didnt made me understand it
User avatar
Shadow
Masterful
Posts: 743
Joined: Tue Jan 29, 2008 12:00 am
Personal rank: Mad Carpenter
Location: Germany
Contact:

Re: Struct

Post by Shadow »

A struct is a method to define a new data structure or to combine different structures together.
Let's take the vector struct. It consists of 3 floats (floating point numbers) and form together AS struct a vector.

Code: Select all

struct Vector
{
  var() config float X, Y, Z;
};
Image
User avatar
Feralidragon
Godlike
Posts: 5493
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Struct

Post by Feralidragon »

In a short summary: a struct is a type which can hold several types of variables.
Think struct as a "group of variables": "vector" is a struct that holds 3 float variables: X, Y and Z, "rotator" is a struct that holds 3 int variables: Yaw, Pitch and Roll, etc.
Example:

Code: Select all

struct MyStructA
{
	var int n;
	var string s;
	var Actor A;
};
Basically I created a type (not variable yet) struct called MyStructA, and MyStructA is composed by:
- int called n;
- string called s;
- actor reference called A.

From there you can now declare one or more actual variables of the type MyStructA, then use that variable to assign or read values:

Code: Select all

var MyStructA someVar;

...

someVar.n = 100;
someVar.s = "I love structs";
someVar.A = Spawn(Class'Actor');

...

local string sss;

	sss = someVar.s;
You can even create another different struct with an existent struct type in it:

Code: Select all

struct MyStructB
{
	var int x;
	var MyStructA mA;
};
Any doubts? :)

EDIT: Shadow you're a post ninja xD, you were quicker than me lol
User avatar
Rakiayn
Masterful
Posts: 550
Joined: Fri Aug 28, 2009 3:33 pm

Re: Struct

Post by Rakiayn »

okay I understand that, so basicly it is not very different from using normal variables, exept this keeps it organized?
User avatar
Feralidragon
Godlike
Posts: 5493
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Struct

Post by Feralidragon »

Yeah, somewhat, they're actually more than that: for example, let's say you want to store players data, from a max of 32 players, within a mutator: name, team, kills (I know you can get any of these directly from the player or playerreplicationinfo, but this is just an exemplification). How would you declare/build the variables?
Here's an example:

Code: Select all

struct PData
{
	var string PlayerName;
	var byte Team;
	var int Kills;
};

var PData PlayersData[32];
See how useful they are?
I think this is the best kind of usage you can give to structs, plus you can make direct comparisons with them, take the following example values:

Code: Select all

PlayersData[0].PlayerName = "John";
PlayersData[0].Team = 0;
PlayersData[0].Kills = 100;

PlayersData[1].PlayerName = "John";
PlayersData[1].Team = 0;
PlayersData[1].Kills = 100;

PlayersData[2].PlayerName = "John";
PlayersData[2].Team = 0;
PlayersData[2].Kills = 124567;

PlayersData[3].PlayerName = "Trololo";
PlayersData[3].Team = 0;
PlayersData[3].Kills = 100;
You can make comparisions like this:

Code: Select all

PlayersData[0] == PlayersData[1]	=> TRUE
PlayersData[0] == PlayersData[2]	=> FALSE
PlayersData[0] == PlayersData[3]	=> FALSE

Also you can give a more advanced functionality like in vector and rotators by creating your own operators, for example:

Code: Select all

final operator(20) PData <+> (PData A, PData B)
{
local PData PD;

	PD.PlayerName = A.PlayerName $ B.PlayerName;
	PD.Team = Min(A.Team, B.Team);
	PD.Kills = A.Kills + B.Kills;
	return PD;
}

...

PlayersData[5] = PlayersData[0] <+> PlayersData[3];

//End values
PlayersData[5].PlayerName 	=> "JohnTrololo";
PlayersData[5].Team 		=> 0;
PlayersData[5].Kills 		=> 200;
Are you seeing the possibilities using structs? :mrgreen:
JackGriffin
Godlike
Posts: 3774
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: -Retired-

Re: Struct

Post by JackGriffin »

Nice examples Ferali. I've often wondered this question myself and I knew I should have used them a few times but didn't really get the concept. Now I do and I'll use them in the future!
So long, and thanks for all the fish
User avatar
Rakiayn
Masterful
Posts: 550
Joined: Fri Aug 28, 2009 3:33 pm

Re: Struct

Post by Rakiayn »

I understand, except for that last peace of code which makes little sense to me
User avatar
Feralidragon
Godlike
Posts: 5493
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Struct

Post by Feralidragon »

As for the last example, here's perhaps a more understandable one:

Code: Select all

//Struct declaration
struct WWW
{
	var int AA;
	var int BB;
	var int CC;
	var int DD;
};

var WWW W1, W2, W3;

_______________________________________________

//Initial values
W1.AA = 100;
W1.BB = 50;
W1.CC = 1500;
W1.DD = 326;

W2.AA = 33;
W2.BB = 80;
W2.CC = 3000;
W2.DD = 1440;

_______________________________________________

//New operator
final operator(20) WWW + (WWW A, WWW B)
{
local WWW WD;

	WD.AA = A.AA + B.AA;
	WD.BB = A.BB + B.BB;
	WD.CC = A.CC + B.CC;
	WD.DD = A.DD + B.DD;
	
	return WD;
}

_______________________________________________

//Operation
W3 = W1 + W2;

//W3 final values
W3.AA: 133;
W3.BB: 130;
W3.CC: 4500;
W3.DD: 1766;
Post Reply