Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by dr_panther. 05/18/24 11:01
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (dr_panther, Ayumi, 1 invisible), 708 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
structs #450106
04/06/15 19:31
04/06/15 19:31
Joined: Apr 2015
Posts: 29
Germany
Saschaw04 Offline OP
Newbie
Saschaw04  Offline OP
Newbie

Joined: Apr 2015
Posts: 29
Germany
Good evening everybody,

Today I read something about structs, but I did not understand why should I use structs?
What are the benefits of structs?

Last edited by Saschaw04; 04/06/15 19:31.

-- started with programming on march 2015 --
-- living in Germany near Dortmund --
Re: structs [Re: Saschaw04] #450107
04/06/15 19:43
04/06/15 19:43
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline
Serious User
Reconnoiter  Offline
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Hi,

See their benefit a bit like skills. They are a good way to save stats and properties of e.g. a type entity. Like for example you have 2 types of enemies, one a Zombie and one a Soldier. You can create 1 struct that determines the amount of variables and their names etc. Than one for each type of enemy that are based on the first struct (so 1 for zombie and 1 for soldier). You can use these 2 structs to set their max hp, movement speed, attack damage etc.

They are also usefull for multiplayer games, since you can sends all your variables within a struct (reducing bandwidth by reducing overhead (I think that was the name)).

Last edited by Reconnoiter; 04/06/15 19:46.
Re: structs [Re: Reconnoiter] #450108
04/06/15 19:54
04/06/15 19:54
Joined: Apr 2015
Posts: 29
Germany
Saschaw04 Offline OP
Newbie
Saschaw04  Offline OP
Newbie

Joined: Apr 2015
Posts: 29
Germany
Okay, now its a bit more clear.

If I understant correctly, structs are better to reduce the used Memory as to reduce the workload in programming (written code).


-- started with programming on march 2015 --
-- living in Germany near Dortmund --
Re: structs [Re: Saschaw04] #450110
04/06/15 20:25
04/06/15 20:25
Joined: Jul 2002
Posts: 3,208
Germany
Error014 Offline
Expert
Error014  Offline
Expert

Joined: Jul 2002
Posts: 3,208
Germany
structs are like your own defined variable type.

That sounds boring. Let's make it sound half as amazing as it is.


Before we begin: In this, I'll brutally use "." instead of "->". Purists are well in their right to criticise, but I believe learning one new thing in this post is enough for now.



So, you probably noticed something in gamestudio. When you act on entities, with the me or you-pointer, then you can suddenly do all KINDS of crazy stuff. Like this:

Code:
my.skill1 = 10;
set(my.flag1,TRANSLUCENT);



And that's cool, isn't it? Can't do that with your variable.

Code:
var myentitiy;

myentity.skill1 = 10; //NOPE! Won't work.




"Yeah, but", you say, "that's cause it's a pointer. I know its not the same as a variable, you dummy.

Alright, okay. But you know what's also a pointer? This guy here:

Code:
BMAP* mybmap;



And, guess what, can't do these things on that one:

Code:
mybmap.skill1 = 10; //NOPE! That won't work



"Yeah, well...", you now say, perhaps already scratching your head, "'cause that's like, a different... kind of pointer. For a bmap. The other one was for entities."

And now, BAM, you got it.
BMAP and ENTITY are super-different, right? The one thing holds 100 skills, and flags, and got crazy stuff going on, like flags.
The other one doesn't NEED that stuff - instead, it has other stuff.

They are supposed to serve different purposes, and thats why their structure differs. Now, I want you to put on your explorer's helmet and open the "atypes.h"-file in your "include"-folder. Then, search for "typedef struct ENTITY". You'll find this

Code:
typedef struct ENTITY {
	C_LINK link;
	char	*type;		// entity file name
	var	x,y,z;		// position of the entity
	var	pan,tilt,roll;	// euler angles
	var	scale_x,scale_y,scale_z;	// scale factors, 0..255
	long	flags;		// entity flags
	var	frame;		// frame number for sprites & models
	var	next_frame;	// interpolation target frame for models
	var	skin;		// skin texture number for models
	var	ambient;	// -100..+100
	var	albedo;		// 0..100, light reflectivity
	var	alpha;		// 0..100, transparency, default 50
	var	lightrange;	// dynamic light range in quants
	var	blue,green,red;	// dynamic light color, 0..255
	long	emask;		// event enable flags
	long	eflags;		// hull and status flags
	var	min_x,min_y,min_z;
	var	max_x,max_y,max_z;	// bounding box for entity->entity collisions
	var	trigger_range;
	var	push;
	var	floor_dist; // distance to the floor (if not UNLIT) 
	long	smask;		// nosend_... flags
	var 	client_id;	// client # that has created this entity
							// or view pointer for a layer entity
	var	skill[100];	// entity skills
	var	pose;			// current bones pose number
	MATERIAL* material;	// entity material
	var	u,v;			// texture offsets
	var	group;	   // collision group
	long	flags2;		// sky flags
	char	*attachname;	
	EVENT	event;		// event function
	EVENT	local;		// client side function
	var	layer;		// layer for 2D entities and skies
	long	vmask;      // mask for suppressing mesh groups
	char	*string1,*string2;	// strings
	float fRadius;    // visual bounding radius
	long	path_index;		// path of the entity
	void* model;      // mesh/skin info pointer
	struct ENTITY* shadow;     // shadow sprite or model
	struct ENTITY* parent;     // parent entity
	BMAP* lightmap;   // external lightmap (f.i. for terrains)
	void*	body;			// physX actor
	var	clipfactor;		// visual clipping factor
} ENTITY;



