Data Realms Fan Forums http://45.55.195.193/ |
|
Running Lua On Attachables http://45.55.195.193/viewtopic.php?f=73&t=17328 |
Page 1 of 1 |
Author: | Kyred [ Tue Dec 08, 2009 9:50 pm ] |
Post subject: | Running Lua On Attachables |
Well, I think I just came across something big. I was messing around with the global table HeldDevices when I came across an interesting discovery. Although Lua itself doesn't run on attachables, you can still call the Update(self) method with another script, causing the script to execute like normal! However, you have to keep executing Update(self) over and over again to get this to keep working. The best way to manage this is by running a second script which will execute the attachable's Update(self) every time the second script updates. Also, you need to have the attachable's Update(self) function saved to a variable. This is the tricky part. To do this, you'll need to find where the Update(self) function of the preset, for the attachable, is within it's respective global table (ie. I had to find the function in HeldDevices). Here's how you do it: First off, in the .ini of your scripted attachable, add code for a dummy MOPixel immediately after you define the attachable, like this: Code: AddEffect = MOPixel PresetName = Fake Script Pixel Mass = 0.001 LifeTime = 650 GlobalAccScalar = 0 ScriptPath = YouFolder.rte/CHANGE_THIS.lua Sharpness = 3 HitsMOs = 0 GetsHitByMOs = 0 Color = Color R = 255 G = 255 B = 255 Atom = Atom Material = Material CopyOf = Bullet Metal TrailColor = Color R = 255 G = 255 B = 255 TrailLength = 30 Then, in CHANGE_THIS.lua, you'll want to add this block of code: Code: for i,v in pairs(HeldDevices) do SCRIPT_FUNCTIONS = v; end Why did you have to do this? First off, when this loop terminates, the last value of "v" in the table will be saved to the variable SCRIPT_FUNCTIONS. This value just so happens to be the last scripted HeldDevice to be loaded by CC. Since we place this dummy MOPixel immediately after our scripted HeldDevice, and therefore was the last to be added to the HeldDevices list, v will correspond to our HeldDevice. The table HeldDevices stores a second table which are the functions Destory, Update, and Create. The tables are indexed like this: HeldDevices[PresetID] = Table2. Table2["Create"] = Create(self), Table2["Update"] = Update(self), and Table2["Destory"] = Destory(self). We save Table2 to the global variable SCRIPT_FUNCTIONS so that we can use this later. Now, in order to run the Update(self) function of the HeldDevice, we simply do SCRIPT_FUNCTIONS["Update"](PointerToOurWeapon); This works, even if the weapon is being held in hand! I'll try to make a Proof-of-Concept for doing this later when I have time. |
Author: | Abdul Alhazred [ Wed Dec 09, 2009 9:37 am ] |
Post subject: | Re: Running Lua On Attachables |
An amazing find, but I don't really understand how you do it. As far as I know there is no global table called "HeldDevices". Perhaps you could explain a bit more? |
Author: | Kyred [ Thu Dec 10, 2009 6:52 pm ] |
Post subject: | Re: Running Lua On Attachables |
Read this thread. These tables were found by mail. http://forums.datarealms.com/viewtopic.php?f=73&t=15638&sid=2845471e2225b27779e82d0602a95fbe If you want to seem them for yourself, type print(HeldDevices); or w/e into the console while in game. Notice, that these tables ONLY exist if and only if you have a mod loaded with scripted HeldDeviecs (or w/e). If you don't, then the table will be empty and therefore nil. Try doing print(MOSRotatings);, I think there are some scripted vanilla versions of these. |
Author: | Darlos9D [ Thu Dec 10, 2009 7:39 pm ] |
Post subject: | Re: Running Lua On Attachables |
So basically you have to set up differently named global variables for each individual script, and then another script calls their Update, Create, and Destroy functions? Well, that's cool, but what actually calls them? Also, you said you can get the objects script like HeldDevices[PresetID]. So why would you go about doing the for loop method when you can just plug in the things PresetID? Or is PresetID something weird that's hard to know? |
Author: | Kyred [ Thu Dec 10, 2009 7:45 pm ] |
Post subject: | Re: Running Lua On Attachables |
Darlos9D wrote: So basically you have to set up differently named global variables for each individual script, and then another script calls their Update, Create, and Destroy functions? Well, that's cool, but what actually calls them? Also, you said you can get the objects script like HeldDevices[PresetID]. So why would you go about doing the for loop method when you can just plug in the things PresetID? Or is PresetID something weird that's hard to know? I've been looking for a way to lookup PresetID's, but I can't find a way how other than this loop method. If anyone can figure this out, please let me know. |
Author: | Darlos9D [ Thu Dec 10, 2009 8:47 pm ] |
Post subject: | Re: Running Lua On Attachables |
I suppose I could use a varied version of my "emitter on gun emits particles that find gun and run a script" method that I posted a while back. Having definite Create and Update functions to call on and having persistent variables would definitely help, since relying entirely on the emitted particle script doesn't really have these luxuries. So thanks, I think you alleviated my secondary fire woes. Heck, this even gives us a nice long-term solution since if we put the script on the weapons, then everything will be easy to get to work once we CAN easily run scripts on attachables, since the scripts will already be written in the right place in the right way. |
Author: | Abdul Alhazred [ Thu Dec 10, 2009 8:52 pm ] |
Post subject: | Re: Running Lua On Attachables |
I know of the other tables but cannot seem to access HeldDevices, even with a scripted weapon loaded. Perhaps you could have a look and tell me what I'm doing wrong? Edit: Removed zip. |
Author: | Kyred [ Thu Dec 10, 2009 9:24 pm ] |
Post subject: | Re: Running Lua On Attachables |
Oh, that would fall under the table HDFirearms. When I made and tested this discovery, I was using the Harmonizer (the shield contest winner), and that fell under HeldDevices. You choose the table for w/e class of object your script is running on. Sorry, my mistake. |
Author: | Abdul Alhazred [ Thu Dec 10, 2009 9:48 pm ] |
Post subject: | Re: Running Lua On Attachables |
I should have realized it. Thanks for helping out! |
Author: | Kyred [ Thu Dec 10, 2009 10:07 pm ] |
Post subject: | Re: Running Lua On Attachables |
It works! Just make sure that when you drop the weapon, and CC takes control of calling Update again, that your sctript stops executing Update();. Otherwise you will get a stack overflow. |
Author: | Abdul Alhazred [ Thu Dec 10, 2009 10:34 pm ] |
Post subject: | Re: Running Lua On Attachables |
Yes, I also got it running but I immediately ran in to a different problem. Anything stored in a "self-variable" seems to be lost between updates. Any ideas? |
Author: | Darlos9D [ Thu Dec 10, 2009 10:41 pm ] |
Post subject: | Re: Running Lua On Attachables |
Abdul Alhazred wrote: Yes, I also got it running but I immediately ran in to a different problem. Anything stored in a "self-variable" seems to be lost between updates. Any ideas? |
Author: | Kyred [ Thu Dec 10, 2009 10:44 pm ] |
Post subject: | Re: Running Lua On Attachables |
Abdul Alhazred wrote: Yes, I also got it running but I immediately ran in to a different problem. Anything stored in a "self-variable" seems to be lost between updates. Any ideas? Trust me, with the right tricks, this can be very usable. |
Author: | Abdul Alhazred [ Fri Dec 11, 2009 7:29 pm ] |
Post subject: | Re: Running Lua On Attachables |
This turned out to require a few more hacks than I had hoped so I would like to have a discussion about different solutions to the various problems mentioned above, and have there for attached a mod containing my way of doing what Kyred discovered. Attachment: The weapon pointers are stored in a global table using the Sharpness of the weapon as key. Since I have not been able to access the weapons Destroy function this table will not stop growing, and I was forced to add some primitive "garbage collection". I would be very interested to hear any thoughts about improvements and alternative solutions. Edit: fixed url |
Author: | Darlos9D [ Sat Dec 12, 2009 4:10 am ] |
Post subject: | Re: Running Lua On Attachables |
Alright, I've been poking around with going through lists like HeldDevices. They seem to hold one of two things: either a "userdata" type object that holds all of the typical MO info (ID, Pos, Vel, Mass, etc), or a table that holds references to the aforementioned control functions. Each one has a rather specific string for a key. For the userdata objects, it's of the form obj#####, and for the tables it's pre#####. If we can just find that pre##### somehow... Hey, is there some way in lua to look at an object and find all of its members? I'd like to pull apart userdata and see if maybe there's a reference in there somewhere, or... something. Also, who else thinks its weird there's a table that holds other tables AND userdata objects in no particular order? |
Page 1 of 1 | All times are UTC [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |