Page 1 of 1

I want to make lua more like C++ / C#

Posted: Mon Nov 24, 2008 5:26 am
by unholysavagery
Well I want functions that are in lua's stack to have private variables or in other words when I do something like this:

Code: Select all

function On_Start()
	test = 1
end

function On_KeyEvent(key)
	if test == 1 then
		exit()
	end
end
Now if that value was private then when I press a key it should not exit but the way lua works now it would exit. I also want to be able to define global values by setting them like this:

Code: Select all

test = 1

function On_KeyEvent(key)
	if test == 1 then
		exit()
	end
end
Now if it was working the way I want then my engine should exit because I defined the variable as a global. I'm not sure if toLua++ or LuaBind does something like this but I need it to work like this and I have no idea what to do, anyone know how I can do this?

Re: I want to make lua more like C++ / C#

Posted: Mon Nov 24, 2008 8:06 am
by Falco Girgis
I'm not really sure about making Lua work the way you want it. Sounds like something that Lua was purposely not written to support as it is a scripting language. But maybe toLua++ or LuaBind does allow that, I really don't know.

If you use the local keyword before your variable declaration, it becomes local to whatever block it was created in. I'm pretty sure that declaring something in the global namespace as "local" would make it global.

I'm sure it's not the answer that you want, but at very worst declaring everything as "local" would do what you want it to.

Re: I want to make lua more like C++ / C#

Posted: Mon Nov 24, 2008 10:39 am
by trufun202
One other thought I had is to have your engine create a storage device for Lua. Basically Lua could pass name/value pairs into the engine and have them persisted, such as:

Code: Select all

function On_Start()
   SaveMyShit("test", 1)
end

function On_KeyEvent(key)
   if GetMyShit("test") == 1 then
      exit()
   end
end
I dunno, that might be kinda ghetto, but it should work.

Re: I want to make lua more like C++ / C#

Posted: Mon Nov 24, 2008 10:40 am
by Falco Girgis
Haha, that's how we were originally going to do things before I redesigned our Lua system to have a heartbeat and Yield().

Re: I want to make lua more like C++ / C#

Posted: Mon Nov 24, 2008 10:45 am
by trufun202
GyroVorbis wrote:Haha, that's how we were originally going to do things before I redesigned our Lua system to have a heartbeat and Yield().
Yeah good point, check out Falco's "PMS" solution. :P If you persist the Lua State, then those global variables will remain in memory as needed.

Re: I want to make lua more like C++ / C#

Posted: Mon Nov 24, 2008 3:02 pm
by unholysavagery
GyroVorbis wrote:I'm not really sure about making Lua work the way you want it. Sounds like something that Lua was purposely not written to support as it is a scripting language. But maybe toLua++ or LuaBind does allow that, I really don't know.

If you use the local keyword before your variable declaration, it becomes local to whatever block it was created in. I'm pretty sure that declaring something in the global namespace as "local" would make it global.

I'm sure it's not the answer that you want, but at very worst declaring everything as "local" would do what you want it to.
Yeah declaring everything as local does work, though it's irritating but it gets the job done. They should do the opposite of this, everything should be local then you can define it as global, that just makes sense to me...

Re: I want to make lua more like C++ / C#

Posted: Wed Dec 03, 2008 5:01 pm
by exor674
You can do stuff like this if you wanna manage the thing totally in lua (There is a way to set this up from C too, but I'm not aware of how as my lua experience comes mainly from WoW modding).

Code: Select all

foo = {}
function foo:setSomething(what)
  self.something = what
end
function foo:getSomething()
  return self.something
end

> foo:setSomething(5)
> print(foo:getSomething())
5
> print(self)
nil
also http://lua-users.org/wiki/InheritanceTutorial

EDIT: Bah, I think I misunderstood your question. I though you were asking how to set up classes in Lua.

Re: I want to make lua more like C++ / C#

Posted: Wed Dec 03, 2008 7:25 pm
by Falco Girgis
That's also not a true class. That's like making a particular instance of a class. If you are using pure Lua to do OO, you have to make each object or "table" have another table that represents its metatable.

Re: I want to make lua more like C++ / C#

Posted: Wed Dec 03, 2008 7:35 pm
by M_D_K
I was thinking about something similar when I wanted to do a C style include in Lua. I thought that if I made a function in the engine called include that you passed the filename. it would create a metatable to store all globals and functions within that included script and give access to any script that included it.(wow how many times did I say include :) )

Thats when I wanted Lua to be more like C/C++, hell I'd settle for UnrealScript :)