Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
0 registered members (), 18,495 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Dynamically create special entities? #178982
01/21/08 02:38
01/21/08 02:38
Joined: Mar 2007
Posts: 677
0x00000USA
M
MrCode Offline OP
User
MrCode  Offline OP
User
M

Joined: Mar 2007
Posts: 677
0x00000USA
I've created this data structure for use with entities that will have a special "magnetism" property:

Code:

typedef struct MAGNET
{
ENTITY* entity;
ENTITY* opt1,opt2;
bool pole;
}MAGNET;



Now I need to be able to create many of these (they will be visible physics entities, so it needs to work like ent_create). How would I achieve this?

I was thinking that it would be a function that would return a MAGNET*, kinda like this:

Code:

MAGNET* mag_create(STRING* filename,VECTOR* pos)
{
...(no idea how an of this would work)...
return [whatever wierd MAGNET pointer this thing would spit out];
}



So, does ne1 have any ideas?


Code:
void main()
{
    cout << "I am MrCode,";
    cout << "hear me roar!";
    system("PAUSE");
}
Re: Dynamically create special entities? [Re: MrCode] #178983
01/22/08 11:39
01/22/08 11:39
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
do a search for malloc(). i'm pretty sure that's the general way to create a "global" struct on the fly. i did something similar with a custom physics engine, and in the "phys_create" function i'd add each new PHYS_ENT into a linked list so i could search through them all at any given time.

very useful

i don't remember how to use malloc() though. search the forum -- there should be a fair amount of examples.

julz


Formerly known as JulzMighty.
I made KarBOOM!
Re: Dynamically create special entities? [Re: MrCode] #178984
01/22/08 14:19
01/22/08 14:19
Joined: Jan 2008
Posts: 49
Sweden
K
Kenchu Offline
Newbie
Kenchu  Offline
Newbie
K

Joined: Jan 2008
Posts: 49
Sweden
Code:
#include <acknex.h>
#include <default.c>

typedef struct LOLZ
{
STRING* name;
} LOLZ;

LOLZ* mk_lolz(STRING* name)
{
LOLZ* tmp = malloc(sizeof(LOLZ));
tmp.name = name;
return tmp;
}

LOLZ* my_lolz;

function main()
{
my_lolz = mk_lolz("howdee");
}



Think thatll work. But im not sure if its done correctly since the string hasnt been alloocated dynamically. But ive got a feeling that lite-c is doing that automatically somehow.

Last edited by Kenchu; 01/22/08 17:41.
Re: Dynamically create special entities? [Re: Kenchu] #178985
01/23/08 01:26
01/23/08 01:26
Joined: Mar 2007
Posts: 677
0x00000USA
M
MrCode Offline OP
User
MrCode  Offline OP
User
M

Joined: Mar 2007
Posts: 677
0x00000USA
Ok, I've come up with this function... Tell me what you think. Is this going to work, or do I have it all wrong?

Code:

MAGNET* mag_create(STRING* filename,VECTOR* pos,int pole)
{
MAGNET* tmp= malloc(sizeof(MAGNET));
tmp->pole= pole;
switch(pole)
{
case POSITIVE:
{
ENTITY* ent_post= ent_create("posmagnet.mdl",curpos,phys_objsphere);
tmp->entity= ent_post;
}
case NEGATIVE:
{
ENTITY* ent_negt= ent_create("negmagnet.mdl",curpos,phys_objsphere);
tmp->entity= ent_negt;
}
}
return tmp;
}



I looked up malloc() and sizeof() on Wikipedia. I take it what I'm doing when I write "MAGNET* tmp= malloc(sizeof(MAGNET));" is I'm allocating in memory the data size of the MAGNET struct. How do I use this to keep generating more MAGNET*s? doesn't the first MAGNET* get overwritten by the next one that is generated? Or is there more I should know about malloc()?


Code:
void main()
{
    cout << "I am MrCode,";
    cout << "hear me roar!";
    system("PAUSE");
}
Re: Dynamically create special entities? [Re: MrCode] #178986
01/31/08 23:58
01/31/08 23:58
Joined: Jan 2008
Posts: 49
Sweden
K
Kenchu Offline
Newbie
Kenchu  Offline
Newbie
K

