PDA

View Full Version : Adding a Custom Component to Proton SDK



rtr_18
01-13-2012, 09:06 AM
Hi!
I've to add a custom component to the SDK. The component has to move any entity (like an overlay entity or button entity)
from the top of the screen to bottom and from bottom top(i.e wrap over) for ever. What are the steps to add a custom component?
How to move an entity from top to bottom and bottom to top forever?

I'm doing some thing like this inside the OnAdd() function my entity:

GetMessageManager()->SetEntityVariable(pEnt, 3000, "pos2d", CL_Vec2f(GetScreenSizeX()/2, GetScreenSizeY()));
GetMessageManager()->SetEntityVariable(pEnt, 3000, "pos2d", CL_Vec2f(GetScreenSizeX()/2,0));

But it didn't do anything. Inserting the above code inside a infinite loop like for( ;; ) is also not working.
What to do?

Seth
01-13-2012, 12:30 PM
GetMessageManager()->SetEntityVariable(pEnt, 3000, "pos2d", CL_Vec2f(GetScreenSizeX()/2, GetScreenSizeY()));
GetMessageManager()->SetEntityVariable(pEnt, 3000, "pos2d", CL_Vec2f(GetScreenSizeX()/2,0));

Hmm... this would wait 3 seconds, and then instant teleport the entity to the bottom of the screen, and then top, in the same tick. Probably not what you wanted.

