Moved triggering to lua, got a strange bug with the const char* holding the name of the lua script.

This commit is contained in:
Steffen Fündgens 2015-01-13 13:52:22 +01:00
parent 60d76a58de
commit fe58023458
8 changed files with 114 additions and 55 deletions

View File

@ -20,7 +20,10 @@
<distance>3.0</distance> <distance>3.0</distance>
<isBiggerThan>false</isBiggerThan> <isBiggerThan>false</isBiggerThan>
<objectNum>0</objectNum> <objectNum>0</objectNum>
<functionPointer>0</functionPointer> <luaScript>deleteRandomObject</luaScript>
<toChangeIdGreen>1</toChangeIdGreen>
<toChangeIdBlue>5</toChangeIdBlue>
<toChangeObjNum>0</toChangeObjNum>
</trigger> </trigger>
<trigger> <trigger>
<name>deleteRandomObjectAtTorch</name> <name>deleteRandomObjectAtTorch</name>
@ -32,7 +35,10 @@
<distance>2.0</distance> <distance>2.0</distance>
<isBiggerThan>false</isBiggerThan> <isBiggerThan>false</isBiggerThan>
<objectNum>0</objectNum> <objectNum>0</objectNum>
<functionPointer>0</functionPointer> <luaScript>deleteRandomObject</luaScript>
<toChangeIdGreen>1</toChangeIdGreen>
<toChangeIdBlue>5</toChangeIdBlue>
<toChangeObjNum>0</toChangeObjNum>
</trigger> </trigger>
</composition> </composition>
@ -58,7 +64,10 @@
<distance>1.0</distance> <distance>1.0</distance>
<isBiggerThan>false</isBiggerThan> <isBiggerThan>false</isBiggerThan>
<objectNum>0</objectNum> <objectNum>0</objectNum>
<functionPointer>-</functionPointer> <luaScript>-</luaScript>
<toChangeIdGreen>-</toChangeIdGreen>
<toChangeIdBlue>-</toChangeIdBlue>
<toChangeObjNum>-</toChangeObjNum>
</trigger> </trigger>
</composition> </composition>
@ -84,7 +93,10 @@
<distance>1.0</distance> <distance>1.0</distance>
<isBiggerThan>false</isBiggerThan> <isBiggerThan>false</isBiggerThan>
<objectNum>0</objectNum> <objectNum>0</objectNum>
<functionPointer>-</functionPointer> <luaScript>-</luaScript>
<toChangeIdGreen>-</toChangeIdGreen>
<toChangeIdBlue>-</toChangeIdBlue>
<toChangeObjNum>-</toChangeObjNum>
</trigger> </trigger>
</composition> </composition>
@ -110,7 +122,10 @@
<distance>1.0</distance> <distance>1.0</distance>
<isBiggerThan>false</isBiggerThan> <isBiggerThan>false</isBiggerThan>
<objectNum>0</objectNum> <objectNum>0</objectNum>
<functionPointer>-</functionPointer> <luaScript>-</luaScript>
<toChangeIdGreen>-</toChangeIdGreen>
<toChangeIdBlue>-</toChangeIdBlue>
<toChangeObjNum>-</toChangeObjNum>
</trigger> </trigger>
</composition> </composition>
@ -136,7 +151,10 @@
<distance>1.0</distance> <distance>1.0</distance>
<isBiggerThan>false</isBiggerThan> <isBiggerThan>false</isBiggerThan>
<objectNum>0</objectNum> <objectNum>0</objectNum>
<functionPointer>-</functionPointer> <luaScript>-</luaScript>
<toChangeIdGreen>-</toChangeIdGreen>
<toChangeIdBlue>-</toChangeIdBlue>
<toChangeObjNum>-</toChangeObjNum>
</trigger> </trigger>
</composition> </composition>

View File

@ -1,10 +0,0 @@
function trigger()
print("Hello from Lua!")
if(not Level) then
print("No Level found!")
return
end
--a = table.getn(Level.getObjects())
--rand = math.random(0, table.getn(Level->getObjects()) - 1)
--Level->getObjects()->erase(level->getObjects()->begin() + rand)
end

View File

@ -0,0 +1,11 @@
function trigger(objectToChange)
print("Hello from Lua!")
--if(not this_level) then
print("No level found!")
return
--end
a = this_level:getObjectCount()
rand = math.random(0, a - 1)
this_level:deleteObject(rand)
print("Triggered from Lua!")
end

View File

