Jump to content

calling methods by name


Rick
 Share

Recommended Posts

Here is something that might be of interest and should allow for some more dynamic behavior controlled from the editor. You can call functions by just knowing the string name of it.

 

function HelloWorld(name)
print("Hello "..name)
end

_G["HelloWorld"]("Rick")

 

With this you can have a model class that has 2 properties. One for a lua file and one for a function name. In the Spawn() you can run a dofile() on the lua file to load it, and inside the update you can read the function name and call it from the string name. This would allow 1 model class to have different behaviors based on what you setup via the editor.

 

So let's say I'm making an rpg game. I have a rat model that I really like and would like to use a few times. Let's say I want a "Small Rat" that has basic attacks. Then later in my game I want to use the same rat model but want it to have more functionality and call it "Big Rat". With this I can have 2 separate lua files that control the behavior of the rat and in the editor assign what lua file gets played with each instance. Very cool.

 

This will also be useful for volume triggers because you can use the same trigger model but have it run different things, because the trigger model is generic and what happens in each trigger would be unique.

Link to comment
Share on other sites

You can even write a little script in the property editor and make the model execute that code. There's an option in the text property to open a multiline text editor in a separate window. I didn't use this feature, but it is an example of just how flexible Lua is.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Maybe enter this text in the property editor:

function CustomBehavior(entity)
Notify("Hello!")
end

 

Then in SetKey add this:

function SetKey(key,value)
if key=="customscript" then
	dostring(value)
end
end

 

So now at this point the new code has been evaluated and the function is usable in Lua.

 

Now add this to the message receive function:

function ReceiveMessage(message,extra)
if message=="custombehavior" then
	CustomBehavior(entity)
end
end

 

I don't know if this is a great idea, because it is confusing, and the state is shared across all instances of the model, but it's interesting that it works.

My job is to make tools you love, with the features you want, and performance you can't live without.

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...