Lua

Discuss anything about DraStic here.
Lordus
Posts:517
Joined:Mon Aug 05, 2013 9:05 pm
Lua

Post by Lordus » Mon Apr 25, 2016 10:42 am

As of the latest beta (2.5.0.0a build 86) Lua support was added to DraStic.
You can share scripts here, or let us know, if you have reasonable ideas of functionality that you would want to be added to the interface.

Here's how it works (also found in Help > Instructions in the app):
Spoiler!

8. Lua interface

Lua scripts can be placed inside DraStic/scripts and will be loaded if:
- A script named <ROM file>.lua is found when a game is loaded.
- A script named 'default.lua' is present. This default script will be loaded for any game.

8.1 Lua callbacks

Define the following 3 callbacks in your Lua script:

// Will be called whenever a new game is loaded.
// 'game' is a string containing the ROM file name without extension.
// Use this to open files, if you want to log anything for example.
function on_load(game)
end


// Will be called when a game is unloaded (before a new game is loaded, or when DraStic is exited).
// Use this to close any files you opened for example.
function on_unload()
end


// Will be called every frame.
// You can use this callback to alter button state or memory.
function on_frame_update()
end


8.2 Lua constants

The following constants can be accessed from your script to get/set button state:
- drastic.C.BUTTON_UP
- drastic.C.BUTTON_DOWN
- drastic.C.BUTTON_LEFT
- drastic.C.BUTTON_RIGHT
- drastic.C.BUTTON_A
- drastic.C.BUTTON_B
- drastic.C.BUTTON_X
- drastic.C.BUTTON_Y
- drastic.C.BUTTON_L
- drastic.C.BUTTON_R
- drastic.C.BUTTON_START
- drastic.C.BUTTON_SELECT
- drastic.C.BUTTON_FAST_FORWARD
- drastic.C.BUTTON_TOUCH

If you want to test if a certain button is set you can do:
if ((buttons & drastic.C.BUTTON_A) ~= 0) then
...
end

If you want to set a certain button:
buttons = buttons | drastic.C.BUTTON_A

8.3 Core Lua functions

drastic.get_path()
Returns a string containing the path to the 'DraStic' folder.

drastic.get_buttons()
Returns an integer with the current button state.
The state of individual buttons can be tested with the constants in drastic.C.<constant>.

drastic.set_buttons(buttons)
Sets DraStic's button state to the integer passed as parameter.

drastic.get_touch()
Returns an integer containing the touch X coordinate in the upper 16 bits,
and the touch Y coordinate in the lower 16 bits.

drastic.set_touch(touch_x, touch_y)
Sets DraStic's touch coordinates to the values passed as parameters.
This only sets the coordinates, if you want to actually press/depress the touchscreen,
set and unset drastic.C.BUTTON_TOUCH with drastic.set_buttons().

Memory access functions
To get/set the emulated DS's memory you can use functions in the form:
drastic.get_ds_memory_<cpu>_<size>(address)
drastic.set_ds_memory_<cpu>_<size>(address, value)

Here are all possible combinations:
- drastic.get_ds_memory_arm9_8(address)
- drastic.get_ds_memory_arm9_16(address)
- drastic.get_ds_memory_arm9_32(address)
- drastic.get_ds_memory_arm7_8(address)
- drastic.get_ds_memory_arm7_16(address)
- drastic.get_ds_memory_arm7_32(address)
- drastic.set_ds_memory_arm9_8(address, value)
- drastic.set_ds_memory_arm9_16(address, value)
- drastic.set_ds_memory_arm9_32(address, value)
- drastic.set_ds_memory_arm7_8(address, value)
- drastic.set_ds_memory_arm7_16(address, value)
- drastic.set_ds_memory_arm7_32(address, value)

8.4 Android Lua functions

android.get_axis_lx()
Returns a float with the left thumb stick X-axis value, if an external controller is used.

android.get_axis_ly()
Returns a float with the left thumb stick Y-axis value, if an external controller is used.

android.get_axis_rx()
Returns a float with the right thumb stick X-axis value, if an external controller is used.

android.get_axis_ry()
Returns a float with the right thumb stick Y-axis value, if an external controller is used.