Joined: Jan 2008
Posts: 49
Sweden
Quote:

Ok, I've come up with this function... Tell me what you think. Is this going to work, or do I have it all wrong?

Code:

MAGNET* mag_create(STRING* filename,VECTOR* pos,int pole)
{
MAGNET* tmp= malloc(sizeof(MAGNET));
tmp->pole= pole;
switch(pole)
{
case POSITIVE:
{
ENTITY* ent_post= ent_create("posmagnet.mdl",curpos,phys_objsphere);
tmp->entity= ent_post;
}
case NEGATIVE:
{
ENTITY* ent_negt= ent_create("negmagnet.mdl",curpos,phys_objsphere);
tmp->entity= ent_negt;
}
}
return tmp;
}



I looked up malloc() and sizeof() on Wikipedia. I take it what I'm doing when I write "MAGNET* tmp= malloc(sizeof(MAGNET));" is I'm allocating in memory the data size of the MAGNET struct. How do I use this to keep generating more MAGNET*s? doesn't the first MAGNET* get overwritten by the next one that is generated? Or is there more I should know about malloc()?




No, since youre returning a pointer to an area in the memory which youve already allocated. If you wouldve created a local MAGNET object in a function and then returned THAT magnet as a pointer, then things probably wouldve gone real mad after a while (you wouldve returned a pointer to an area which could have anything in it. Local variables are "cleaned" when they reach the end of their block).

When you do:

MAGNET* tmp= malloc(sizeof(MAGNET));

You callocate a memory, and store the adress in tmp. So when you later on return tmp, you arent returning an "objecT", youre returning a pointer to that space you created (usually something like 00FA0E63). It's more or less just a number that you are returning, but since you store that number as a pointer of MAGNET (i.e. MAGNET* my_pointer = mag_create("lolz", vector(0,0,0), 1) for example), lite-c knows that it has the properties of MAGNET struct.

You can try having this in your main:

Code:

function main()
{
MAGNET* my_pointer = mag_create("lolz", vector(0,0,0), 1);
printf("%p", my_pointer); // Prints the location of what the pointer is pointing to.
}



Last edited by Kenchu; 02/01/08 00:11.
Re: Dynamically create special entities? [Re: Kenchu] #178987
02/01/08 04:48
02/01/08 04:48
Joined: Mar 2007
Posts: 677
0x00000USA
M
MrCode Offline OP
User
MrCode  Offline OP
User
M

Joined: Mar 2007
Posts: 677
0x00000USA
So basically what you're saying is, that I've allocated the number of the address of my "tmp" MAGNET pointer, so I can allocate the values of the struct to other places?

