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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
unholysavagery
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 73
Joined: Tue Oct 28, 2008 4:27 am

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

Post 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?
This is the internet, men are men, women are men and children are the FBI.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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.
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

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

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

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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().
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

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

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

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
unholysavagery
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 73
Joined: Tue Oct 28, 2008 4:27 am

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

Post 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...
This is the internet, men are men, women are men and children are the FBI.
exor674
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Sat Nov 29, 2008 3:06 am

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

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

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

Post 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 :)
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
Post Reply