Game Development Community

object reading others behavior?

by rennie moffat · in iTorque 2D · 10/25/2011 (1:13 pm) · 22 replies

I have run into something completely unexpected. I have a behavior, it is attached to objectX and objectY. The behavior tells objectOwner to move to pts A B C D. I do it on a count, moving at xSpeed. So...

function moverShaker_behavior::onPositionTarget(%this)
{
	%count = %count + 1;
	
	echo("frmtnSldr_bhvr::onBvAdd::onPosTarg");
	echo("frmtnSldr_bhvr::onBvAdd:: %this.name  " @ %this.name);
	echo("frmtnSldr_bhvr::onBvAdd:: %count  " @ %count);
	
	if(%count == 1)
	{
		//means owner has reached pos1
		%this.owner.moveTo(%this.pos2, %this.speed, true, true, true, 0.1);
		echo("frmtnSldr_bhvr::onBvAdd:: %this.name " @ %this.name);
	}
	if(%count == 2)
	{
		//means owner has reached pos1
		%this.owner.moveTo(%this.pos3, %this.speed, true, true, true, 0.1);
		echo("frmtnSldr_bhvr::onBvAdd:: %this.name " @ %this.name);
	}
}

What seems to be happening is that the objects are using each others pts for some reason as their counts are equal, the function is the same. Is it possible for the object to be reading the others instructions????

This is freaking me out.





About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.

Page «Previous 1 2
#1
10/25/2011 (8:04 pm)
I know this is probably nonsense, but when I have one object with this behavior, it acts appropriately, with two, they act seemingly random.

??
weird, hallucinating? please fill me in.
#2
10/27/2011 (9:58 am)
Hi there,
can anyone, if you have a minute, please go over this behavior. The problem is... that if I have one object, with this behavior, in my scene, it works fine, but with two or more objects owning it, things get funky.

The point of the behavior is for the owner, to start at pt0 (point 0), then move to points 1, 2, 3, ... etc, in a straight line along x, then along y, then along x, then along y... Again, it works perfectly with one object in the scene. But if I attach it to others, they all begin to act, well, weird. Going off in all angles, not taking the pattern I would expect.


Is this possible?? That other objects are reading the others functions?? very wierd and unexpected.


pastebin.com/QYZF4Hgu
#3
10/27/2011 (4:52 pm)
I am really serious here. Is there an obvious, stupid mistake I am making?



#4
10/27/2011 (11:33 pm)
A suggestion, you could replace your all 14 similar looking if statements (like the one below)...

if(%count == 1)
{
//means owner has reached pos1
%this.owner.moveTo(%this.pos2, %this.speed, true, true, true, 0.1);
//echo("frmtnSldr_bhvr::onBvAdd:: %this.name " @ %this.name);
}

... with the following.

if(%count >= 14){
%nr = 0;
%count = 0;
}else{
%nr = %count +1;
}
%this.owner.moveTo(%this.pos[%nr], %this.speed, true, true, true, 0.1);

You would also need to adjust the initiation of the %this.pos[] list, so that your lines,
%this.pos1 = %this.pos0.x SPC %this.pos0.y + 130;
instead becomes,
%this.pos[1] = %this.pos0.x SPC %this.pos0.y + 130;
#5
10/28/2011 (5:15 am)
Your %count is a local variable so I would expect that to be some random initial value everytime.

You should assign the count to the behavior and refer to %this.count?

Also, use a switch statement for those tests, you will get a little bit of a speed boost or do what Simon said.
#6
10/28/2011 (7:50 am)
Thanks Guys,

@Scott you were right. The %count was causing it. I just thought that since it was local, it did not need a pointer. that ONLY THAT FUNCTION IN THAT BEHAVIOR could see it, but instead, any function, of that behavior could see it. Hmm cool. Thanks.

As far as the switch case goes, can you give me a quick example of how you would apply it? My mind, looking at this example, can not figure out, mostly, what %myVaribale should be and how to apply it.

Thanks.
%myVariable = "test";  
  
switch$(%myVariable)  ////what would %myVariable be in my case
{  
   case "blah":  
      %results = "found blah";  ///what would the %results be? ..unsure. 
   case "test":  
      %results = "found test";  
   default:  
      %results = "found didly squat";  
}


@Simon, again, thank you. I am sure your method will work however I am just sticking with swapping the variable %count to %this.count. However, your way of using "[%nr]" is something I am just getting into now and seeing you apply it here has given me more confidence to use it in the future.


Cheers guys, much obliged.

