Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/05/23 14:22
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
2 registered members (3run, AndrewAMD), 667 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Entity skill as struct pointer #380359
08/16/11 11:09
08/16/11 11:09
Joined: Aug 2011
Posts: 3
F
Faolan Offline OP
Guest
Faolan  Offline OP
Guest
F

Joined: Aug 2011
Posts: 3
Greetings all! I've started working with gamestudio again a few days ago after a many year hiatus.

I'm working on a project and progress has been steady, but i've run into a problem which my limited knowledge can't find a solution for.

I want to store a pointer to a struct in an entity's skill31, so that i can store a unique copy of that struct for each entity.

This is my first time i've used structs or things like malloc, so i'm probably doing it horribly wrong.

I tried something like this:

Code:
#define TESTSKILL skill31


typedef struct
{
	var x;
	var y;
	var z;
	
}NODE;

ENTITY* tester;

function test(ENTITY* ent)
{
	if(ent.TESTSKILL==-1)
	{
		ent.TESTSKILL = (NODE*) malloc (sizeof(NODE));
		
		ent.TESTSKILL->x =3;
		beep();
	}
	
}


action test_ent()
{
	my.TESTSKILL=-1;
	tester =my;
	test(my);
}



but that didn't work, so can anyone tell me the correct way to achieve this? Any help would be much appreciated laugh

Last edited by Faolan; 08/16/11 11:10.
Re: Entity skill as struct pointer [Re: Faolan] #380360
08/16/11 11:41
08/16/11 11:41
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
You need a cast:
((NODE*)(ent.TESTSKILL))->x =3;

The cast in the malloc line doesn't do anything. wink

Re: Entity skill as struct pointer [Re: Lukas] #380361
08/16/11 11:53
08/16/11 11:53
Joined: Aug 2011
Posts: 3
F
Faolan Offline OP
Guest
Faolan  Offline OP
Guest
F

Joined: Aug 2011
Posts: 3
Ah i see, knew i was doing it wrong somewhere ^^

Many thanks, now i can start using it properly, and look forward to many more different errors instead hehe

EDIT: Ok so i got that test working fine, but how do i do the same thing with an array of my structs? I'm thinking i need to do something like:
Code:
ent.PATHLIST = malloc (sizeof(NODE)*10);


but i'm not sure how i would then go about accessing the different structs in the array.

Trying to wrap my head around all these new things i've never used before is confusing but fun laugh


Last edited by Faolan; 08/16/11 12:21.
Re: Entity skill as struct pointer [Re: Faolan] #380370
08/16/11 13:27
08/16/11 13:27
Joined: Feb 2010
Posts: 320
TANA/Madagascar
3dgs_snake Offline
Senior Member
3dgs_snake  Offline
Senior Member

Joined: Feb 2010
Posts: 320
TANA/Madagascar
Hi,

Perhaps this could help you :
Code:
#include <acknex.h>
#include <default.c>

#define PATHLIST skill31

typedef struct
{
	var x;
	var y;
	var z;	
} NODE;

ENTITY* tester;

function test(ENTITY* ent)
{
	/*if(ent.TESTSKILL == NULL)
	{
		ent.TESTSKILL = (NODE*) malloc (sizeof(NODE));
		
		((NODE *)ent.TESTSKILL)->x =3;
		beep();
	}*/
	if (ent.PATHLIST == NULL)
	{
		ent.PATHLIST = malloc (sizeof(NODE)*3);
	}
}


action test_ent()
{
	my.PATHLIST = NULL;
	tester =my;
	test(my);
	
	// Pathlist
	NODE *data = (NODE *)my->PATHLIST;
	
	// First element
	(data+0)->x = 0;
	(data+0)->y = 0;
	(data+0)->z = 0;
	
	// Sec element
	(data+1)->x = 0;
	(data+1)->y = 0;
	(data+1)->z = 0;
	
	// Third element
	(data+2)->x = 0;
	(data+2)->y = 0;
	(data+2)->z = 0;
	
	while(1)
	{
		// Print curent x value
		// DEBUG_VAR(((NODE *)my->TESTSKILL)->x, 10);
		NODE *data = (NODE *)my->PATHLIST;
		DEBUG_VAR((data+0)->x, 10);
		DEBUG_VAR((data+1)->x, 40);
		DEBUG_VAR((data+2)->x, 60);
		
		wait(1);
	}
}

void main()
{
	// Wait for graphics
	wait(1);
	
	// Switch vid
	video_switch(4, 32, 0);
	
	// Set title
	video_window(NULL, NULL, 0, "Struct pointer in skills");
	
	// Load level
	level_load("");
	
	// Create entity
	ENTITY *test = ent_create(CUBE_MDL, nullvector, test_ent);
	
	while(1)
	{
		NODE *data = (NODE *)test->PATHLIST;
		
		// First Element 
		(data+0)->x ++;
		
		// Second element
		(data+1)->x += 2;
		
		// Third element
		(data+2)->x += 4;
		
		// Wait 1 frame
		wait(-1);
	}
}



Code:
ent.PATHLIST = malloc (sizeof(NODE)*10);

// Get node array
(NODE *)nodes = (NODE *)ent->PATHLIST;

//Accessing the first element
nodes->x = 0;
nodes->y = 0;
nodes->z = 0;

// Or
(nodes + 0) ->x = 0;
(nodes + 0) ->y = 0;
(nodes + 0) ->z = 0;

// Second element
(nodes + 1) ->x = 0;
(nodes + 1) ->y = 0;
(nodes + 1) ->z = 0;

// Nth element
/*
(nodes + n) ->x = 0;
(nodes + n) ->y = 0;
(nodes + n) ->z = 0;
*/



I think you understand.

Perhaps you could create a PATH struct containing an array of nodes and the number of nodes in it? that would be more easy to handle I think.

Best regards.

Re: Entity skill as struct pointer [Re: 3dgs_snake] #380373
08/16/11 14:03
08/16/11 14:03
Joined: Aug 2011
Posts: 3
F
Faolan Offline OP
Guest
Faolan  Offline OP
Guest
F

Joined: Aug 2011
Posts: 3
Thanks i read over it and i think i understand it mostly, i'll have to put it into practise and see if i truely have understood correctly tongue

The reason i'm doing this is i'm starting to write a simple pathfinding ai, i had a working example to go off but it was using global arrays. I wanted to use structs to keep things more organised (and to explore using structs), and i wanted to be able to dynamically create different path arrays so that more than one entity can be using the pathfinding at the same time.

I think you've both given me enough to get started, so i'll go give it a try and see what happens.

Thanks for your help laugh


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