Does this mean I can use it like ent_create, in that I can create as many as I want (or at least as many as my PC's RAM will hold, which is a lot, )?


Code:
void main()
{
    cout << "I am MrCode,";
    cout << "hear me roar!";
    system("PAUSE");
}
Re: Dynamically create special entities? [Re: MrCode] #178988
02/02/08 11:25
02/02/08 11:25
Joined: Jan 2008
Posts: 49
Sweden
K
Kenchu Offline
Newbie
Kenchu  Offline
Newbie
K

Joined: Jan 2008
Posts: 49
Sweden
Quote:

So basically what you're saying is, that I've allocated the number of the address of my "tmp" MAGNET pointer




Exactly, tmp is just a reference number, like a note with an adress to a house on it. You can have several seperate notes with the same adress on them, pointing to the same house (allocated space). That can however be dangerous, if you were to free the memory of the allocated space (i.e. remove the house), and still having notes with the adress to it (pointers), the program could crash if you were to try to use these pointers. So having several pointers doesnt mean you have several instances of that struct instance you created with malloc. It's still just one instance, but which you can have as many pointers to it as you want to. Usually you dont need more than one.

Quote:

so I can allocate the values of the struct to other places?




Im not quite sure if i understand this correctly but, yes, you can allocated the values of the struct to other places. I think you only use the term "allocated" when you store data dynamically (i.e. on the heap, i.e. with malloc). But remember that pointers and regular values are different.

Quote:

Does this mean I can use it like ent_create, in that I can create as many as I want (or at least as many as my PC's RAM will hold, which is a lot, )?




Yes, exactly. env_create probably works pretty much the same. It returnes an ENTITY pointer to a space it has allocated, and initialized its members within the struct.

Last edited by Kenchu; 02/02/08 11:32.
Re: Dynamically create special entities? [Re: Kenchu] #178989
02/02/08 18:56
02/02/08 18:56
Joined: Mar 2007
Posts: 677
0x00000USA
M
MrCode Offline OP
User
MrCode  Offline OP
User
M

Joined: Mar 2007
Posts: 677
0x00000USA
Awesome! I just tested the printf() thing, and it works! It even created the entity I wanted. I'll need to resize it in MED, though, as it's a little too big.

EDIT: ok, I have a problem. I have set up a test function for using mag_create:

Code:

void mag_toggle()
{
while(1)
{
switch(key_lastpressed)
{
case 25:
mag_create("posmagnet.mdl","negmagnet.mdl",curpos,POSITIVE);
case 49:
mag_create("posmagnet.mdl","negmagnet.mdl",curpos,NEGATIVE);
}
wait(1);
}
}



The thing is, when I press P or N (that's the keys the scancodes correspond to), it creates tons of little magnet models (they don't attract/repel each other yet, the math for the actual behavior hasn't been worked out, but I have an idea). maybe this has to do with the fact that I'm using a while loop?

I've also modified the mag_create function a little, too, but it shouldn't be of any real consequence:

Code:

I had remembered that the STRING* I had set up as a parameter wasn't being used, so
I've created an additional one for a secondary model (the negative magnet), and I've
incorporated the STRING*s in the ent_create functions within mag_create:

MAGNET* mag_create(STRING* filename1,STRING* filename2,VECTOR* pos,int pole)
{
MAGNET* tmp= malloc(sizeof(MAGNET));
tmp->pole= pole;
switch(pole)
{
case POSITIVE:
{
ENTITY* ent_post= ent_create(filename1,curpos,phys_objsphere);
tmp->entity= ent_post;
}
case NEGATIVE:
{
ENTITY* ent_negt= ent_create(filename2,curpos,phys_objsphere);
tmp->entity= ent_negt;
}
}
return tmp;
}


(btw, removing the while loop makes it not work.)

Last edited by MrCode; 02/02/08 19:58.

Code:
void main()
{
    cout << "I am MrCode,";
    cout << "hear me roar!";
    system("PAUSE");
}
Re: Dynamically create special entities? [Re: MrCode] #178990
02/02/08 20:03
02/02/08 20:03
Joined: Mar 2007
Posts: 677
0x00000USA
M
MrCode Offline OP
User
MrCode  Offline OP
User
M

Joined: Mar 2007
Posts: 677
0x00000USA
ok, nvr mind. It works now. I just had to set the on_p and on_n vars to my mag_toggle function (now without the while loop).


Code:
void main()
{
    cout << "I am MrCode,";
    cout << "hear me roar!";
    system("PAUSE");
}
Re: Dynamically create special entities? [Re: MrCode] #178991
02/03/08 05:40
02/03/08 05:40
Joined: Mar 2007
Posts: 677
0x00000USA
M
MrCode Offline OP
User
MrCode  Offline OP
User
M

Joined: Mar 2007
Posts: 677
0x00000USA
Alright. I believe the time has come to start assigning behavior to these "magnets". I was thinking of using a spherical c_scan to detect nearby entities, and if it detects one or more, it will attract to it (or repel it), and the same goes for the others (because they also have their own c_scan).

I know the attraction/repulsion thing is going to involve a bit of trigonometry, and I've got that part worked out (mostly), but how can I use c_scan to detect whether the magnet is POSITIVE or NEGATIVE, if I can at all?

Are the me and you pointers universal, or can they only be used with entities?

Last edited by MrCode; 02/03/08 05:43.

Code:
void main()
{
    cout << "I am MrCode,";
    cout << "hear me roar!";
    system("PAUSE");
}
Page 1 of 2 1 2

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

Gamestudio download | 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