Scripts / Trying to Understand Lua

Author
Message
TazMan
GameGuru TGC Backer
13
Years of Service
User Offline
Joined: 20th Jan 2011
Location: Worldwide Web
Posted: 17th Jan 2016 13:52 Edited at: 17th Jan 2016 13:53
Hello Everyone.

Just a couple of questions.

If I create a variable above all the functions, localize it and attach this script to an object, shouldn't this variable be globaly local to the entity and not any other entity that the script is attached to IE:



At least this is what I thought, If I attach this script to four items I get the last entity number on all of them. If this is not the case how do I make a variable local to the entity but global to the whole script?

Thanks
HP Pavilion Laptop, AMD A8-4555M APU with Radeon(tm) Graphics HD Graphics 1.6GHz, 8GB Memory, 64 Bit Operating System (Windows 10), 1 TB Hard Drive.

I've got something to say - It's better to burn out than fade away.
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 17th Jan 2016 14:06
you will return the last 'e' because testvar is being updated each time the script is loaded (each object running the script)
a local variable just means it will be effected by that script (same name script) and not by others (different name script)... thus any object running said script will be able to change it.

if you want the entity number you can simply prompt e but i assume this isn't the intention so you will need to use an array for multiple objects to store separate values
life\'s one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, AMD R9 200 series , directx 11
TazMan
GameGuru TGC Backer
13
Years of Service
User Offline
Joined: 20th Jan 2011
Location: Worldwide Web
Posted: 17th Jan 2016 17:39
Surely this is wasteful, If you assign the item to an array and the array placement index is the entity number you will get a lot of the array not used. For instance if you have three entities that have been placed on the level and the Enity numbers are 10001, 10002 and 10003, all the array from 10000 back are empty.

Wouldn't it be better to set up an array foobar {} and place the entities in it starting at 1 and then extending it 1 at a time as you put the other 2 in then you would have a smaller array that would be a lot quicker to search. I am not sure about the size of arrays taking up space in an environmeent like this but a smaller array I would think would be better.

Thanks for your input
HP Pavilion Laptop, AMD A8-4555M APU with Radeon(tm) Graphics HD Graphics 1.6GHz, 8GB Memory, 64 Bit Operating System (Windows 10), 1 TB Hard Drive.

I've got something to say - It's better to burn out than fade away.
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 17th Jan 2016 19:42 Edited at: 17th Jan 2016 19:43
for instance you can set values such as:
testvar[1] = 10001
testvar[2] = 10002
testvar[3] = 10003 -- and so on.

The pain is than there is not some command to resize array when needed.
Althought there is another ways, but it does not work for me in GG, maybe not applied yet.



Quote: "
The table library defines two functions to manipulate array sizes: getn, which returns the size of an array, and setn, which sets the size of an array"


Source: http://www.lua.org/pil/19.1.html

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
TazMan
GameGuru TGC Backer
13
Years of Service
User Offline
Joined: 20th Jan 2011
Location: Worldwide Web
Posted: 18th Jan 2016 11:26 Edited at: 18th Jan 2016 11:52
I did find a way to accomplish this 3com with the code below:



It puts all entities with this script into an array starting at 1 and incrementing by 1 as it finds the next.

I also tried table.getn(g_EntityE), table.setn(g_EntityE) and found that this did not work, but #g_EntityE works to get the size of the array and in lua you seem to be able to resize just by adding another element
HP Pavilion Laptop, AMD A8-4555M APU with Radeon(tm) Graphics HD Graphics 1.6GHz, 8GB Memory, 64 Bit Operating System (Windows 10), 1 TB Hard Drive.

I've got something to say - It's better to burn out than fade away.
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 18th Jan 2016 18:49
Glad you get solve it.

Seems like # does no taake in count the null indixes, in another words you can empty some arrays passing null values, and it remove all, but these values remain wasting memory, sound like "ipairs" goes better for remove all the elements from one array.

Another langs have commands for resize array dinamicaly when needed, it looks like LUA does not.
When you remove some element from array, it does not resize automatically.

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
TazMan
GameGuru TGC Backer
13
Years of Service
User Offline
Joined: 20th Jan 2011
Location: Worldwide Web
Posted: 19th Jan 2016 09:37
Well if you use the script below it will give you 3, as it should be because you have a value in the 3rd position.



But if your script looks like this then you would get 1, which would also be expected since you have essentialy got rid of the last two entries



And if you put 'nil' in all the entries then 'ArrayEntries' would show 0. So you could say by this that it is reducing the size of the array when necessary.
HP Pavilion Laptop, AMD A8-4555M APU with Radeon(tm) Graphics HD Graphics 1.6GHz, 8GB Memory, 64 Bit Operating System (Windows 10), 1 TB Hard Drive.

I've got something to say - It's better to burn out than fade away.
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 19th Jan 2016 10:47
So early or later you would need "table.sort" in scene.

Think initially you can build the array pretty nice for yours need, but in test game mode, assuming that array contain entity data, when some entities death/Destroy or you remove from, or something else, the array values moving too, and you could get something like: a = {5, nil, nil, 54, 23, nil, nil, 1, nil, 22}.

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
TazMan
GameGuru TGC Backer
13
Years of Service
User Offline
Joined: 20th Jan 2011
Location: Worldwide Web
Posted: 19th Jan 2016 16:47 Edited at: 19th Jan 2016 16:50
Since I can't find a sort function that will work with GG you could sort out the nils if that is what you want by using a functiion similiar to the one below.



This would then acheive the result of reducing the size of the array when there are nils in between the array entries.
HP Pavilion Laptop, AMD A8-4555M APU with Radeon(tm) Graphics HD Graphics 1.6GHz, 8GB Memory, 64 Bit Operating System (Windows 10), 1 TB Hard Drive.

I've got something to say - It's better to burn out than fade away.
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 19th Jan 2016 17:45
Yeah temp array do the trick.

Learning LUA becomes handy, since Lee are talking about open hardcode to LUA commands, so the models would have many LUA scripts applied.

In another words, you will be able to model a static robot, or a scripted one, yes I know you already can to do many of that things, but then, you will can to do even better.

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
TazMan
GameGuru TGC Backer
13
Years of Service
User Offline
Joined: 20th Jan 2011
Location: Worldwide Web
Posted: 20th Jan 2016 10:34
Oh I am definately enjoying the scripting 3com and am sure I will enjoy it more as time goes on. I decided to start learning it when I wanted my door to do more than the usual. Infact I have taken a couple of weeks off modeling to get to grips with it. I think I will have to get back to that this weekend though..

I always have loved being able to make the computer do things that I wanted it to do though. My kids don't understand that the best way to do that is to programme it.
HP Pavilion Laptop, AMD A8-4555M APU with Radeon(tm) Graphics HD Graphics 1.6GHz, 8GB Memory, 64 Bit Operating System (Windows 10), 1 TB Hard Drive.

I've got something to say - It's better to burn out than fade away.
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 21st Jan 2016 19:51
Quote: "I always have loved being able to make the computer do things that I wanted it to do though"

It's quite nice, even when you get your firts model working in GG.
But now you can get it working plus doing what you want, when you want.

Sooner or later I'll have to script my scifi industrial machines, right now I'm working with PlayInEditor=1, just for testing anim, but most of them will work just when required, via button in console or something like that, (pass?).

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM

Login to post a reply

Server time is: 2024-04-27 19:53:34
Your offset time is: 2024-04-27 19:53:34