View unanswered posts | View active topics It is currently Fri Dec 27, 2024 9:28 am



Reply to topic  [ 15 posts ] 
 Running Lua On Attachables 
Author Message
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post 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
Keep in mind, I am scripting this for weapons. If you are doing something else, change HeldDevices to something else.

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.


Last edited by Kyred on Thu Dec 10, 2009 7:18 pm, edited 1 time in total.



Tue Dec 08, 2009 9:50 pm
Profile
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post 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?


Wed Dec 09, 2009 9:37 am
Profile
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post 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.


Thu Dec 10, 2009 6:52 pm
Profile
User avatar

Joined: Mon Jul 16, 2007 9:50 am
Posts: 1512
Location: Tallahassee, FL
Reply with quote
Post 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?


Thu Dec 10, 2009 7:39 pm
Profile YIM
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post 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?
You could spawn an invisible MOPixel to call the update on it's update. Or, if you mod is specialized (like XtremeEdge), you can have the Scene control this. Or you can get even crazier and modify your actor's Update function to do this.

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.


Thu Dec 10, 2009 7:45 pm
Profile
User avatar

Joined: Mon Jul 16, 2007 9:50 am
Posts: 1512
Location: Tallahassee, FL
Reply with quote
Post 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.


Thu Dec 10, 2009 8:47 pm
Profile YIM
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post 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.


Last edited by Abdul Alhazred on Fri Dec 11, 2009 7:30 pm, edited 1 time in total.



Thu Dec 10, 2009 8:52 pm
Profile
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post 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.


Thu Dec 10, 2009 9:24 pm
Profile
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post Re: Running Lua On Attachables
I should have realized it. Thanks for helping out!


Thu Dec 10, 2009 9:48 pm
Profile
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post 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.

Image


Thu Dec 10, 2009 10:07 pm
Profile
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post 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?


Thu Dec 10, 2009 10:34 pm
Profile
User avatar

Joined: Mon Jul 16, 2007 9:50 am
Posts: 1512
Location: Tallahassee, FL
Reply with quote
Post 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?
Uh oh. If this is true then there really isn't much point in pursuing it for me...


Thu Dec 10, 2009 10:41 pm
Profile YIM
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post 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?
Yeah, it's because of pointer differences. Make sure the "self" you are referencing is the same one that is used in the Update() function. What I did was assign "self" to a global in the Create() function. Not ideal when you are using multiple weapons, unless your index each global with an MOPixel (edit the MOPixel's preset name and use that in the index. self.PresetName actually transfers among all pointers of that object.).

Trust me, with the right tricks, this can be very usable.


Thu Dec 10, 2009 10:44 pm
Profile
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post 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:
LuaSMG_v1.zip [1.41 KiB]
Downloaded 158 times
I had some issues reliably finding the weapons using the SceneMan:GetMOIDPixel-method so I'm actually looping through all MOIDs every update. I'm mostly interested in creating contents for missions so I decided to put the calls to the weapon functions in the mission activity. You can find the Activity Code Here.

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


Last edited by Abdul Alhazred on Sat Dec 12, 2009 2:13 pm, edited 1 time in total.



Fri Dec 11, 2009 7:29 pm
Profile
User avatar

Joined: Mon Jul 16, 2007 9:50 am
Posts: 1512
Location: Tallahassee, FL
Reply with quote
Post 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?


Sat Dec 12, 2009 4:10 am
Profile YIM
Display posts from previous:  Sort by  
Reply to topic   [ 15 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.070s | 14 Queries | GZIP : Off ]