Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/24/24 20:04
M1 Oversampling
by Petra. 04/24/24 10:34
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Data from CSV not parsed correctly
by EternallyCurious. 04/20/24 21:39
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 642 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Structs?[solved] #351977
12/30/10 13:08
12/30/10 13:08
Joined: Sep 2010
Posts: 82
P
peteredlin Offline OP
Junior Member
peteredlin  Offline OP
Junior Member
P

Joined: Sep 2010
Posts: 82
Hello for a project i want to use a struct to load my function.
But how do i make the function accept differrent structs of the (same type of course)

All help is welcome


Last edited by peteredlin; 12/31/10 00:53.
Re: Structs? [Re: peteredlin] #351981
12/30/10 13:38
12/30/10 13:38
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline
Serious User
DJBMASTER  Offline
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
Hi, all you need to do is pass a pointer to a structure into your function...
Code:
typedef struct
{
	var ammo;
	var reload_rate;
} 
Gun;

function ReloadWeapon(Gun* gun)
{
	gun -> ammo = 100;
}

void main()
{
	Gun new_gun;
	ReloadWeapon(&new_gun);
}



Re: Structs? [Re: peteredlin] #351982
12/30/10 13:43
12/30/10 13:43
Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Rei_Ayanami Offline
Expert
Rei_Ayanami  Offline
Expert

Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Put a id variable as the first variable of all your different structs.

Than, take the void in the function like this:

void yourFunction(void* structVoid)
{

Copy the the first byte into a temponary struct, to identfy the struct.

INDENTSTRUCT* newIdent = sys_malloc(sizeof(INDENTSTRUCT));

memcpy(newIdent, structVoid, 4); //in case you are using a var, as it has 4 bytes

if(newIdent.id == 1) //Your first Struct
{
YOURFIRSTSTRUCT* tempStruct;
memcpy(tempStruct, structVoid, sizeof(YOURFIRSTSTRUCT));

the same goes for id two and so on laugh

Hope this helps laugh

Re: Structs? [Re: Rei_Ayanami] #351994
12/30/10 15:36
12/30/10 15:36
Joined: Sep 2010
Posts: 82
P
peteredlin Offline OP
Junior Member
peteredlin  Offline OP
Junior Member
P

Joined: Sep 2010
Posts: 82
why does the following code not work then?

Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <ackphysx.h>

///////////////////////////////

typedef struct
{
	ENTITY* entity;
	char filename;
	VECTOR* position;
} simple_ent;



function Create_ent(simple_ent* ent)
{
	ent.entity = ent_create(ent.filename,ent.position,NULL);
	

}




ENTITY* box;

function main()
{
 	simple_ent cube;
	cube.filename = "cube.mdl";
	cube.position=vector(0,0,0);
 	
 	physX_open();
 	level_load("level.hmp");
 	Create_ent(cube);
 	//while(1)
 	//{
 	//box.x+=0.001;
 	//wait(1);
 	
 	//}
 	
 	
}



I get an error that the file cant be opened

Last edited by peteredlin; 12/30/10 15:37.
Re: Structs? [Re: peteredlin] #351995
12/30/10 15:41
12/30/10 15:41
Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Rei_Ayanami Offline
Expert
Rei_Ayanami  Offline
Expert

Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
it fails here:

cube.position=vector(0,0,0);

Re: Structs? [Re: peteredlin] #351997
12/30/10 16:10
12/30/10 16:10
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
try char* instead of char


Visit my site: www.masterq32.de
Re: Structs? [Re: Rei_Ayanami] #351998
12/30/10 16:11
12/30/10 16:11
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
There are multiple errors in that code.
Firstly, you defined your filename string wrong. "char filename;" defines only one character. You have to use "char* filename;" to create a char array.


Secondly, with "cube.filename = "cube.mdl";" you just create a temporary char array. (This only works when initialising a char array while defining it, but that might not even work in Lite-C either.) It will be invalid in the next line. This is how you should to it:
Code:
cube.filename = malloc(sizeof(char)*number_of_characters_you_need_plus_one);
strcpy(cube.filename,"cube.mdl"); // strcpy, not str_cpy




For your vector, better use VECTOR, instead of VECTOR*. Then you can just set it like this:
Code:
cube.position.x = 0;
cube.position.y = 0;
cube.position.z = 0;



Last edited by Lukas; 12/31/10 11:00. Reason: sizeof(char) instead of sizeof(char*)
Re: Structs? [Re: Lukas] #352020
12/30/10 20:25
12/30/10 20:25
Joined: Sep 2010
Posts: 82
P
peteredlin Offline OP
Junior Member
peteredlin  Offline OP
Junior Member
P

Joined: Sep 2010
Posts: 82
Thank you very much!

Now it works, learned some again blush

Re: Structs? [Re: peteredlin] #352071
12/31/10 11:02
12/31/10 11:02
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
You're welcome.

But please note that I made a little error in my code. It should be sizeof(char) instead of sizeof(char*). This way four times the nececcary memory was allocated.


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