Problem with Remove

Posted By: Towelie

Problem with Remove - 10/08/06 14:02

Nevermind my original problem. I found out that it needed to be ent_remove, not just remove.
However, I get no errors but the code doesnt do what its supposed to. I suppose its not loopign right but I dont know why...
Heres the code.
Code:
 
var vec_dist_var=0;
var player_vector[3];

action vec_dist_test
{
while(1)
{
player_vector.x=my.x;
player_vector.y=my.y;
player_vector.z=my.z;
wait(1);
vec_dist_var=vec_length(player_vector);
wait(1);
IF(vec_dist_var>=100)
{
ent_remove(me);
return;
}
wait(1);
}
}



I put in numbers... and itll appear if its over 70, because thats how far the player is from the object, however, it I move away from it it stays like its not suuposed to. Any help?
Posted By: Xarthor

Re: Problem with Remove - 10/08/06 14:15

Never (!) run ent_remove() from within:
- a while(1) loop
- an event function

Use this:
Code:

var vec_dist_var=0;
var player_vector[3];

action vec_dist_test
{
while(!my.skill20)
{
vec_set(player_vector.x,my.x);
wait(1);
vec_dist_var=vec_length(player_vector);
IF(vec_dist_var>=100) { my.skill20 = 1; }
wait(1);
}
wait(1);
ent_remove(me);
}



Uhm but what exactly want you the code to do?
Sorry didn't understand what you said in your first post.
Posted By: Towelie

Re: Problem with Remove - 10/08/06 14:18

I wanted the code to
1) Check how far from the player an object is (the object holding the action vec_dist_test)
2)If the player goes over 300 units from the object, the object gets removed.

Your code wouldn't work then would it? The ent_remove is outside the if function.
Posted By: Xarthor

Re: Problem with Remove - 10/08/06 14:20

Ok why not use this:
Code:

define distance,skill1;
define max_dist,skill2;
define kill_skill,skill3;

action dist_object
{
my.max_dist = 300;

// wait until the player was created:
while(!player) { wait(1); }

//while my.kill_skill == 0 ..
while(!my.kill_skill)
{
//store the distance in a skill
my.distance = vec_dist(my.x,player.x);

//compare
if(my.distance > my.max_dist)
{ my.kill_skill = 1; } //its bigger so kill the while
}
//wait a frame
wait(1);
//remove me:
ent_remove(me);
}

undef distance;
undef max_dist;
undef kill_skill;



Edit:
my code will work, caus it uses a boolean var (the kill_skill) as condition for the while loop.
So if you set the kill_skill to non-zero the while loop will be "killed" and the entity proceeds with the code which comes after the while loop.
So it waits one frame (safety reasons), and then removes itself.
Posted By: Towelie

Re: Problem with Remove - 10/08/06 14:30

Hmm, a few things:
1) That looks like a foreign language... which it still is so far. I've been scripting for about a week, and its mostly been not very advanced stuff with buttloads of errors I've worked out.
2) The code itself, the one you gave me, didn't work. I set it to as low as 1 and it didn't remove the entity.

Thanks for the help so far!
Posted By: Xarthor

Re: Problem with Remove - 10/08/06 14:37

It might not get removed because you have no entity which has the player pointer set to it.

Are you using the templates?

btw: that is no foreign language but c-script

Edit:
Ok I changed the code slightly.
It now checks the distance to the camera and not to the player entity:
Code:

define distance,skill1;
define max_dist,skill2;
define kill_skill,skill3;

action dist_object
{
my.max_dist = 300;
my.kill_skill = 0;

//while my.kill_skill == 0 ..
while(!my.kill_skill)
{
//store the distance in a skill
my.distance = vec_dist(my.x,camera.x);

//compare
if(my.distance > my.max_dist)
{ my.kill_skill = 1; } //its bigger so kill the while
}
//wait a frame
wait(1);
//remove me:
ent_remove(me);
}

undef distance;
undef max_dist;
undef kill_skill;


Posted By: Towelie

Re: Problem with Remove - 10/08/06 14:41

Now that I take a closer look at it...
Should my.x, my.y and my.z beplayer.x, player.y and player.z?
I tried this and I come up with an empty pointer for each one over and over again.
I'm using the a6 walkthrough template for my player action, because I cannot write my own yet.
However, the script you are looking at is in a separate file (I included it)
Posted By: Towelie

Re: Problem with Remove - 10/08/06 17:06

Ok well I went back a step and tested something simpler. Again, I am gettign a constant empty poitner error.
New code:
Code:

var vec_dist_var=0;
var player_vector[3];
var vec_varx=0;

action vec_check
{
while(1);
{
vec_varx=vec_dist(my.x,player.x);
wait(1);
if(vec_varx<=100)
{
ent_remove(me);
return;
}
wait(1);
}

}

