Gamestudio Links
Zorro Links
Newest Posts
Executing Trades on Next Bar Open
by Zheka. 06/20/24 14:26
Lapsa's very own thread
by rki. 06/19/24 11:27
A simple game ...
by VoroneTZ. 06/18/24 10:50
Face player all the time ...
by bbn1982. 06/18/24 10:25
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 692 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Mino, squik, AemStones, LucasJoshua, Baklazhan
19061 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
How do I...structures #234179
11/01/08 13:08
11/01/08 13:08
Joined: May 2006
Posts: 53
Puerto Rico
monchito Offline OP
Junior Member
monchito  Offline OP
Junior Member

Joined: May 2006
Posts: 53
Puerto Rico
Hi: A7.5 Comm.
I'm trying to learn how to use structures with A7 to change my code from scripts but with not much success. My problem is accessing and comparing elements. Errors and crash are the norm
I tried samples from the forum, sometimes works...is there a
tutorial for this? Rules to know for structures in lite-C?
Please some direction to follow.
My other working engine is irrlicht 4.1 with DevCpp, no problem
there with structures and pointers, i know the rules.
Are my option are still using C-script with 3dgs and C with irrlicht?

#define MODE_STAND 0;
#define MODE_WALK 1;

typedef struct
{
var health;
var shield;
var count;
var state;
var pathnode;
var lin_speed;
var rot_speed;
var anim_speed;
var vision_range;
var weapon_type;
var weapon;
var weapon_range;
var ammo;
ENTITY* model;
} entity3d;

function main()
{
entity3d enemy;
enemy.model = ent_create ("mymodel.mdl", vector(0,0, 25), NULL);
func(enemy);
}

function func(entity3d* ent)
{
ent.state = MODE_WALK // error!!?
if (ent.state == MODE_WALK) // error!!?
}

Re: How do I...structures [Re: monchito] #234186
11/01/08 13:36
11/01/08 13:36
Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
Quad Offline
Senior Expert
Quad  Offline
Senior Expert

Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
you are mainly doing it right, and struct rules for lite is same as C language.

1. make sure you are saving it as .c file

2. remove semicolons from defines.
add semicolons to prooper places(marked red)

#define MODE_STAND 0
#define MODE_WALK 1

typedef struct
{
var health;
var shield;
var count;
var state;
var pathnode;
var lin_speed;
var rot_speed;
var anim_speed;
var vision_range;
var weapon_type;
var weapon;
var weapon_range;
var ammo;
ENTITY* model;
} entity3d;

function func(entity3d* ent); dont forget prototypes
function main()
{
entity3d enemy;
enemy.model = ent_create ("mymodel.mdl", vector(0,0, 25), NULL);
func(enemy);
}

function func(entity3d* ent)
{
ent.state = MODE_WALK;//dont forget semicolons

if (ent.state == MODE_WALK) printf("yaaaay i am walking.")
}



----

You should see the "yaaay i am walking" popup when you urn it