Have you worked through this tutorial (http://www.rtsoft.com/wiki/doku.php?id=proton_entity)? This shows how to move entities around and uses a technique that doesn't require making a new Component - check it out, should give you a good idea of how to do it.

rtr_18
01-13-2012, 12:54 PM
GetMessageManager()->SetEntityVariable(pEnt, 3000, "pos2d", CL_Vec2f(GetScreenSizeX()/2, GetScreenSizeY()));
GetMessageManager()->SetEntityVariable(pEnt, 3000, "pos2d", CL_Vec2f(GetScreenSizeX()/2,0));

Hmm... this would wait 3 seconds, and then instant teleport the entity to the bottom of the screen, and then top, in the same tick. Probably not what you wanted.

Have you worked through this tutorial (http://www.rtsoft.com/wiki/doku.php?id=proton_entity)? This shows how to move entities around and uses a technique that doesn't require making a new Component - check it out, should give you a good idea of how to do it.

I need a component any how. I used the following code:

if((pEnt->GetVar("pos2d")->GetVector2().y < GetScreenSizeY()) && (pEnt->GetVar("pos2d")->GetVector2().y >= 0))
{
//m_bDirForward = true;
GetMessageManager()->SetEntityVariable(pEnt, 3000, "pos2d", CL_Vec2f(GetScreenSizeX()/2, GetScreenSizeY()));
}
else
if((pEnt->GetVar("pos2d")->GetVector2().y > 0) && (pEnt->GetVar("pos2d")->GetVector2().y == GetScreenSizeY()))
{
//m_bDirForward = false;
GetMessageManager()->SetEntityVariable(pEnt, 6000, "pos2d", CL_Vec2f(GetScreenSizeX()/2,0));
}
But the entity is moved out of the window(my requirement is: it has to be moved to the bottom first and then to the top once it reaches the bottom).
This has to be done repeatedly for the entity.

rtr_18
01-13-2012, 12:58 PM
GetMessageManager()->SetEntityVariable(pEnt, 3000, "pos2d", CL_Vec2f(GetScreenSizeX()/2, GetScreenSizeY()));
GetMessageManager()->SetEntityVariable(pEnt, 3000, "pos2d", CL_Vec2f(GetScreenSizeX()/2,0));

Hmm... this would wait 3 seconds, and then instant teleport the entity to the bottom of the screen, and then top, in the same tick. Probably not what you wanted.

Have you worked through this tutorial (http://www.rtsoft.com/wiki/doku.php?id=proton_entity)? This shows how to move entities around and uses a technique that doesn't require making a new Component - check it out, should give you a good idea of how to do it.

Hi!
I've used the following code:

if((pEnt->GetVar("pos2d")->GetVector2().y < GetScreenSizeY()) && (pEnt->GetVar("pos2d")->GetVector2().y >= 0))
{
//m_bDirForward = true;
GetMessageManager()->SetEntityVariable(pEnt, 3000, "pos2d", CL_Vec2f(GetScreenSizeX()/2, GetScreenSizeY()));
}
else
if((pEnt->GetVar("pos2d")->GetVector2().y > 0) && (pEnt->GetVar("pos2d")->GetVector2().y == GetScreenSizeY()))
{
//m_bDirForward = false;
GetMessageManager()->SetEntityVariable(pEnt, 6000, "pos2d", CL_Vec2f(GetScreenSizeX()/2,0));
}

But it waits for 3 seconds and moved the entity out of the screen. What modifications has to be done to move it to the bottom first and then to top then to bottom continuously?

Seth
01-13-2012, 01:23 PM
Ok, first, I would wire up your OnUpdate in your component so it gets called every frame. Then do your movement in there, and don't bother with MessageManager.

Untested code:


//Some where in your OnAdd you need something like this so it will get called:

GetParent()->GetFunction("OnUpdate")->sig_function.connect(1, boost::bind(&MyComponent::OnUpdate, this, _1));


void MyComponent::OnUpdate(VariantList *pVList)
{
LogMsg("If you don't see this message every frame, something is wrong!");

float speed = 3;
m_pPos2d->y += speed*GetBaseApp()->GetGameDeltaTick(); //fps independendent speed

if (m_pPos2d->y > GetScreenSizeYf())
{
//move it back to the top of the screen
m_pPos2d->y = 0;
}

}

Something like that. Note that this assumes this entity has an OverlayRenderComponent added to its entity as a sibling to this component, otherwise moving the position like that won't matter, as nothing would be rendering.

rtr_18
01-13-2012, 02:00 PM
Ok, first, I would wire up your OnUpdate in your component so it gets called every frame. Then do your movement in there, and don't bother with MessageManager.

Untested code:


//Some where in your OnAdd you need something like this so it will get called:

GetParent()->GetFunction("OnUpdate")->sig_function.connect(1, boost::bind(&MyComponent::OnUpdate, this, _1));


void MyComponent::OnUpdate(VariantList *pVList)
{
LogMsg("If you don't see this message every frame, something is wrong!");

float speed = 3;
m_pPos2d->y += speed*GetBaseApp()->GetGameDeltaTick(); //fps independendent speed

if (m_pPos2d->y > GetScreenSizeYf())
{
//move it back to the top of the screen
m_pPos2d->y = 0;
}

}

Something like that. Note that this assumes this entity has an OverlayRenderComponent added to its entity as a sibling to this component, otherwise moving the position like that won't matter, as nothing would be rendering.


Only the top to bottom movement is working now also. Here's the code modified by me.


float speed = 0.3;

if((m_pPos2d->y >= 0) && (m_pPos2d->y <= GetScreenSizeYf()))
{
m_pPos2d->y += speed*GetBaseApp()->GetGameDeltaTick(); //fps independendent speed
}
if ((m_pPos2d->y >= GetScreenSizeYf()) && (m_pPos2d->y < 0))
{
//move it back to the top of the screen
m_pPos2d->y = 0;//speed*GetBaseApp()->GetGameDeltaTick();
}

Seth
01-13-2012, 10:36 PM
Your code:


if ((m_pPos2d->y >= GetScreenSizeYf()) && (m_pPos2d->y < 0))
{
//move it back to the top of the screen
m_pPos2d->y = 0;//speed*GetBaseApp()->GetGameDeltaTick();
}

So, you want to move your y to 0 if y is > GetScreenSizeYf() *AND* if y is less than 0?

It's impossible for both of things to be true at the same time. Why did you add the m_pPos2d->y < 0 part?

rtr_18
01-14-2012, 06:58 AM
Your code:


if ((m_pPos2d->y >= GetScreenSizeYf()) && (m_pPos2d->y < 0))
{
//move it back to the top of the screen
m_pPos2d->y = 0;//speed*GetBaseApp()->GetGameDeltaTick();
}

So, you want to move your y to 0 if y is > GetScreenSizeYf() *AND* if y is less than 0?

It's impossible for both of things to be true at the same time. Why did you add the m_pPos2d->y < 0 part?

Thanks for pointing it out. Now it's working.But the direction is not from bottom to top. It's restarting from top once it reached the bottom.
I tried this:



m_pPos2d->y -= speed*GetBaseApp()->GetGameDeltaTick();


But my entity is not moved at all after reaching the bottom. What to do?

More over I need two clarifications:



m_pPlayCount = &GetVar("playCount")->GetUINT32();
m_pVarTarget = GetVar("target");


(1)In the above code where do the arguments("playCount" and "target") come from?
I searched the code I could not find. I'm following only RTSimpleApp.

(2) Already existing components like TouchHandlerComponent and OverlayRendererComponent need no header files of them
to be included in the App.cpp file to use it. But my custom component needs the header file to be included to use it. Do I have missed some thing?
Do I need to register my component some where?

yohanip
01-16-2012, 07:35 PM
Hi, maybe i could point out something, i had the learning curve just like you did :) and we should just consult the source code as there would be the aanswer lying,

for

m_pPlayCount = &GetVar("playCount")->GetUINT32();
m_pVarTarget = GetVar("target");

for the case
m_pVarTarget = GetVar("target");
we should consult here : shared/Manager/VariantDB.cpp line 67, if vardb could not find the requested variant, it will create one

and for the case
m_pPlayCount = &GetVar("playCount")->GetUINT32();
we should consult shared/util/Variant.h line , same with above, if there are none then the system will create one for you..

Yes, this dynamics and references would take time to grasp, but as soon as you grasp the concept.. it really is totally rocks your programming world :D

hope that would give some enlightenment about the Variant and the VariantDB concept

and for the bouncing problem you had, maybe you would consider adding a direction parameter?
when the y reach <= 0 then dir = 10
when the y reach >= GetScreenYf then dir =-10
on your OnUpdate routine?

rtr_18
02-01-2012, 07:30 AM
Hi, maybe i could point out something, i had the learning curve just like you did :) and we should just consult the source code as there would be the aanswer lying,

for

m_pPlayCount = &GetVar("playCount")->GetUINT32();
m_pVarTarget = GetVar("target");

for the case
m_pVarTarget = GetVar("target");
we should consult here : shared/Manager/VariantDB.cpp line 67, if vardb could not find the requested variant, it will create one

and for the case
m_pPlayCount = &GetVar("playCount")->GetUINT32();
we should consult shared/util/Variant.h line , same with above, if there are none then the system will create one for you..

Yes, this dynamics and references would take time to grasp, but as soon as you grasp the concept.. it really is totally rocks your programming world :D

hope that would give some enlightenment about the Variant and the VariantDB concept

and for the bouncing problem you had, maybe you would consider adding a direction parameter?
when the y reach <= 0 then dir = 10
when the y reach >= GetScreenYf then dir =-10
on your OnUpdate routine?

Sorry for the late reply. I was on leave for the last two weeks. Can you tell me where to add this direction parameter? Is it like "m_pPos2d"
in the previous post of Mr. Seth or Do I've to add some where else?

yohanip
02-01-2012, 07:49 AM
Sorry for the late reply. I was on leave for the last two weeks. Can you tell me where to add this direction parameter? Is it like "m_pPos2d"
in the previous post of Mr. Seth or Do I've to add some where else?

Since you implements a component to handle your needs, you might want to put that direction parameter on your entity, or on your component
if on your entity, you would use this code

m_dir = &GetParent()->GetVar("my_direction")->getfloat();
if you would like to have that variable only visible at your component level

m_dir = &GetVar("my_direction")->getfloat()

IMO, where you put that variable it's just the matter of your usage and taste

maybe you would like to use GetVarWithDefault instead of GetVar
put that on your "Component::OnAdd" function

now on your "Component::OnUpdate" function, you would put your constraints to the "*m_dir" field

Cheer ;)