Help with a Team component

Get technical support about the C++ source code and about Lua scripts for maps, entities, GUIs, the console, materials, etc. Also covered are the Cafu libraries and APIs, as well as compiling, linking, and the build system.
Post Reply
SoulRider
Posts:95
Joined:2014-04-06, 00:16
Help with a Team component

Post by SoulRider » 2014-08-15, 03:39

As I am very new to C++, I am having troubles creating my Team Component. There are a few syntax errors, but I am a bit confused so I'd thought I'd post my work so far, and look for some explanations on what I am doing wrong, and what I am not understanding.

I am trying to create a CompTeam, but I seem to have made a bit of a mess of it. I started off with CompBasics and began editing that into CompTeam. I would appreciate a few pointers on where I am going wrong :)

Files are attached below.

I am trying to create a variable m_TeamNum and want lua functions to get and set team number.
Attachments
CompTeam.hpp
The Header file
(2.73KiB)Downloaded 409 times
CompTeam.cpp
The cpp file
(5.19KiB)Downloaded 402 times
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Help with a Team component

Post by Carsten » 2014-08-15, 17:10

Hi SoulRider,

I've just had a look at your files. Generally, they look very good. I think what got you caught was the very first two lines in the header file:

Code: Select all

#ifndef CAFU_GAMESYS_COMPONENT_ENTITY_BASICS_HPP_INCLUDED
#define CAFU_GAMESYS_COMPONENT_ENTITY_BASICS_HPP_INCLUDED
The problem is the "BASICS" part, that is, these two lines are the same as in the CompBasics.hpp file.

This error now and then happens to me, too, and often in such cases it is completely unclear from compiler output that this is the root of the problem, because the error messages tend to point entirely elsewhere. If you change these two lines to e.g.

Code: Select all

#ifndef CAFU_GAMESYS_COMPONENT_ENTITY_TEAM_HPP_INCLUDED
#define CAFU_GAMESYS_COMPONENT_ENTITY_TEAM_HPP_INCLUDED
the problem will be solved, and any subsequent compiler error messages start to make sense again. ;-)

I'm sure you can figure out the subsequent problems on your own, but I too looked a bit further: Most errors are trivial, such as a missing trailing T in ComponentTeamT, a single colon where double colon :: is needed, and so on.

I saw that you implemented GetTeamNumber() and SetTeamNumber(). This is of course possible, but with variables that are declared of type TypeSys::VarT<...> and processed by method FillMemberVars(), although it does no harm, it is not necessary, because the component system automatically provides you with Lua get and set methods for such variables:

Code: Select all

-- in Lua code, if Team is your team component:
local tn = Team:get("TeamNum")   -- "TeamNum" is the string given in the C++ constructor.
Team:set("TeamNum", 1 - tn)  -- toggle 0 and 1   ;-)
See the example scripts in Games/DeathMatch/Scripts for plenty of similar examples of the other components, and see the Base component reference documentation for more details about the get() and set() methods.
Of course, using these methods with the string parameters is somewhat inconvenient, and implementing custom methods as you are doing is a good method to bridge the gap until our component system can automatically provide script access such as Team.TeamNum etc. Another reason for custom methods is any functionality that is not covered by the automatically provided methods.

Finally, in the code in the cpp file that mentions ComponentTeamT::EntityTeamT, I didn't know what to do with it, and this is where I stopped for now.

:up:
Best regards,
Carsten
SoulRider
Posts:95
Joined:2014-04-06, 00:16

Re: Help with a Team component

Post by SoulRider » 2014-08-15, 18:27

Thanks a lot for the help so far. I will go through it again, and then will update with any further issues I am having. As I am a play and learn person, I generally ask questions until I reach a better understanding :)

I am really enjoying taking on this project, it is great fun, although a little brain twisting at times :D
SoulRider
Posts:95
Joined:2014-04-06, 00:16

Re: Help with a Team component

Post by SoulRider » 2014-08-15, 20:36

OK, I have read your instructions on get() and set(), and yet I cannot get them working....

When I use this code:

Code: Select all

int ComponentTeamT::GetTeamNumber(lua_State* LuaState)
{
	int tn = ComponentTeamT:get("TeamNum");
	lua_pushinteger(LuaState, tn);
    return 1;
}

