ent_create(..., ..., function(a, b, c)); possible?

Posted By: SomebodyNew

ent_create(..., ..., function(a, b, c)); possible? - 02/23/10 09:42

Hello everybody,

the function ent_create(STRING* filename, VECTOR* position, function); is used to create an entity and immediately assign a function to that newly created entity.
I would like to pass over values to the function that is assigned to the new entity. To do this I tried the following code:

Code:
// The basic includes
#include <acknex.h>
#include <default.c>

// List of functions
function create_scaled_entity(X_Scale, Y_Scale);
function main();

function create_scaled_entity(X_Scale, Y_Scale)
{
	my.scale_x = X_Scale;
	my.scale_y = Y_Scale;
}

function main()
{
	level_load(NULL);
	camera.tilt= -90;
	camera.z = 3000;
	
	ent_create("Test.mdl", nullvector, create_scaled_entity(5, 5));
}



This is pretty straight forward. The "main" function creates an entity via ent_create and assigns the function "create_scaled_entity" to this newly created entity.
This works until I try to pass over parameters to the new entity in the same line that creates it.
The engine crashes and returns an E1513 "Crash in ..." (mostly it is an empty-pointer-error).

Am I doing something wrong or is this not possible? If so, is there a workaround?
I don't really understand why this returns a pointer-error.

Thank you for taking a look at this.

Have a good start in the day.
Posted By: Helghast

Re: ent_create(..., ..., function(a, b, c)); possible? - 02/23/10 09:45

try this instead:
Code:
// The basic includes
#include <acknex.h>
#include <default.c>

// List of functions
function create_scaled_entity(ENTITY* Ent, var X_Scale, var Y_Scale);
function main();

function create_scaled_entity(ENTITY* Ent, var X_Scale, var Y_Scale)
{
	Ent.scale_x = X_Scale;
	Ent.scale_y = Y_Scale;
}

function main()
{
	level_load(NULL);
	camera.tilt= -90;
	camera.z = 3000;
	
	ENTITY* tempEnt = ent_create("Test.mdl", nullvector, NULL);
	create_scaled_entity(tempEnt, 5, 5)
}



Untested, but you get the idea wink

regards,
Posted By: pararealist

Re: ent_create(..., ..., function(a, b, c)); possible? - 02/23/10 10:07

OR maybe this (untested)
Code:
ENTITY*  create_scaled_entity( var Scale, STRING* model)
{
	ENTITY* tempEnt = ent_create(model, nullvector, NULL);
	if(tempEnt != NULL)
	{
		//scale all axis the same
		tempEnt.scale_x = Scale;
		tempEnt.scale_y = Scale;
		tempEnt.scale_z = Scale;
		//
		return tempEnt;
	}
	return NULL;
}


Posted By: hopfel

Re: ent_create(..., ..., function(a, b, c)); possible? - 02/23/10 10:24

Can I barge me in? ^^
Your ideas are not bad, but what can you do, if you want to use the "my" pointer? (Maybe to use IGNORE_ME or so)
The "my" from the entity-function is the same "my" from the function who runs the entity-function, or? So you get problems sometimes. (example: http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=306650&page=2 last post, last three lines :D)
Posted By: Helghast

Re: ent_create(..., ..., function(a, b, c)); possible? - 02/23/10 10:35

Originally Posted By: pararealist
OR maybe this (untested)
Code:
ENTITY*  create_scaled_entity( var Scale, STRING* model)
{
	ENTITY* tempEnt = ent_create(model, nullvector, NULL);
	if(tempEnt != NULL)
	{
		//scale all axis the same
		tempEnt.scale_x = Scale;
		tempEnt.scale_y = Scale;
		tempEnt.scale_z = Scale;
		//
		return tempEnt;
	}
	return NULL;
}



Doesnt make alot of sense to return NULL there, return the entity instead then, otherwise you might be right, this would be a better function laugh

regards,
Posted By: pararealist

Re: ent_create(..., ..., function(a, b, c)); possible? - 02/23/10 10:53

I take it you mean so?

Code:
ENTITY*  create_scaled_entity( var Scale, STRING* model)
{
	ENTITY* tempEnt = ent_create(model, nullvector, NULL);
	if(tempEnt != NULL)
	{
		//scale all axis the same
		tempEnt.scale_x = Scale;
		tempEnt.scale_y = Scale;
		tempEnt.scale_z = Scale;
		//		
	}
	return tempEnt;
}


Posted By: Gordon

Re: ent_create(..., ..., function(a, b, c)); possible? - 02/23/10 11:34

What no one has mentioned is that entity function are not allowed to have parameters. And the manual is currently of no help here. I guess you can think of it this way... the level editor allows you to assign actions to models and other entities. There is no way to assign a parameter to the assigned function in the level editor. ent_create is the script way of placing an entity in the level.

Also your code you first posted the create_scaled_entity(5, 5) is a call to that function not a pointer to that function with parameters. There is no way to code a pointer to a function AND pass parameters to it. This is why my is null as the entity has not been created yet and this function is not associated with the entity only the return value is used as the pointer to the action for this function.

So the bottom line is that entity action functions can not have parameters.
Posted By: Aku_Aku

Re: ent_create(..., ..., function(a, b, c)); possible? - 02/23/10 19:55

Totally agree...
Function from the Online Manual

Quote:
Quote:
Actions are another special kind of functions. They don't have parameters and don't return a value, but will appear in the action pop-up list in WED's entity property panel. So actions can be assigned to entities through WED, and are automatically executed after level loading. Action names are limited to 20 characters. Besides that, there is no difference between functions and actions. This is our usual example for an entity action, which consists of just three instructions, and lets the entity rotate


So actions can not have parameters.
Posted By: Gordon

Re: ent_create(..., ..., function(a, b, c)); possible? - 02/25/10 03:54

Good find. I forgot that was there.
Posted By: badapple

Re: ent_create(..., ..., function(a, b, c)); possible? - 02/25/10 04:37

its as simple as this ...

you = ent_create("yourmodel.mdl",vector(0,0,0),NULL);
you.scale_x=your value;
you.scale_y=you.scale_x;
you.scale_z=you.scale_x;

///pass on any peramiters you like///

set(you,PASSABLE | BRIGHT | TRANSLUCENT):
you.alpha=50;
Posted By: pararealist

Re: ent_create(..., ..., function(a, b, c)); possible? - 02/25/10 04:58

If you had said
Php Code:
I would like to change the created entity's values 



instead of
Php Code:
I would like to pass over values to the function that is assigned to the new entity. To do this I tried the following code: 



but i really should have realised what was meant.
© 2024 lite-C Forums