Login  Register
 



Post new topicReply to topic
 
Author Message
 PostPosted: 21 Apr, 2012 
 

Joined: 31 Mar, 2011
Posts: 128
Location: Hanoi, Vietnam
Offline
Hi all, I see in many lua file that have defined function like OnCreate, OnGotTarget, OnWeaponFired,... then I tried to make a new function named TestFunction in unit's lua file:
Code:
TestFunction = function(self)
       LOG("This function can run, yeah!")
end,

Ehh, the problem is that moho log didn't print "This function can run, yeah!" string so I think my TestFunction isn't run by game engine. Why original game's function is run but my function? Is it defined by game engine? Is there anyway to execute my TestFunction?(I don't want to use ForkThread or self:TestFunction() script). Thank you


Last edited by phong1892 on 21 Apr, 2012, edited 1 time in total.

Top
 Profile  
 PostPosted: 21 Apr, 2012 
 

Joined: 26 Feb, 2009
Posts: 2996
Offline
yeah sure,

when you define a new function in the units script you have to call the function
by using

self:TestFunction()

and the msg will be put in your LOG

remember though if the function is in a units script.. ONLY that unit can call that function because its local to that unit, if you put it in a defaultunits.lua class the function will only be available to that unit type class, if you put it in unit.lua it will be available to all units :)

_________________
Domino.
______________


Last edited by Domino on 21 Apr, 2012, edited 1 time in total.

Top
 Profile  
 PostPosted: 21 Apr, 2012 
 

Joined: 31 Mar, 2011
Posts: 128
Location: Hanoi, Vietnam
Offline
Very detail :). I see, thank you


Top
 Profile  
 PostPosted: 21 Apr, 2012 
 

Joined: 31 Mar, 2011
Posts: 128
Location: Hanoi, Vietnam
Offline
Uhm, I'm very confused. I don't know which script I should use: pairs or ipairs or nothing in order to loop into a table. For example: I can use:

Code:
for k, v in table do


also:

Code:
for k, v in pairs(table) do


or:

Code:
for k, v in ipairs(table) do


I've read lua manual but I don't understand it. Could you explain to me? Which script do you guys usually use?
Thank in advance


Top
 Profile  
 PostPosted: 21 Apr, 2012 
 

Joined: 05 Oct, 2007
Posts: 16426
Location: camping near the biggest power-up
Offline
Use ipairs when your tables use numbers, use regular pairs when they don't. This is important because one will only read numbers and the other doesn't read numbers at all.

In SupCom's Lua, if you don't use one of the above the engine will assume you meant one and put it in for you. I can't remember which though.

_________________
Nephylim wrote:
But, an FA army in an FA environment just looks... right.
help wrote:
Does anyone know how to use air transports? I cant get them to pick up troops.


Top
 Profile  
 PostPosted: 21 Apr, 2012 
 

Joined: 26 Feb, 2009
Posts: 2996
Offline
which ever one you use,

the one like this is universal

Code:
for i, v in table do

LOG('i = ' .. repr(i))
LOG('v = ' .. repr(v))

end


i will always key or index and v will always contain the value(s)

if the value is a table then inside the loop a value can be returned with

v.key

.. from the above code you can prolly isotane what you need to do to get the values you require..

however there is also another usefull function for finding a value in a table.

table.find(table, value)

say we have a table like so..

table = { 'i', 'want', 'to', 'find', 'a', 'value' }

this is a numerically numbered table.. (even though we cant see the numbers)

we can use

Code:
if table.find(table, 'want') then

end


which will return the value (true) or nil (false)

id just go with the first one... :)

_________________
Domino.
______________


Top
 Profile  
 PostPosted: 22 Apr, 2012 
 
User avatar

Joined: 15 Feb, 2007
Posts: 20036
Location: Presumably, at the time of posting, his computer.
Offline
BulletMagnet wrote:
In SupCom's Lua, if you don't use one of the above the engine will assume you meant one and put it in for you. I can't remember which though.


Regular pairs.

Specifically, in Supreme Commander then

Code:
for k, v in table do


is exactly equivalent to

Code:
for k, v in pairs(table) do


ipairs has very little use, unless you're definitely treating a table like an array, which is rare in Lua.

_________________
I'm watchin you!
Image


Top
 Profile  
 PostPosted: 22 Apr, 2012 
 