::))((
#7
10/28/2011 (11:43 am)
%myVariable would be %this.count

The results line is what you have after your if (%this.count == <somevalue>).
#8
10/28/2011 (12:40 pm)
Hi Scott, I am slightly confused by that. I did a quick test, going on what I think you said....

function StatusBhvr::switchIttIUp(%this)
{
	%this.schedule(1000, switchIttIUp);
	echo("switchIttIUp");
	%this.count = %this.count + 1;
	
	switch$(%this.count)
	{
		case "loadLeft":
		echo("loadLeftNow");
		case "loadRight":
		echo("loadRightNow");		
	}
}

Yields no echos from inside the cases.

??? not sure what I am doing wrong.

#9
10/28/2011 (3:12 pm)
Rennie, this is what you should have

function formationSoldier_behavior::onPositionTarget(%this)
{
	
	if(!isObject(%this.name)) 
	return;

	%count = %count + 1;
	echo("frmtnSldr::onPosTar %this.name @ %count " @ %this.name @ %count);	
	echo("frmtnSldr::onPosTar %this.name" @ %this.name);	
	
	//echo("frmtnSldr_bhvr::onBvAdd::onPosTarg");
	//echo("frmtnSldr_bhvr::onBvAdd:: %this.name  " @ %this.name);
	//echo("frmtnSldr_bhvr::onBvAdd:: %count  " @ %count);

        switch (%this.count) 
	{
             case 1:
		//means owner has reached pos1
		%this.owner.moveTo(%this.pos2, %this.speed, true, true, true, 0.1);
		//echo("frmtnSldr_bhvr::onBvAdd:: %this.name " @ %this.name);
             case 2:
 		//means owner has reached pos1
		%this.owner.moveTo(%this.pos3, %this.speed, true, true, true, 0.1);
		//echo("frmtnSldr_bhvr::onBvAdd:: %this.name " @ %this.name);
             case etc etc
	}
#10
10/28/2011 (3:37 pm)
Thanks Scott,
I am just wondering is the 1 in case 1 associated with the actual value of %this.count or is it just upon entering the function the first time, it goes to case 1 etc?
#11
10/29/2011 (2:03 pm)
The 1..2..3 is the value of %this.count - it will switch to the value.
#12
10/29/2011 (2:59 pm)
so case 1, would suggest %this.count == 1? and so on?


And related, is an int all I can use to enter a switch$? In other words, can I use a "phrase"?
#13
10/30/2011 (1:29 am)
You can use a string as well.
#14
10/30/2011 (7:57 am)
Great, thanks Scott.
not to sound jerky, but how does the switch, save me computing power over the if?
#15
10/30/2011 (11:52 am)
In your original code you tested count for 1..2..3..4 etc

There was no need to e.g. test for count == 5 if you already have found it is equal to 2. Switch/case will stop checking when it has found a match.
#16
10/30/2011 (11:57 am)
Oh. I was under the impression that, if I say for instance....


if(x == 0)
{
///this
return; //// and by stating "return;" that all "if"s below are not checked.
}



....However, going on that logic, I suppose that this means that it will not check the ones above either!!! Ha. Brilliant. Thanks Scott! Please correct me if my logic is wrong.

Cheers.


#17
10/30/2011 (12:02 pm)



ps. tho... how does it not check?


For instance, if I have %caseVar = thisThat



so...



%caseVar = thisThat;


switch$(%caseVar)
case thisThatThing:
doThis;
case thisThatThere:
doThatThere;
case thisThat:
doThisThat;


How does, or more specifically, in my mind, I would think the computer still has to check each case... I am still unsure, if this is true, how it is more effecient, to the engine over say this scanario....


if(%case $= "thisThatThing")
doThisThat;
if(%case $= "thisThatThere")
doThisThere;
if(%case $= "thisThat")
doThisThat;






Sorry, but I am just trying to figure out the inner working of the program.






#18
10/30/2011 (12:05 pm)
This being said tho, I must say that if nothing else, the visual look, layout of switch, seems easier to digest as a reader and that alone is enough to use it.


With switch being an option, can you give me an example of when you use switch over if and vice versa... despite your teachings I still think they are one in the same.

#19
10/30/2011 (12:11 pm)
Rennie, sorry, I'm not being very clear.

In your original example at the very top you have multiple if tests. There are no return statements in the if block therefore every if will be checked even if count was equal to 1, you would still check for 2,3,4 etc

With the switch/case statement no more checks will be made once the first case that satisfies the value is reached e.g. if count was one then the first case statement will execute and no more. In your code you would still test for count == 2 in the if statement, thus switch/case is a little more efficient.

#20
10/30/2011 (12:15 pm)
Ok, sorry to be such a picky nerd about it but anyhow, thanks for the tips. I am sure it will come in handy.... but just to be clear, if a return exists in an if... it will negate checking thru the rest of the ifs... I know this is true.. anyhow, I do appreciate it. happy sunday!


Page «Previous 1 2