Jump to content

#include?


Aily
 Share

Recommended Posts

The idea is simple:

 

I want to make one lua class to control NPC for example.

 

Now i have one NPC, lets call it JOE.gmf

JOE have script JOE.lua, with architecture as all other LUA scripts in Leadwerks.

JOE.lua have many lines of code, and many parameters.

 

Now i add new NPC, let's call it MAX.gmf

MAX have same code, but another parameters. May be MAX can shot.

I copy now JOE.lua to MAX.lua, and ajust parameters.

 

All cool, JOE and MAX walking in world.

 

BUT!!!

I see that them stacking in each other then close to each other.

I need to fix both lua files in same time?

 

I try to do:

copy JOE.lua to "scripts\npc.lua" file

clear JOE.lua code

And put there only require("scripts/npc")

 

And i have error, that classtable is nil. It is normal? Anyone knows how to correct include files?

"Better" is big enemy of "good"

Link to comment
Share on other sites

In your main lua game script, you could find all loaded NPC models in the scene and assign them to a generic NPC script. Then you loop through the list of NPC scripts and call it's Update() methods.

 

Not actual code just the idea

 

main lua file

-- a table to store all loaded NPC's in the scene
npcs = {}

loop through all entities in scene looking for "type" "npc" as a variable or model key
  -- take the model and associate it with your NPC code
  AddToNPCList(CreateNPC(model, "custom params for this model"))
end

-- main game loop
while true
  loop npcs
    -- run npc logic on each npc in the list
     UpdateNPC(npc)
  end
end

 

 

You can use Lua OOP to get fancy with the above where each NPC is an "object" and has it's own functions. The main idea is that you are just associating the model with your own shared code. If you weren't to use Lua this is how you/could do it.

Link to comment
Share on other sites

Guest Red Ocktober

i'm just learning this lua stuff as well... so my advice here is mainly based on experience with BMax and c++...

 

from what i see, you're approach to making objects is a lil off... especially if you want to leverage the existing class logic (code)...

 

if, in the example you laid out above, Joe and Max are objects that are derived from a common class, why not have the common MyCass.lua be "included" in both Max.lua and Joe.lua... that way, if you find a logic error in the base class, the only thing you'd have to change would be in the MyClass.lua logic, and it would be inherited by the decendants...

 

if, on the other hand, you need to change the way Max behaves and add something that Joe doesn't do, then you'd simply change Max's logic...

 

both should still inherit from MyClass.lua, so no changes need to be made there... and the changes in Max or joe, if needed, would be minimal...

 

example:

 

MyClass.lua

basic npc logic that would apply to all npcs

 

Max.lua

require("scripts/MyClass");

do only the things that are Specific to Max

 

Joe.lua

require("scripts/MyClass");

do only the things that are specific to Joe...

 

this way you can leverage the good and tested code by having the descendants "inherit" from the base class logic...

 

also... all that looping around that Rick suggests (don't hit me Rick :rolleyes: ) in the main code tends to get messy after a while... i'd add some sort of callback mechanism in an update function in the MyClass logic, so it'd be handled automatically... this way you could add as many instances of MyClass to the editor, and they'd behave as they should without having any main application code overseeing everything...

 

a lot easier to think out and to maintain... at least the way i see it...

 

everything is self a self contained "entity", which, if designed properly, is capable of living in an scene within the editor, and therefore in any game that includes the scene...

 

this way, your whole game (at least one level) could be run in the editor...

 

this is mainly why i'm even messing with lua...

 

--Mike

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