Joined: 31 Mar, 2011
Posts: 128
Location: Hanoi, Vietnam
Offline
Hi all,
After a hard working day, I'm very tired but fell happy when read your reply. Nice work, mate. Thank you


Top
 Profile  
 PostPosted: 22 Apr, 2012 
 

Joined: 31 Mar, 2011
Posts: 128
Location: Hanoi, Vietnam
Offline
One more question: I read the lua tutorial but didn't find any script that remove or delete some letters in a string. For example, I want to remove "Left" word in "Bone_Left" string to get only "Bone" word.


Top
 Profile  
 PostPosted: 23 Apr, 2012 
 

Joined: 26 Feb, 2009
Posts: 2996
Offline
Look for

string.sub( )
string.gsub( )

you can use these 2 to parse strings :)

_________________
Domino.
______________


Top
 Profile  
 PostPosted: 23 Apr, 2012 
 

Joined: 31 Mar, 2011
Posts: 128
Location: Hanoi, Vietnam
Offline
Domino wrote:
Look for

string.sub( )
string.gsub( )

you can use these 2 to parse strings :)


I already used string.sub(), it's ok.

I tried string.gsub() but got no expectation return.
For example, I want to remove the last 5 letters:
Code:
s = "xxxyyyy_unit" #(x and y are whatever)
s1 = string.gsub(s, "_unit", "") #I want to remove "_unit" sub string
print(s1) #just print it


I got s1 string same as s string.


Top
 Profile  
 PostPosted: 23 Apr, 2012 
 

Joined: 26 Feb, 2009
Posts: 2996
Offline
if s = 'xxx_unit then
s1 = string.gsub(s, "_unit", "", 1)

will remove everything after that, from the first instance and return xxx.

if s = 'xxx_unit_unit then

s1 = string.gsub(s, "_unit", "", 2)

would return xxx_unit

we can also use string.sub in this way

if s = 'xxx_unit then

s1= string.sub(s, 1, -5)

which would also return xxx

but also

s1= string.sub(s, 4, -1)

would leave _unit


but also, i use this..

local position = string.find(s, "_")
local unitid = string.sub(s, 1, position-1)
local leftover = string.sub(s, position+1, -1)

which gives

unitid = xxx
leftover = _unit

:)

_________________
Domino.
______________


Top
 Profile  
 PostPosted: 23 Apr, 2012 
 

Joined: 31 Mar, 2011
Posts: 128
Location: Hanoi, Vietnam
Offline
Domino wrote:
if s = 'xxx_unit then
s1 = string.gsub(s, "_unit", "", 1)

will remove everything after that, from the first instance and return xxx.

if s = 'xxx_unit_unit then

s1 = string.gsub(s, "_unit", "", 2)

would return xxx_unit

we can also use string.sub in this way

if s = 'xxx_unit then

s1= string.sub(s, 1, -5)

which would also return xxx

but also

s1= string.sub(s, 4, -1)

would leave _unit


but also, i use this..

local position = string.find(s, "_")
local unitid = string.sub(s, 1, position-1)
local leftover = string.sub(s, position+1, -1)

which gives

unitid = xxx
leftover = _unit

:)


Nice work, mate. It could be much better now, haha.
Do you know which version of lua that game engine used? I usually test my script in lua 5.1.4. Is it OK?


Top
 Profile  
 PostPosted: 23 Apr, 2012 
 
User avatar

Joined: 15 Feb, 2007
Posts: 20036
Location: Presumably, at the time of posting, his computer.
Offline
They use a customized 5.0.2, or something like that. But all Lua 5 is pretty close. 5.2 did some funky stuff with envs but that's it.

_________________
I'm watchin you!
Image


Top
 Profile  
 PostPosted: 23 Apr, 2012 
 

Joined: 31 Mar, 2011
Posts: 128
Location: Hanoi, Vietnam
Offline
DeadMG wrote:
They use a customized 5.0.2, or something like that. But all Lua 5 is pretty close. 5.2 did some funky stuff with envs but that's it.

I see, thank you


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic



Quick Tools

Search for:
Jump to:  

© 2002-2010 Gas Powered Games Corp. All Rights Reserved. Gas Powered Games is the exclusive trademark of Gas Powered Games Corp.
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
 
Home| Games | Company | News & Press | Support
  Terms of Use   |    Copyright Information   |    Privacy Policy