@ -179,7 +179,10 @@ std::vector<int> Converter::newComposition(int type, float posX, float posZ){
XMLElement* distance = doc->NewElement("distance"); XMLElement* distance = doc->NewElement("distance");
XMLElement* isBiggerThan = doc->NewElement("isBiggerThan"); XMLElement* isBiggerThan = doc->NewElement("isBiggerThan");
XMLElement* objectNum = doc->NewElement("objectNum"); XMLElement* objectNum = doc->NewElement("objectNum");
XMLElement* functionPointer = doc->NewElement("functionPointer"); XMLElement* luaScript = doc->NewElement("luaScript");
XMLElement* toChangeIdGreen = doc->NewElement("toChangeIdGreen");
XMLElement* toChangeIdBlue = doc->NewElement("toChangeIdBlue");
XMLElement* toChangeObjNum = doc->NewElement("toChangeObjNum");
name->SetText("-"); name->SetText("-");
xPosition->SetText("0"); xPosition->SetText("0");
@ -190,7 +193,10 @@ std::vector<int> Converter::newComposition(int type, float posX, float posZ){
distance->SetText("1.0"); distance->SetText("1.0");
isBiggerThan->SetText("false"); isBiggerThan->SetText("false");
objectNum->SetText("0"); objectNum->SetText("0");
functionPointer->SetText("-"); luaScript->SetText("-");
toChangeIdGreen->SetText("-");
toChangeIdBlue->SetText("-");
toChangeObjNum->SetText("-");
trigger->InsertEndChild(name); trigger->InsertEndChild(name);
trigger->InsertEndChild(xPosition); trigger->InsertEndChild(xPosition);
@ -201,7 +207,10 @@ std::vector<int> Converter::newComposition(int type, float posX, float posZ){
trigger->InsertEndChild(distance); trigger->InsertEndChild(distance);
trigger->InsertEndChild(isBiggerThan); trigger->InsertEndChild(isBiggerThan);
trigger->InsertEndChild(objectNum); trigger->InsertEndChild(objectNum);
trigger->InsertEndChild(functionPointer); trigger->InsertEndChild(luaScript);
trigger->InsertEndChild(toChangeIdGreen);
trigger->InsertEndChild(toChangeIdBlue);
trigger->InsertEndChild(toChangeObjNum);
std::vector<int> ret = nextID; std::vector<int> ret = nextID;
nextID[1] += 1; nextID[1] += 1;

View File

@ -60,11 +60,12 @@ void Level::load() {
//Expose the class Level and its functions //Expose the class Level and its functions
luabridge::getGlobalNamespace(L) luabridge::getGlobalNamespace(L)
.beginClass<Level>("Level") .beginClass<Level>("Level")
.addFunction("getObjects", &Level::getObjects) .addFunction("deleteObject", &Level::deleteObject)
.addFunction("getObjectCount", &Level::getObjectCount)
.endClass(); .endClass();
//Push the level as a global variable //Push the level as a global variable
luabridge::push(L, this); luabridge::push(L, this);
lua_setglobal(L, "Level"); lua_setglobal(L, "this_level");
this->physics = Physics(); this->physics = Physics();
@ -316,7 +317,7 @@ void Level::load() {
if (name.compare("-") != 0){ if (name.compare("-") != 0){
float xPos, yPos, zPos, distance; float xPos, yPos, zPos, distance;
bool isBigger; bool isBigger;
int idGreen, idBlue, objectNum, functionPointer_int; int idGreen, idBlue, objectNum;
errorCheck(xmlTrigger->FirstChildElement("xPosition")->QueryFloatText(&xPos)); errorCheck(xmlTrigger->FirstChildElement("xPosition")->QueryFloatText(&xPos));
errorCheck(xmlTrigger->FirstChildElement("yPosition")->QueryFloatText(&yPos)); errorCheck(xmlTrigger->FirstChildElement("yPosition")->QueryFloatText(&yPos));
@ -358,13 +359,32 @@ void Level::load() {
object = objects[objectIdentifiers[i][0]]; object = objects[objectIdentifiers[i][0]];
} }
} }
errorCheck(xmlTrigger->FirstChildElement("functionPointer")->QueryIntText(&functionPointer_int)); const char* charLuaScript = xmlTrigger->FirstChildElement("luaScript")->GetText();
if(charLuaScript == NULL){
printf("XMLError: No Lua script found for a trigger.\n");
}
std::string luaScript = charLuaScript;
int toChangeIdGreen, toChangeIdBlue, toChangeObjNum, objectToChange=0;
errorCheck(xmlTrigger->FirstChildElement("toChangeIdGreen")->QueryIntText(&toChangeIdGreen));
errorCheck(xmlTrigger->FirstChildElement("toChangeIdBlue")->QueryIntText(&toChangeIdBlue));
errorCheck(xmlTrigger->FirstChildElement("toChangeObjNum")->QueryIntText(&toChangeObjNum));
for (unsigned int i = 0; i<objectIdentifiers.size(); i++){
if (objectIdentifiers[i][1]==toChangeIdGreen && objectIdentifiers[i][2]==toChangeIdBlue && objectIdentifiers[i][3]==toChangeObjNum){
objectToChange = objectIdentifiers[i][0];
}
}
if (object != 0) { if (object != 0) {
Trigger trigger = Trigger(position, distance, isBigger, object, functionPointer_int, L, this); if (objectToChange != 0) {
Trigger trigger = Trigger(position, distance, isBigger, object, luaScript, L, objectToChange, this);
triggers.push_back(trigger); triggers.push_back(trigger);
} }
else { else {
printf("Trigger object not found.\n"); printf("Object to be changed by a trigger not found.\n");
}
}
else {
printf("Triggering object not found.\n");
} }
} }
} }
@ -461,3 +481,19 @@ void Level::setSkydomeSize(float size) {
std::vector<Object*>* Level::getObjects() { std::vector<Object*>* Level::getObjects() {
return &objects; return &objects;
} }
void Level::deleteObject(int objectIndex){
objects.erase(objects.begin() + objectIndex);
printf("Deleting an object.\n");
}
int Level::getObjectCount(){
return objects.size();
}

View File

@ -30,6 +30,8 @@ class Level {
glm::vec4 getFogColour(); glm::vec4 getFogColour();
void setSkydomeSize(float size); void setSkydomeSize(float size);
std::vector<Object*>* getObjects(); std::vector<Object*>* getObjects();
void deleteObject(int objectIndex);
int getObjectCount();
private: private:
void errorCheck(tinyxml2::XMLError error); void errorCheck(tinyxml2::XMLError error);
std::string levelNum; std::string levelNum;

View File

@ -1,36 +1,22 @@
#include "trigger.hh" #include "trigger.hh"
#include "level.hh" #include "level.hh"
Trigger::Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, int functionPointer_int, lua_State* L, Level* level) { Trigger::Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* L, int objectToChange, Level* level) {
this->position=position; this->position=position;
this->distance=distance; this->distance=distance;
this->isBigger=isBigger; this->isBigger=isBigger;
this->object=object; this->object=object;
void (*functionPointer)(Level*) = NULL; this->triggerLuaScript=("../Levels/scripts/" + luaScript + ".lua").c_str();
switch(functionPointer_int) { printf("Hallo\n");
case 0: printf(this->triggerLuaScript);
this->functionPointer = &Trigger::trigger_function_0; printf("Hallo\n");
break; printf("\n");
case 1:
this->functionPointer = &Trigger::trigger_function_1;
break;
case 2:
this->functionPointer = &Trigger::trigger_function_2;
break;
case 3:
this->functionPointer = &Trigger::trigger_function_3;
break;
case 4:
this->functionPointer = &Trigger::trigger_function_4;
break;
default:
printf("Trigger function could not be found.\n");
}
this->level = level; this->level = level;
this->L = L; this->L = L;
if(L == nullptr){ if(L == nullptr){
printf("L is NULL in trigger!\n"); printf("L is NULL in trigger!\n");
} }
this->objectToChange = objectToChange;
triggered = false; triggered = false;
} }
@ -43,15 +29,21 @@ Trigger::~Trigger(){
void Trigger::triggerUpdate(){ void Trigger::triggerUpdate(){
if (!triggered){ if (!triggered){
if (isBigger && (glm::distance(object->getPosition(), position) > distance)) { if (isBigger && (glm::distance(object->getPosition(), position) > distance)) {
(this->*functionPointer)(level); printf(triggerLuaScript);
luaL_dofile(L, "../Levels/scripts/Level1/deleteRandomObject.lua"); printf("\n");
luabridge::getGlobal(L, "trigger")(); printf(triggerLuaScript);
printf("\nTriggering\n");
//luaL_dofile(L, luaScript);
//luabridge::getGlobal(L, "trigger")(objectToChange);
triggered = true; triggered = true;
} }
else if (!isBigger && (glm::distance(object->getPosition(), position) < distance)) { else if (!isBigger && (glm::distance(object->getPosition(), position) < distance)) {
(this->*functionPointer)(level); printf(triggerLuaScript);
luaL_dofile(L, "../Levels/scripts/Level1/deleteRandomObject.lua"); printf("\n");
luabridge::getGlobal(L, "trigger")(); printf(triggerLuaScript);
printf("\nTriggering\n");
//luaL_dofile(L, luaScript);
//luabridge::getGlobal(L, "trigger")(objectToChange);
triggered = true; triggered = true;
} }
} }

View File

@ -14,7 +14,7 @@ class Level;
class Trigger { class Trigger {
public: public:
Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, int functionPointer_int, lua_State* L, Level* level); Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* L, int objectToChange, Level* level);
Trigger(); Trigger();
~Trigger(); ~Trigger();
void triggerUpdate(); void triggerUpdate();
@ -23,10 +23,11 @@ class Trigger {
float distance; float distance;
bool isBigger; bool isBigger;
Object* object; Object* object;
void (Trigger::*functionPointer)(Level*); const char* triggerLuaScript;
Level* level; Level* level;
bool triggered; bool triggered;
lua_State* L; lua_State* L;
int objectToChange;
void trigger_function_0(Level* level); void trigger_function_0(Level* level);
void trigger_function_1(Level* level); void trigger_function_1(Level* level);
void trigger_function_2(Level* level); void trigger_function_2(Level* level);