android.get_rotation()
Returns an integer with the current device rotation in degrees.
The range is [0,-180[ for counter-clockwise rotation and [0,180] for clockwise rotation.

android.set_layout(layout)
Sets the current screen layout (range [0,4], in the order the layouts are displayed in the app's menus).

android.show_overlay(show)
Shows (1) or hides (0) the virtual gamepad overlay.

android.set_screen_swap(swap)
Enables (1) or disables (0) screen swapping.
This is an example script, that will automatically press D-Pad Left/Right based on device rotation.
If you want to try it, then copy the file to DraStic/scripts and rename it either to 'default.lua' for it to be active for all games, or to '<ROM file>.lua' for it to be active for a specific game only.
rotation.lua
(576Bytes)Downloaded 3243 times

User avatar
Kyousuke753
Posts:846
Joined:Sun Apr 10, 2016 10:30 am
Location:???

Re: Lua

Post by Kyousuke753 » Mon Apr 25, 2016 10:46 am

I'll try this and see what it does...thank you lordus. :) also this is a good for testing on Super Mario 64 DS.

I opted in for Beta Testing but the update didn't arrive it's been 6 days now.
Last edited by Kyousuke753 on Sat Apr 30, 2016 12:15 pm, edited 1 time in total.
Image

Lordus
Posts:517
Joined:Mon Aug 05, 2013 9:05 pm

Re: Lua

Post by Lordus » Mon Apr 25, 2016 11:09 am

You probably need to reinstall the app from Google Play to get the beta version.

User avatar
ericbazinga
Posts:1123
Joined:Mon Apr 13, 2015 6:46 pm
Location:Somewhere in VRchat
Contact:

Re: Lua

Post by ericbazinga » Mon Apr 25, 2016 11:37 am

Wow the app has a brand-new look! I'm both surprised and impressed at the same time!
Image

You can also find me on Reddit (u/Ericbazinga) and Discord (in the DraStic Discord server). You'll find me in those places almost daily and here almost never.

User avatar
JeaneMaggie
Posts:126
Joined:Sun Dec 06, 2015 12:11 pm

Re: Lua

Post by JeaneMaggie » Mon Apr 25, 2016 1:14 pm

Lordus wrote:As of the latest beta (2.5.0.0a build 86) Lua support was added to DraStic.
You can share scripts here, or let us know, if you have reasonable ideas of functionality that you would want to be added to the interface.
Would it possible to add control mappings to the physical buttons of the phone to use alongside the virtual controller, such as the volume buttons and the power button? An example would be to map the power button as the menu button for DraStic?
No. No no no no no no.

Lordus
Posts:517
Joined:Mon Aug 05, 2013 9:05 pm

Re: Lua

Post by Lordus » Mon Apr 25, 2016 2:51 pm

JeaneMaggie wrote:
Lordus wrote:As of the latest beta (2.5.0.0a build 86) Lua support was added to DraStic.
You can share scripts here, or let us know, if you have reasonable ideas of functionality that you would want to be added to the interface.
Would it possible to add control mappings to the physical buttons of the phone to use alongside the virtual controller, such as the volume buttons and the power button? An example would be to map the power button as the menu button for DraStic?
An app can't easily (and really shouldn't at all) override the power button. You can already map the volume buttons in the external controller settings, though.

xperia64
Posts:307
Joined:Fri Feb 28, 2014 7:41 pm

Re: Lua

Post by xperia64 » Mon Apr 25, 2016 10:22 pm

A Super Mario 64 DS improved control scheme script:
http://pastebin.com/BrM2yDKe

L+R is Enable/Disable
The left joystick does the touchscreen joystick
Y has been remapped to A since there should be no reason to use the run button

Known Issues:
Going through doors with this enabled and the joystick tilted will make the joystick stuck. Just disable and enable the script again.
Last edited by xperia64 on Tue Apr 26, 2016 1:36 pm, edited 1 time in total.

User avatar
Kyousuke753
Posts:846
Joined:Sun Apr 10, 2016 10:30 am
Location:???

Re: Lua

Post by Kyousuke753 » Tue Apr 26, 2016 4:00 am

Image

Seeing new updates is really refreshing. :D The Beta update arrived 3 days ago.
Image

User avatar
Kyousuke753
Posts:846
Joined:Sun Apr 10, 2016 10:30 am
Location:???

Re: Lua

Post by Kyousuke753 » Tue May 17, 2016 7:28 am

So I've tested this with Mario Kart DS and it works like a accelerometer I can drift and turn fine.

This script good with racing games. Thank you Lordus

If you need anything for testing please post here so I can test too, I love testing.
Image

Exedion
Posts:1
Joined:Fri Jun 10, 2016 10:48 pm

Re: Lua

Post by Exedion » Fri Jun 10, 2016 10:54 pm

One question: with lua script can hidden virtual buttons automatically when enter in a battle? That will be a important feature

Post Reply