Jump to content

using lua scripted objects from c++


nelsoncs
 Share

Recommended Posts

hi

 

i am getting some troubles, due to my lack of knowledge.

 

the easy part is:

 

add

#include "lua.h"

in your App.h

 

so, any scripted object will work, but:

if you create a camera in your map (via editor) you have to make some tricks to use that camera and not create another via c++

 

your initila c++ code, creates a context, window etc, you have to write your scripts using those objects...

 

you may write something like this on your Scripts functions:

 

function Script:PostRender()

 

local window = Window:GetCurrent()

local context = Context:GetCurrent()

 

if window:KeyHit(Key.M) then

self.showtext = not(self.showtext)

end

 

if self.showtext

 

context:SetBlendMode(Blend.Alpha)

context:SetColor(255,255, 255)

context:DrawText("hello", x, y)

end

 

end

 

as i get some troubles using a camera defined in c from lua scripts (sure i am doing something wrong) i define the camera inside the editor and then i use that camera from c++

see:

http://www.leadwerks.com/werkspace/topic/10154-set-camera-to-a-camera-in-the-scenelevelmap/

 

Juan

Paren el mundo!, me quiero bajar.

Link to comment
Share on other sites

Juan,

Thanks for the reference to your solution to obtain the camera from the editor/map in C++. this kind of pushed me in the right direction addressing my current problem.

 

I have a design where new entities will be created after the map has been loaded, so I wanted to be able to use the World::ForEachEntity....() functions from within a given, non-Leadwerks::Object class. This turned out to be somewhat difficult to figure out but I think I have a solution.

 

The code below is what I used as an experiment to get the entity class names printed on my console widget.

 

1.

 

SomeClass.h

 

... (prototype)

static void entityClass ( Leadwerks::Entity * entity,

Leadwerks::Object* extra ); // this is the declaration for a callback function, it has to be static

 

2.

SomeClass.cpp

 

... (using the callback function, maybe in the constructor, notice that the 'this' pointer for the classinstance is

cast to a leadwerks object pointer and passed in as a parameter)

 

world->ForEachEntityInAABBDo( aabb,

&entityClass,

(Leadwerks::Object *) this

);

 

3.

SomeClass.cpp

 

...

void SomeClass::entityClass(Leadwerks::Entity * entity,

Leadwerks::Object* extra) {

 

std::string str ( entity->GetClassName() );

 

cout << "Attack::entityClass: " << str << '\n';

((SomeClass *) extra)->getConsole()->addText( &str );

}

 

// Note: "ForEachEntityInAABBDo" seems to require that the

// callback function be static. This creates a really big problem

// if trying to get data back into a C++ class because a static function

// has no idea what class instance you are interested in. So, this solution

// casts the current class instance pointer (this) to a Leadwerks Object

// and then casts back to the cognate Class *. Seems to work but

// the assumption is that there is never a problem with pointer

// Implementations at runtime.

 

 

I was going take the time to confirm that the pointer thunks were ok using Valgrind, but I get so many reported memory leaks from within the engine, thus obsfucating my inspection, that I may wait till something breaks.

 

So, now that I can access basic data for entites created in the Leadwerks editor at anytime, it is on towards making use of their Lua scripts from C++.......

Link to comment
Share on other sites

After your map is loaded you can also loop over the entities in the world.

 

list<Entity*>::iteration iter;
for(iter = world->entities.begin(); iter != world->entities.end(); ++iter)
{
}

 

This saves you from having to mess with passing a this pointer as I you can loop over this in any class. Your idea is good for other things like collisions as you have no other options but to use a callback, but with going over entities loaded from the map you have this more straight forward way.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...