Jump to content

[c++] Set lua script from string


Skrakle
 Share

Recommended Posts

I'm looking for a way to set the entity script by sending a string rather than loading a lua file, something like:

 

entity::SetScriptWithString("function Script:Start()\nself.index=-1\nend");

 

I'm building a rpg and have lots of characters which will require customized functions in their script and will be generated in-game depending how the character is configured.

 

Is there some way to do that?

Link to comment
Share on other sites

You can try to use loadstring.

But it is very expensive since your code will basically be compiled at runtime and error handling will be a lot harder. So only do this if you really have to.

 

Additionally I am not sure if you really can attach a Start method to an entity with this.

 

f = loadstring("self.index = self.index - 1")

f()

Link to comment
Share on other sites

So remember that functions are just variables. So define these variables in a common script and then for each instance you can assign your custom function to that instances variable.

 

Common Script

 

function Script:Start()
   self.myFunc = nil
end
function Script:UpdateWorld()
  -- call myFunc
  self.myFunc()
end

-- define somwhere else custom functionality to myFunc
entity.script.myFunc = function() -- do whatever
end

entity1.script.myFunc = function() --do something different here
end

entity2.script.myFunc = function() --do something completely different here
end

Link to comment
Share on other sites

You can try to use loadstring.

But it is very expensive since your code will basically be compiled at runtime and error handling will be a lot harder. So only do this if you really have to.

 

Additionally I am not sure if you really can attach a Start method to an entity with this.

 

f = loadstring("self.index = self.index - 1")

f()

Does this mean that once a script has been set to an entity, we can't change it for another at runtime?

 

 

 

Common Script

 

function Script:Start()
self.myFunc = nil
end
function Script:UpdateWorld()
-- call myFunc
self.myFunc()
end

-- define somwhere else custom functionality to myFunc
entity.script.myFunc = function() -- do whatever
end

entity1.script.myFunc = function() --do something different here
end

entity2.script.myFunc = function() --do something completely different here
end

 

That would solve my issue, although i would need to set those functions from c++ which is why a SetScriptFromString method would be simpler in my case. How can i add/modify lua variables/functions from c++?

Link to comment
Share on other sites

i don´t understand.

 

Why would you have to set these from c++?

the example is for lua only.

 

Also you can always change a script that is attached to an entity.

 

There is even a undocumented function.:

entity:SetScript("newEntityScript.lua", true)

 

(first parameter is the path to the lua file, the second is if the start function should be called)

Link to comment
Share on other sites

now you need to tell me how you want to compile dynamic c++ code for your entities.

 

I really think what you plan to do is possible in lua. But i just don´t know what your requirements etc. are. There where already some good examples like the class-like system in lua, the loadstring if you really want to generate dynamic functions etc.

Link to comment
Share on other sites

now you need to tell me how you want to compile dynamic c++ code for your entities.

 

I really think what you plan to do is possible in lua. But i just don´t know what your requirements etc. are. There where already some good examples like the class-like system in lua, the loadstring if you really want to generate dynamic functions etc.

What i meant by doing everything in c++ is doing the game entirely in c++. Here's an example of what i'm trying to do.

 

Let's say i have 100 characters in my map, one of them is controllable by the player. The remaining 99 doesn't need the code blocks where it scans for keys to move/jump around and the character that the player controls doesn't need code blocks for AI. During the game, if the player ends up controlling another character, it is when i would generate and set a new script (by string) with new generated code. That being said, i don't want to end up with 100's of lua files.

 

I have other alternatives, i just wondered if this was possible.

Link to comment
Share on other sites

I think we need to look at what makes a character.

 

- 3D Model

- Attributes

- Functionality

 

Atributes and Functionality doesn't need to be part of the LE Script directly. They can be variables to a Lua class inside them. What this means is that you can create 1 script called Actor and have variables that control functionality.

 

Functions are variables in Lua. That means you can assign functions to variables. So let's say you have 2 standalone functions:

 

function AI_Think(actor)

-- AI logic

end

 

function Player_Think(actor)

-- user input

end

 

Now in your 1 Actor LE script you have a variable named something like self.update where for the player you set it to Player_Think and call self.update() inside UpdateWorld.

 

 

function Script:Start()
  if player then
     self.update = Player_Think
  else
     self.update = AI_Think
  end
end

function Script:UpdateWorld()
  -- this will call the correct function based on what this actor is
  self.update(self)
end

 

Passing functions around to variables is basically like your lua code injection. This is why Lua is so powerful. Functions as variables opens the door to some interesting things

  • Upvote 1
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...