action vec_dist_test
{
while(1)
{
player_vector.x=player.x;
player_vector.y=player.y;
player_vector.z=player.z;
wait(1);
vec_dist_var=vec_length(player_vector);
wait(1);
IF(vec_dist_var>=100)
{
ent_remove(me);
return;
}
wait(1);
}
}



The top action is the new action while the bottom action is the original problem to this thread.
Posted By: Xarthor

Re: Problem with Remove - 10/08/06 17:26

Add the following while loop BEFORE your while(1) loop:
while(!player) { wait(1); }
This will wait until the player pointer gets non-zero.
So it prevents any empty pointer message.
However I think the player pointer will no be set at all, so try out the edited code from me above, which works with the camera position.

Another thing:
Instead of writing:
vector.x = bla.x
.y = .y
and so on, you can simply write:
vec_set(player_vector.x,player.x);
This does exactly the same as your 3 lines in the action "vec_dist_test"

However there is no need to store this, caus you can also pass entity parameters (such as positions, skills) to a engine function.

Next thing is:
vec_length does not return the distance of your player to the object, but the distance to the level origin.

And:
As I said do NOT remove an entity from within its while loop.
Instead use a skill (set to zero) as a condition for the while loop.
so: while(!my.skill20) will run as long as my.skill20 == 0
Set skill20 to non-zero and the while loop will be aborted and the action will continue with the stuff which comes behind the while loop.
Take a close look at my example thats exactly what I'm doing.
Posted By: Towelie

Re: Problem with Remove - 10/08/06 19:14

But I'm not that advanced, I dont even know how to use skills in that way yet.
I'm using Kinji's Noob Way manual, which was designed for a5. I know this is the source of many problems, but stuff like this should be the same no?
Posted By: Xarthor

Re: Problem with Remove - 10/08/06 19:38

Ok let me try to explain to you what skills are and how to use them:
A skill is nothing different than a variable but this var is attached to an object (entity for example).
So this skill is like a property of an object.
In this skill you can store numbers and access them through the entity pointer (my, you e.g.) a dot . and the skill name or - if you didn't define a skill name - the default skill name like skill1, skill2 ...
E.g.:
my.skill1 = 123; //stores the number 123 into the first skill of the entity "my"

Defining a skill:
As I said you can define names for skills.
This is used to make code easier to read plus you wouldnt have to remember which skill does what when you have names which pretty much tell about their use.
Now lets define a skill name.
The skill name definition consists of 3 parts:
1. the keyword "define"
2. the name of the skill "name"
3. the skill number so to speak
So here is a skill defining:
define age,skill1;
age: name of skill1

So now we don't have to write:
my.skill1 = 18; anymore
but we can use age as name:
my.age = 18;

The undefining:
I recommend to undefine a skill at the end of a script file, unless it should be used in a different wdl file aswell.
However errors like: "skill1 defined twice" or something similiar are really annoying.
So we just add the following line at the end of our script file (for the "age" example):
undef age; //yep thats all

Now how to access skills of other entitys:
Lets assume we have 2 entities, the bot_a and bot_b, both have a skill1 named age and we want bot_a to get bot_b's age and compare it to its own age.
So the first new thing are entity pointers.
pointers point towards an entity and are no different than my and you, however my and you are modified by some instructions and better left how they are for now.

So lets create two entity pointers then:
entity* bot_a;
entity* bot_b;

Now we can assign on of thoses pointers to any entity.
However each pointer can only point towards ONE entity at a time.

So lets script thoses dudes then.
First bot_b, because he just stands there and has his age:
Code:

define age,skill1;

entity* bot_a;
entity* bot_b;

action bot_b_act
{
bot_b = my; //now the pointer bot_b points to the entity with the action bot_b_act
my.age = 18;
}


Good, so now we add the bot_a, but to make sure that we don't get an invalid pointer error we must add a safety instruction to make sure that an entity with the bot_b pointer pointing to it exists:
Code:

action bot_a_act
{
bot_a = my;
my.age = 15;

//This is our safety line:
while(!bot_b) { wait(1); }
/* it waits until the entity pointer bot_b points towards an entity
and is getting non-zero that way */
beep; //if you hear the beep sound, the bot_b exists

//now lets get the age of bot_b and compare it to our age:
if(bot_b.age < my.age)
{
beep; //he is younger than we are, so beep once
}
else
{
beep; beep; //he is older, so beep twice
}
}



I hope this helped you a bit.

My advice would be to first learn the basics, like vars, arrays, skills, pointers
And then try to write such actions.
Because if you once got used to bad programming style it will be hard to get away from it.
© 2024 lite-C Forums