Holy cow! That's a lot of stuff. And all of these, you can access by going

my.<something>

BMAP is comparably simple

Code:
typedef struct BMAP {
	C_LINK	link;
	long 	width,height; // original size of the bitmap
	long	bytespp;	// original bytes per pixel (1..4)
	void	*ptr;		// internal use
	byte	*pixels;	// ptr to palettized, 565, 4444, 888 or 8888 coded original image
	long	flags;      // see BMPF_... above
	void	*d3dtex;	// 	LPDIRECT3DTEXTURE9 (usually a different format than the original image)
	float	u1,v1,u2,v2; // texture pixel bounds
	long	u,v;		// cutout start size
	long	refcount;	// for implicitely created bmaps
	long	finalwidth,finalheight,finalbytespp;
	long	pitch,finalpitch;
	long	miplevels;
	long	finalformat;
	void	*finalbits;	// bitmap content when locked, otherwise NULL
} BMAP;



and that, to, you can use, but you tend to do that indirectly using the bmap_-functions.



Okay, so, here's the point. In your dream game, who knows what you want to have. Mystical armor to find? Space alien species to attack? Strange alcohol to buy? Who's to say. So, in an engine, you'll want FLEXIBILITY. Structs allow you to have all kind of strange things in one neat package, that is easy and simple to access. Once the code is (properly) written, it's easy to ADD things to your package. For instance, if you HAD armor:

Code:
typedef struct ARMOR {
 var defense;
 char element;
} ARMOR;



And you add it to your game

Code:
ARMOR* armorlisttofind;
int amountofarmorstofind;
...

armorlisttofind = (ARMOR*) sys_malloc(amountofarmorstofind * sizeof(ARMOR));



Then you don't have to change lots of stuff once you realized you messed up and forgot the most important bit of information of armor.

Code:
typedef struct ARMOR {
 var defense;
 char element;
 var howpinkitis; //0: slightly pink, 10:SUPER pink!!
} ARMOR;



The other stuff stays, but now in the rest of the game, you can access that bit of info

Code:
void NPCdialog() {
if(playerarmor.howpinkitis>8) {
Textbox("Oh Snap you're awesome!");
} else {
Textbox("Go away you suck");
}
}





structs give you the ability to add your own type of information containers, suited to your needs, and relatively adaptable for changes later down the road. They can be stuffed together in arrays and worked together on groups, which is infinitely better than handling thousands of

"first_armor_defense", "first_armor_howpinkitis", "first_armor_element", "second_armor_defense", "second_armor_howpinkitis", ...

variables. They are great, and should be your friend, and it's an outrage of how easy it is to miss their usefulness and strength at first.




Technically, as far as I understand, they "just" reserve a chunk of memory and the different elements you have in them are stored one after another. But you don't have to worry about that, the engine does that for you. Because it loves you - if you love structs.

If you don't, it's not so into you.


Perhaps this post will get me points for originality at least.

Check out Dungeon Deities! It's amazing and will make you happy, successful and almost certainly more attractive! It might be true!
Re: structs [Re: Error014] #450112
04/06/15 20:56
04/06/15 20:56
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
Good said.
One small thing:
Structs can be used as variables without pointers, but cannot be copied then:
Code:
typedef struct {
    var model;
    var damage;
    var animations;
} WEAPON;

WEAPON weapons[10]; // 10 Global weapons

void init()
{
    weapons[0].model = 1;
    weapons[0].damage = 35;
    weapons[0].animations = 2;
}

action enemy()
{
    WEAPON weapon;
    weapon.damage = 30;
    [...]
}



Visit my site: www.masterq32.de
Re: structs [Re: MasterQ32] #450114
04/06/15 21:25
04/06/15 21:25
Joined: Apr 2015
Posts: 29
Germany
Saschaw04 Offline OP
Newbie
Saschaw04  Offline OP
Newbie

Joined: Apr 2015
Posts: 29
Germany
Thank you very much for the detailed answers.
Tomorrow I will read it again and think about it.

But today I get so much Information (I read "Spiele entwickeln mit Gamestudio von Jonas Freiknecht"), so I am confused now.


-- started with programming on march 2015 --
-- living in Germany near Dortmund --
Re: structs [Re: Saschaw04] #450116
04/06/15 21:48
04/06/15 21:48
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Error014@ Man, I always love to read how you explain things and how you write your posts in general grin you should be the one to write new manual and create video tutorials for A9 if it's ever going to come out!

best regards!


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: structs [Re: 3run] #450153
04/07/15 14:01
04/07/15 14:01
Joined: Apr 2015
Posts: 29
Germany
Saschaw04 Offline OP
Newbie
Saschaw04  Offline OP
Newbie

Joined: Apr 2015
Posts: 29
Germany
OK I think I understand.
For me it sounds that structs are like a re-useable template. Very useful.


-- started with programming on march 2015 --
-- living in Germany near Dortmund --

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1