3333333333
Re: How do I...structures [Re: Quad] #234252
11/01/08 20:26
11/01/08 20:26
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
For "clean and safe" usage, dont you also need to zero any structure instances?
As in
Code:
function main()
{
entity3d enemy;    //define new entity3d object
zero(enemy);       //make sure all elements are at zero'ed  ????
...

Ive always been blurry on this issue...


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: How do I...structures [Re: EvilSOB] #234289
11/01/08 23:24
11/01/08 23:24
Joined: May 2006
Posts: 53
Puerto Rico
monchito Offline OP
Junior Member
monchito  Offline OP
Junior Member

Joined: May 2006
Posts: 53
Puerto Rico
Hi:
Actually the error are accessing the elements of the model inside the structure. Ej: entity.model.x, entity.model.tilt
Maybe i'm doing something wrong.....

unction main()
{
entity3d enemy;
enemy.model = ent_create ("mymodel.mdl", vector(0,0, 25), NULL);
func(enemy);
}
function func(entity3d* ent)
{
VECTOR v1;
ent.state = MODE_WALK;

v1.x = ent.model.x; // Error and crash!
vec_set (v1.x, ent.model.x); // Error and crash
ent.model.y -= 1; // Error and crash!
}

NOTE:
The problems disappear when the structures are declared globally. Not inside a function. Still investigating. (reading the manual)
Thanks for your corrections....

Re: How do I...structures [Re: monchito] #234297
11/02/08 00:17
11/02/08 00:17
Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
Quad Offline
Senior Expert
Quad  Offline
Senior Expert

Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
try

ent.model->y


3333333333
Re: How do I...structures [Re: Quad] #234301
11/02/08 00:34
11/02/08 00:34
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Works OK for me, as long as Ive got #include <acknex.h> at the top
and have inserted level_load(NULL); into the start of main,
otherwise its all your code.

The below code is the ENTIRE *.c file I am testing with, if its different
to yours, post the COMPLETE file of your demo code.

Code:
#include <acknex.h>
#include <default.c>
//
#define MODE_STAND 0
#define MODE_WALK 1

typedef struct
{
	var health;
	var shield;
	var count;
	var state;
	var pathnode;
	var lin_speed;
	var rot_speed;
	var anim_speed;
	var vision_range;
	var weapon_type;
	var weapon;
	var weapon_range;
	var ammo;
	ENTITY* model;
} entity3d; 

function func(entity3d* ent);


function main()
{
	level_load(NULL);
	entity3d enemy;
	enemy.model = ent_create ("mymodel.mdl", vector(0,0, 25), NULL);
	func(enemy);
}


function func(entity3d* ent)
{
	VECTOR v1;
	ent.state = MODE_WALK;
	v1.x = ent.model.x; 			// no Error and crash! for me
	vec_set (v1.x, ent.model.x); 		// no Error and crash! for me
	ent.model.y -= 1; 			// no Error and crash! for me
}



"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: How do I...structures [Re: EvilSOB] #234317
11/02/08 04:01
11/02/08 04:01
Joined: May 2006
Posts: 53
Puerto Rico
monchito Offline OP
Junior Member
monchito  Offline OP
Junior Member

Joined: May 2006
Posts: 53
Puerto Rico
Ok, worked for me... here the modification i'm trying from C-script. Maybe a new from scratch is better than a modified one. Now less Errors and counting...still debuging
Code:
[/code]
#include <acknex.h>
#include <default.c>
//
#define MODE_STAND  0
#define MODE_WALK   1
#define MODE_RUN    2
#define MODE_DEATH  3
#define MODE_ATTACK 4
#define MODE_STOP   5
#define MODE_MARCH  6
#define MODE_STANDBY 7

STRING* buff = "          ";
static int NPC_Counter = 0;

typedef struct
{
	var health;
	var shield;
	var count;
	var state;
	var pathnode;
	var lin_speed;
	var rot_speed;
	var anim_speed;
	var vision_range;
	var weapon_type;
	var weapon;
	var weapon_range;
	var ammo;
	ENTITY* model;
} entity3d; 

function act_player(entity3d* ent);
function all_events(); 
function check_downward(entity3d* ent, vmin, vmax);
function act_enemy(entity3d* ent);


function main()
{
   level_load(NULL);  // load the game level
   entity3d hero;
   hero.model = ent_create ("player.mdl", vector(0,0,0), NULL);
   act_player(hero);
   NPC_Counter++;

   entity3d enemy;
   enemy.model = ent_create ("player.mdl", vector(40,40,0), NULL);
   act_enemy(enemy);
   NPC_Counter++;
}


function act_player(entity3d* ent)
{
   ent.state = MODE_WALK;
   ent.health = 100;
   ent.vision_range = 200;
   ent.lin_speed = 5;
   ent.rot_speed = 1;
   ent.model.scale_x = .5;
   ent.model.scale_y = .5;
   ent.model.scale_z = .5;
   ent.model.emask |= (ENABLE_BLOCK | ENABLE_ENTITY | ENABLE_IMPACT);
   ent.model.event = all_events;
   //set(ent.model, INVISIBLE);
   player = my = ent.model;
   
   while(1)
   {
     camera.x = my.x;
     camera.y = my.y;
     camera.z = my.z + 5;
     camera.pan = my.pan;
     camera.tilt = my.tilt;

     move_mode = IGNORE_PASSABLE + USE_BOX;  
     // movement keys 'WASD' 
     if(key_w == 1)
     { 
        ent_move (vector(5 * time_step,0,0), nullvector);
        // little up-down movement while walking
        my.z += .5 * sin(total_ticks * 25) * time_step;
        // if inclined, get straight while walking 
        if(my.tilt < 1) my.tilt += .25;
        if(my.tilt > 1) my.tilt -= .25;
     }
     
     if(key_s == 1)
     {
     	  ent_move (vector(-5 * time_step,0,0), nullvector);

        if(my.tilt < 1) 
           my.tilt += .25;

        if(my.tilt > 1) 
           my.tilt -= .25;
     }

     if(key_a == 1) 
        my.pan += 3 * time_step;
     
     if(key_d == 1)
        my.pan -= 3 * time_step;
     
     if(key_q == 1)  // do not allow more than 30 deg inclination
     { 
       if(my.tilt > 30)
          my.tilt = 30;
          
       my.tilt += 1;
     }

     if(key_z == 1) // do not allow more than 30 deg inclination
     {
     	 if(my.tilt < -30)
     	    my.tilt = -30;
     	 
     	 my.tilt -= 1;
     }
    
     check_downward(&ent, 0, 3);

     wait(1);
  }

}

function check_downward(entity3d* ent, vmin, vmax)
{
    proc_kill(4);
    var dist_trace;
    VECTOR temp;
    
    // check downward 
    vec_set (temp.x, ent.model.x);
    temp.z = ent.model.z - 15000;
    trace_mode = IGNORE_ME + IGNORE_SPRITES + USE_BOX + IGNORE_PASSABLE;
    dist_trace = trace (ent.model.x, temp.x);

    // stay above ground
    if(dist_trace < vmin)
       ent.model.z += 1; 

    if(dist_trace > vmax)
       ent.model.z -= 1;

    if(dist_trace > (vmax + 5))
       ent.model.z -= 3;

    if(dist_trace < (vmin - 4))
       ent.model.z += 3;   

}

function act_enemy(entity3d* ent)
{
  var percent;
  var d1;
  var d2;
  var vmin = 0;
  var vmax = 3;
  VECTOR temp;
  var dist_trace;

  my = ent.model;
  my.scale_x = .4;
  my.scale_y = .4;
  my.scale_z = .4;
      
  while(!player){wait(1);}

  ent.state = MODE_WALK;       	

  ent.health = 100;
  ent.vision_range = 250;
  
  while(ent.health > 10)
  {
     d1 = vec_dist(ent.model.x, player.x);

    if (ent.state == MODE_WALK)
    {
       percent += 3 * time_step;
       ent_animate(ent.model,"walk", percent, ANM_CYCLE);
       percent %= 100;
    }
    

    if (ent.state == MODE_ATTACK)
    {
       vec_set(temp,my.x);
       if(vec_dist(me.x, player.x)<1000 && vec_to_screen(temp,camera)!=NULL)
       {
           str_for_num(buff, ent.health);
 	   draw_text(buff, temp.x, temp.y-20, vector(200,200,200));
       }
       // perform animation
       percent += 3 * time_step;
       ent_animate(my,"attack", percent, ANM_CYCLE);
       percent %= 100;
       // rotate to the target
       vec_set(temp,player.x); 
       vec_sub(temp,my.x);
       vec_to_angle(my.pan,temp); // now MY looks at YOU
    }   

    // traced something in range AND is the player
    if((c_trace (my.x, player.x, IGNORE_ME | IGNORE_PASSABLE) < ent.vision_range) && (you == player))
    {  
       d2 = vec_dist(my.x,player.x);
       //im moving toward the player
       if((d2 <= d1) && ( ent.health > 30))
       {         	
          ent.state = MODE_ATTACK;
       }
    }
    else
    {
       ent.state = MODE_WALK;
    }    
    
    wait(1);
    
    check_downward(&ent, vmin, vmax);

  }
}

function all_events() 
{
  switch (event_type)
  {
    case EVENT_BLOCK:
      
      return;
    
    case EVENT_ENTITY: 
    
      return;
  }
}  
[code]


Re: How do I...structures [Re: monchito] #234321
11/02/08 07:14
11/02/08 07:14
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Dunno whats going on here, your entity3d objects keep corrupting
themselves for some reason.

Gimme a day and I'll give it a re-write, keeping the same basic layout
and functionality to see if the problem persists.

Should be done by this time tomorrow at the latest. I'll leave a post
of my progress if Im not done by then.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: How do I...structures [Re: EvilSOB] #234334
11/02/08 10:38
11/02/08 10:38
Joined: May 2006
Posts: 53
Puerto Rico
monchito Offline OP
Junior Member
monchito  Offline OP
Junior Member

Joined: May 2006
Posts: 53
Puerto Rico
Here is the only way i can find to make it work. Making the structures global
Code:
[/code]
#include <acknex.h> 
#include <default.c>
////////////////////////////////////////////////////////////////////
var video_depth = 32; // 32 bit mode
var d3d_lockable = 1;

typedef struct
{
   var health;
   var shield;
   var count;
   var state;
   var pathnode;
   var lin_speed;
   var rot_speed;
   var anim_speed;
   var range;
   var weapon_type;
   var weapon;
   var weapon_range;
   var ammo;
   ENTITY* model;
}entity3d; 

entity3d npc[5];
STRING* buff = "          ";
static int NPC_Counter = 0;

entity3d* player1 = &npc[0];
entity3d* enemy1 = &npc[1];
entity3d* enemy2 = &npc[2];
entity3d* enemy3 = &npc[3];

#define MODE_STAND  0
#define MODE_WALK   1
#define MODE_RUN    2
#define MODE_DEATH  3
#define MODE_ATTACK 4
#define MODE_STOP   5
#define MODE_MARCH  6
#define MODE_STANDBY 7
 
STRING* work01_wmb = "flatplane.wmb";  // flat plane for test
ENTITY* any;
function all_events(); 
function act_player(entity3d* ent);
function check_downward(ent, vmin, vmax);
function act_enemy(entity3d* ent);
function loadEntity();

////////////////////////////////////////////////////////////////////
function main()
{
   fps_max = 50;
   level_load (work01_wmb);
   wait(5);
   loadEntity();
}

function act_player(entity3d* ent)
{
  var speed = 5;
  my = player = ent.model;
  set(ent.model, INVISIBLE);
  my.scale_x = .5;
  my.scale_y = .5;
  my.scale_z = .5;
  my.emask |= (ENABLE_BLOCK | ENABLE_ENTITY | ENABLE_IMPACT);
  my.event = all_events;

  while(1)
  {
     camera.x = my.x;
     camera.y = my.y;
     camera.z = my.z + 5;
     camera.pan = my.pan;
     camera.tilt = my.tilt;

     move_mode = IGNORE_PASSABLE + USE_BOX;  
      
     if(key_w == 1)
     { 
        ent_move (vector(speed * time_step,0,0), nullvector);

        my.z += .5 * sin(total_ticks * 25) * time_step;


        if(my.tilt < 1)
           my.tilt += .25;
        
        if(my.tilt > 1)
           my.tilt -= .25;
     }
     if(key_s == 1)
     {
     	ent_move (vector(-speed * time_step,0,0), nullvector);

        if(my.tilt < 1)     //tilted down?...recover
           my.tilt += .25;

        if(my.tilt > 1)     // tilted up... recover
           my.tilt -= .25;
     }

     if(key_a == 1) 
        my.pan += 3 * time_step;
     
     if(key_d == 1)
        my.pan -= 3 * time_step;
     
     if(key_q == 1)
     { 
       if(my.tilt > 30)   // maximun tilt allow
          my.tilt = 30;
          
       my.tilt += 1;
     }

     if(key_z == 1)
     {
     	 if(my.tilt < -30)  // minimum tilt allow
     	    my.tilt = -30;
     	 
     	 my.tilt -= 1;
     }
    
   check_downward(my, 10, 13);  // put me on the surface

   wait(1);
  }
}

function loadEntity()
{
   zero(npc[0]);
   npc[0].model = ent_create ("player.mdl", vector(270,120,20), NULL);
   act_player(npc[0]);

   zero(npc[1]);
   npc[1].model = ent_create ("gangst1.mdl", vector(300,-300, 25), NULL);   
   act_enemy(npc[1]);  
}

function all_events() 
{
  switch (event_type)
  {
    case EVENT_BLOCK:
      
      return;
    case EVENT_ENTITY: 

      return;
  }
}  

function check_downward(ent, vmin, vmax)
{
    proc_kill(4);
    var dist;
    VECTOR temp;
    
    my = ent;

    // check downward
    vec_set (temp, my.x);
    temp.z = my.z - 15000;
    trace_mode = IGNORE_ME + IGNORE_SPRITES + USE_BOX + IGNORE_PASSABLE;
    dist = trace (my.x, temp);
    
    // stay above ground
    if(dist < vmin) { my.z += 1; }
    if(dist > vmax) { my.z -= 1; }
    if(dist > (vmax + 5)) { my.z -= 3; }
    if(dist < (vmin - 4)) { my.z += 3; }   
}


function act_enemy( entity3d* ent )
{
  var percent;
  var d1;
  var d2;
  var vmin = 0;
  var vmax = 3;
  VECTOR temp;
  var dist_trace;

  my = ent.model;
  my.scale_x = .4;
  my.scale_y = .4;
  my.scale_z = .4;
      
  while(!player){wait(1);}

  ent.state = MODE_WALK;       	

  ent.health = 100;
  ent.range = 250;
  while(ent.health > 10)
  {
     d1 = vec_dist(ent.model.x, player.x);

    if (ent.state == MODE_WALK)
    {
       percent += 3 * time_step;
       ent_animate(ent.model,"walk", percent, ANM_CYCLE);
       percent %= 100;
    }
    

    if (ent.state == MODE_ATTACK)
    {
       vec_set(temp,my.x);
       if(vec_dist(me.x, player.x)<1000 && vec_to_screen(temp,camera)!=NULL)
       {
  	  str_for_num(buff, ent.health);
  	  draw_text(buff, temp.x, temp.y-20, vector(200,200,200));
       }
       // perform animation
       percent += 3 * time_step;
       ent_animate(my,"attack", percent, ANM_CYCLE);
       percent %= 100;
       // rotate to the target
       vec_set(temp,player.x); 
       vec_sub(temp,my.x);
       vec_to_angle(my.pan,temp); // now MY looks at YOU
    }   

    // traced something in range AND is the player
    if((c_trace (my.x, player.x, IGNORE_ME | IGNORE_PASSABLE) < ent.range) && (you == player))
    {  
       d2 = vec_dist(my.x,player.x);
       //im moving toward the player
       if((d2 <= d1) && ( ent.health > 30))
       {         	
          ent.state = MODE_ATTACK;
       }
    }
    else
    {
       ent.state = MODE_WALK;
    }    
    wait(1);
    check_downward(my, vmin, vmax);
  }
}
[code]


Re: How do I...structures [Re: monchito] #234423
11/02/08 21:36
11/02/08 21:36
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
GOT IT!
Now that I know what the problem is, its blindingly obvious. I'll leave it up to you to fix, but this is what is happening.
Everything im talking about here currently lives in your loadEntity() function.

1> This NEW chunk works fine because it is a pointer, and doesnt get emptied when this function finishes.
But you'll need to remember to use "free(hero);" when you're completely done with the object.
Code:
function loadEntity()
{
   ...
   entity3d* hero = malloc(sizeof(entity3d));
   zero(*hero);
   hero.model = ent_create("player.mdl", vector(70,120,20), NULL);
   act_player(hero);
   ...
}

2> This chunk works of yours fine because its globally defined so doesnt need "free()" to be used, but is ugly to look at.
Code:
entity3d npc[5];

function loadEntity()
{
   ...
   zero(npc[0]);
   npc[0].model = ent_create("maddog.mdl", vector(270,100,20), NULL);
   act_enemy(npc[0]);
   ...
}

3> This chunk fails because it is a local object only, and causes a crash about one frame after it finishes.
Code:
function loadEntity()
{
   ...
   entity3d gangsta;
   zero(gangsta);
   gangsta.model = ent_create("gangst1.mdl", vector(270,140,20), NULL);
   act_enemy(gangsta);
   ...
}
The reason chunk 3> causes a crash is because once this function (loadEntity) is completed and finishes,
the object "gangsta" is removed, and its space in memory can be overwritten by anything else at any time.
BUT, act_enemy(..) or act_player(...) are still using a pointer to this 'empty' space !!
Next time you try to access the contents of what WAS gangsta, you're going to get random data. CRASH...F%%k!
It may even work for several frames, but eventually something will use the empty space and then go splat.

So unless you use code chunks like 1> or 2>, you MUST make sure the function containing the declaration of
"entity3d SomeEntity;" keeps running until act_enemy(..) or act_player(...) have been closed off,
and SomeEntity can be destroyed.

I hope Ive made it understandable, and if youve got any questions, fire away.
Best of luck.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Page 1 of 2 1 2

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