int ComponentTeamT::SetTeamNumber(lua_State* LuaState)
{
    
	if (lua_isnumber(LuaState, 4))
	{
		ComponentTeamT:set("TeamNum", LuaState);
		return 1;
	}
}

Code: Select all

I get these errors:
Libs\GameSys\CompTeam.cpp(84) : error C2275: 'cf::GameSys::ComponentTeamT' : ill
egal use of this type as an expression
Libs\GameSys\CompTeam.cpp(84) : error C2275: 'cf::GameSys::ComponentTeamT' : ill
egal use of this type as an expression
Libs\GameSys\CompTeam.cpp(84) : error C2143: syntax error : missing ';' before '
:'
Libs\GameSys\CompTeam.cpp(84) : error C2143: syntax error : missing ';' before '
:'
Libs\GameSys\CompTeam.cpp(94) : error C3861: 'set': identifier not found
scons: *** [Libs\build\win32\vc10\x86\release\GameSys\CompTeam.obj] Error 2
scons: building terminated because of errors.
I have made lots of small changes, and generated lots of different errors, but I cannot work out the correct method of using get() and set(), as copying the method in the documentation doesn't work.

EDIT---------------------------------

Sorry, I just realised get() and set() are lua methods.... So I don't need to implement any functions in this component at all, only the variable?
Last edited by SoulRider on 2014-08-15, 21:06, edited 2 times in total.
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Help with a Team component

Post by Carsten » 2014-08-15, 20:41

SoulRider wrote:Sorry, I just realised get() and set() are lua methods.... So I don't need to implement any functions in this component at all, only the variable?
Correct! :cheesy:
Best regards,
Carsten
SoulRider
Posts:95
Joined:2014-04-06, 00:16

Re: Help with a Team component

Post by SoulRider » 2014-08-15, 21:14

Well, now that is sorted, all seems to be ok. Component compiles correctly, woohoo.

A quick question about scripts. I have noticed for example the door object script. It has no C++ component code, it appears to be a script created entirely in lua from components. Is this correct? It sure makes things easier for me to use that approach :)

Here attached are the Team files again now they are completed (subject to testing).
Attachments
CompTeam.hpp
Header
(2.5KiB)Downloaded 383 times
CompTeam.cpp
cpp
(3.16KiB)Downloaded 393 times
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Help with a Team component

Post by Carsten » 2014-08-16, 15:12

SoulRider wrote:A quick question about scripts. I have noticed for example the door object script. It has no C++ component code, it appears to be a script created entirely in lua from components. Is this correct? It sure makes things easier for me to use that approach :)
Yes, this is correct.

Whenever you need components that define the "behavior" of an entity, using the Script component as seen in the example with the doors is a good way to go -- this is what Script components were made for. The only "downside" of this approach is that immediately related variables, although they can freely be defined and used in the Lua script code, cannot be seen in the Map Editor's grid view (the "Entity Inspector"). You can complement this with extra components such as your Team component at any time, of course, and/or "bypass" this limitation via the ScriptCode property of the Script components (the doors in map Kidney use this approach, too).
Here attached are the Team files again now they are completed (subject to testing).
Looking good! :-)
Best regards,
Carsten
SoulRider
Posts:95
Joined:2014-04-06, 00:16

Re: Help with a Team component

Post by SoulRider » 2014-08-17, 15:23

Quick question, How do I get my new component to show up in CaWE?

I have added it to the HumanPlayer component, but I also want to add it to the editor list of components, so I can make objects team specific within the maps.
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Help with a Team component

Post by Carsten » 2014-08-17, 19:35

SoulRider wrote:Quick question, How do I get my new component to show up in CaWE?
Did you add it to the list at the bottom of the Libs/GameSys/AllComponents.cpp file? (Requires a matching #include at the top as well.)
(The comment in the file explains why this inconvenient list is unfortunately necessary.)

This should be enough for the component to appear in CaWE.
Best regards,
Carsten
SoulRider
Posts:95
Joined:2014-04-06, 00:16

Re: Help with a Team component

Post by SoulRider » 2014-08-17, 21:58

Thanks Carsten. Everything working correctly so far.

It seems that changes to AllComponents.cpp causes a large portion of the engine to be rebuilt :D
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests