News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: Lua Tutorial: Exceeding 112KB Script Limit  (Read 5240 times)

0 Members and 1 Guest are viewing this topic.

Offline Altamurenza

  • Full Member
  • ***
  • Posts: 118
  • Gender: Male
  • I love cheat, unique, realistic, & expansion mod.
    • View Profile
Lua Tutorial: Exceeding 112KB Script Limit
« on: February 24, 2022, 12:03:43 PM »
- INTRODUCTION -

I'm quite sure you guys already know this limitation in Bully. But.. if you haven't notice it yet, let me explain it to you briefly.

So.. Bully has some kind of limitation on their engine i suppose, which is not allowing you to put any of compiled script beyond 112kb. It will crash the game upon starts if doing so. We used to solve this problem by code-splitting, we certainly use ImportScript function in the main script in order to retrieve the shards of code from another script. That's what we keep doing over the years in order to avoid 112kb limit.

Well, actually.. there is another way better than ImportScript.
You can even load 1.8mb of compiled script (tested by me so far) without splitting it.

- DISCLAIMER -

Based on the introduction above, i have no single intention to call code-splitting in negative connotation.

I mean, i have no problem with code-spiltting anyway, it's a good thing, easy to understand, and clear about their scopes. I often do that to my mods because i like well-structured scripts.. (maybe). Even most people often do that too, also in some cases, they MUST do it for whatever reasons they have.

- LOADFILE -

I assume you already heard about loadfile, yes.. this function is available in Lua modified-standard library 5.0.2 used by Bully. Its job is pretty much similar to ImportScript as they do call upon another script into the main one. But.. there are few difference between them, one of the most noticeable is the ImportScript call scripts from Scripts.img and loadfile call scripts from directory.

* if you have no idea what directory is, you may read this wikipedia page before continuing *

1) DEFAULT PATH

Based on Lua 5.0.2 documentation, loadfile function requires a specific file path as its main argument.
But as far as observed it myself, some versions of Bully may have different default directory.

I only know the default directory of loadfile on SE (PC) and AE (Android) since i don't play any other version except both.
    PC default directory = where Bully.exe is located
    Android default directory =  where storage folder is located (3 folders before "../Android/")

However, i would STRONGLY advise you to place your script in Scripts folder as they're related.
    PC = "../Bully - Scholarship Edition/Scripts" (Bully folder > Scripts folder)
    Android = "../Android/data/com.rockstargames.bully/files/BullyOrig/Scripts"
    * if BullyOrig or Scripts folder doesn't exist, create new one *

2) FILE REQUIREMENT

In the modified version of Lua standard library we have, loadfile function cannot read anything but compiled Lua 5.0.2.
That means, it cannot read Lua source code directly or any Lua 5.1 (and beyond) compiled script.

Otherwise, it will show you an error message. --> "parser not loaded"

If your file is already compiled by LuaC, you can rename it and its extension to anything you want.
The extension of file does not really necessary as the game also recognize it, as long as it's compiled by LuaC.

3) OTHER

loadfile mechanism is quite similar to dofile, make sure you don't confuse with both.
I would not explain dofile here, it's quite same already.

- EXCEEDING 112KB TUTORIAL -

1. Create new Lua script and name it RESOURCE.lua
2. Fill the RESOURCE.lua with some gigantic-size contents, you can copy this for example:
Code: [Select]
-- this is all .NIF files available in World.img
-- EDIT: Table is too long to be here, i uploaded it on the link below

https://pastebin.com/jXUUYbcG
3. Compile RESOURCE.lua with LuaC and name it to RESOURCE.lur
    * you can clearly see RESOURCE.lur size is bigger than 112kb *
4. Create new Lua script and name it STimeCycle.lua
5. Fill the STimeCycle.lua with this hybrid script:
Code: [Select]
function main()
while SystemIsReady() or AreaIsLoading() do
Wait(0)
end

local PC = type(_G.ClassMusicSetPlayers) == "function" and true or false

local DIR = PC and "Scripts" or "storage/emulated/0/Android/data/com.rockstargames.bully/files/BullyOrig/Scripts"
local FILE_NAME = "RESOURCE.lur" -- replace this with your file name!

local FILE, ERROR = loadfile(DIR.."/"..FILE_NAME)
if FILE then
FILE()

if not PC then
Wait(1500)
end

local OPTION, X, Y, Z = 1
while true do
Wait(0)

X, Y, Z = PlayerGetPosXYZ()

OPTION = IsButtonBeingPressed(PC and 0 or 8, 0) and (OPTION - 1 < 1 and table.getn(TABLE) or OPTION - 1) or (IsButtonBeingPressed(PC and 1 or 11, 0) and (OPTION + 1 > table.getn(TABLE) and 1 or OPTION + 1) or OPTION)
if IsButtonBeingPressed(PC and 3 or 6, 0) then
CreatePersistentEntity(TABLE[OPTION].Name, X + 2, Y + 2, Z, 0, AreaGetVisible())
end

if not PC then
if ItemGetCurrentNum(420) then
GiveWeaponToPlayer(420, false)
end
PlayerLockButtonInputsExcept(true, 2, 3, 4, 5, 7, 9, 14, 15, 16, 17, 18, 19)
end

_G[PC and "TextPrintString" or "MinigameSetAnnouncement"]((PC and "- EXCEED 112KB DEMO -\n\n" or "")..""..OPTION..") "..TABLE[OPTION].Name, PC and 0 or true, PC and 1 or nil)
end
else
while true do
Wait(0)

TextPrintString(tostring(ERROR), 0, 2)
end
end
end
6. Compile STimeCycle.lua with LuaC and name it to STimeCycle.lur
7. Copy RESOURCE.lur and paste it into your Scripts folder
    * SE (PC) = "../Bully - Scholarship Edition/Scripts"
    * AE (Android) = "../Android/data/com.rockstargames.bully/files/BullyOrig/Scripts"
8. Insert STimeCycle.lur into your Scripts.img and don't forget to rebuild
9. Go play Bully and see the results

- CLOSING STATEMENT -

This is my first tutorial in Bully-Board, i hope i'll be able to share some more experiences soon.
Make sure you read all the explanations carefully to avoid any confusion.

External links:
- Compilation, Execution, and Errors via lua.org
- Loads a Lua file and parses it via gammon.com

Check out my other tutorials:
- Lua Tutorial: Non-STimeCycle Freeroam Mods
- Lua Tutorial: Check If a File Exists in Directory
« Last Edit: April 07, 2022, 03:29:38 PM by Altamurenza »

Offline RBS ID

  • Jr. Member
  • **
  • Posts: 67
  • Gender: Male
  • I don't know.
    • View Profile
    • This is website title.
Re: Lua Tutorial: Exceeding 112KB Script Limit
« Reply #1 on: February 24, 2022, 09:12:44 PM »
I noticed when my compiled script is too big, there will be the sound of rats, then not long after, the game crashed. And sometime the AcidPool effect spawned. Kinda strange. (Bully AE)

Btw good tutorial & very useful.  :)
« Last Edit: February 24, 2022, 10:15:46 PM by RanggaBS »

Offline Razc01na

  • Lord Slayer
  • Full Member
  • ***
  • Posts: 156
  • Gender: Male
  • Doom is life
    • View Profile
Re: Lua Tutorial: Exceeding 112KB Script Limit
« Reply #2 on: February 25, 2022, 07:21:33 PM »
One other way could be to strip debug info from compiled scripts

Offline Altamurenza

  • Full Member
  • ***
  • Posts: 118
  • Gender: Male
  • I love cheat, unique, realistic, & expansion mod.
    • View Profile
Re: Lua Tutorial: Exceeding 112KB Script Limit
« Reply #3 on: February 26, 2022, 03:21:47 AM »
One other way could be to strip debug info from compiled scripts

That's for sure, it may reduce 1/4 to 1/2 sizes of the output file.
But it won't work on the examples given above.

If you compile this https://pastebin.com/jXUUYbcG, you won't be able to achieve 112 kb in any way.
Compiled with strip debug option: 146 KB
Compiled with default option: 204 KB

Also, it would seem so bad if i had to split the table contents even though they had the same purpose.
However, it's about the preferences after all.

Offline RBS ID

  • Jr. Member
  • **
  • Posts: 67
  • Gender: Male
  • I don't know.
    • View Profile
    • This is website title.
Re: Lua Tutorial: Exceeding 112KB Script Limit
« Reply #4 on: March 21, 2022, 04:02:27 AM »
Sorry, I just replied to this post now.

This method doesn't work on Bully AE. The game force stop when will loading screen.

Offline Altamurenza

  • Full Member
  • ***
  • Posts: 118
  • Gender: Male
  • I love cheat, unique, realistic, & expansion mod.
    • View Profile
Re: Lua Tutorial: Exceeding 112KB Script Limit
« Reply #5 on: March 22, 2022, 10:06:19 AM »
Sorry, I just replied to this post now.

This method doesn't work on Bully AE. The game force stop when will loading screen.

It worked perfectly on my mod.
This is how i use loadfile on my mod:

local FILE, ERROR = loadfile("storage/emulated/0/Android/data/com.rockstargames.bully/files/BullyOrig/Scripts/RESOURCE.lur")
if FILE then
  FILE()
else
  Wait(1000) -- avoid MinigameSetAnnouncement crash
  while true do
    Wait(0)
    MinigameSetAnnouncement("RESOURCE.lur "..(tostring(ERROR) == "parser not loaded" and "is not compiled!" or "is missing!"), true)
  end
end

Offline RBS ID

  • Jr. Member
  • **
  • Posts: 67
  • Gender: Male
  • I don't know.
    • View Profile
    • This is website title.
Re: Lua Tutorial: Exceeding 112KB Script Limit
« Reply #6 on: March 23, 2022, 05:26:59 AM »
I really don't know why the game still crashed on my phone.

Code: [Select]

function F_LoadFile(PATH)
    local FILE, ERROR = loadfile(PATH)
    if FILE then
        FILE()
    else
        Wait(1000)
        while true do
            Wait(0)
            MinigameSetAnnouncement ("ERROR", true)
        end
    end
end

function main()
  Wait(2000)
  local s = 1
 
 F_LoadFile("storage/emulated/0/Android/data/com.rockstargames.bully/files/BullyOrig/Scripts/RESOURCE.lur")
  Wait(3000)
  while true do
    Wait(0)
    local X, Y, Z = PlayerGetPosXYZ ()
    if IsButtonBeingPressed (8, 0) then
s = s - 1
if s < 1 then
  s = table.getn(TABLE)
end
    elseif IsButtonBeingPressed (11, 0) then
s = s + 1
if s > table.getn(TABLE) then
  s = 1
end
    elseif IsButtonBeingPressed (6, 0) then
CreatePersistentEntity(TABLE[s].Name, X + 2, Y + 2, Z, math.random(360), AreaGetVisible())
    end
    MinigameSetAnnouncement (s .. ". " .. TABLE[s].Name, true)
  end
end


Offline Altamurenza

  • Full Member
  • ***
  • Posts: 118
  • Gender: Male
  • I love cheat, unique, realistic, & expansion mod.
    • View Profile
Re: Lua Tutorial: Exceeding 112KB Script Limit
« Reply #7 on: March 23, 2022, 09:32:14 AM »
I really don't know why the game still crashed on my phone.

Code: [Select]

function F_LoadFile(PATH)
    local FILE, ERROR = loadfile(PATH)
    if FILE then
        FILE()
    else
        Wait(1000)
        while true do
            Wait(0)
            MinigameSetAnnouncement ("ERROR", true)
        end
    end
end

function main()
  Wait(2000)
  local s = 1
 
 F_LoadFile("storage/emulated/0/Android/data/com.rockstargames.bully/files/BullyOrig/Scripts/RESOURCE.lur")
  Wait(3000)
  while true do
    Wait(0)
    local X, Y, Z = PlayerGetPosXYZ ()
    if IsButtonBeingPressed (8, 0) then
s = s - 1
if s < 1 then
  s = table.getn(TABLE)
end
    elseif IsButtonBeingPressed (11, 0) then
s = s + 1
if s > table.getn(TABLE) then
  s = 1
end
    elseif IsButtonBeingPressed (6, 0) then
CreatePersistentEntity(TABLE[s].Name, X + 2, Y + 2, Z, math.random(360), AreaGetVisible())
    end
    MinigameSetAnnouncement (s .. ". " .. TABLE[s].Name, true)
  end
end


I tried your code, and it's fine in mine.
Screenshot:


I forgot to mention that i'm running Bully: Anniversary Edition on BlueStacks 5.
It's based on Android 7 and the Bully AE version is 1.0.0.17

Back to the topic, i've never seen such a crash caused by loadfile actually.
Do you really use that script and the table given above?

Please have a try the same scripts as me, download in the attachments below.
If it doesn't work, then it actually prove the device is matter to loadfile.


Offline RBS ID

  • Jr. Member
  • **
  • Posts: 67
  • Gender: Male
  • I don't know.
    • View Profile
    • This is website title.
Re: Lua Tutorial: Exceeding 112KB Script Limit
« Reply #8 on: March 23, 2022, 11:13:43 AM »
Yes, I've tried it. I've been try your script & props table script that you've given, compile it, and move it to Bully "Scripts" folder, but still crashed.

Edit:
I've been try your script in the attachment and it works now.
« Last Edit: March 23, 2022, 11:25:22 AM by RanggaBS »

Offline Altamurenza

  • Full Member
  • ***
  • Posts: 118
  • Gender: Male
  • I love cheat, unique, realistic, & expansion mod.
    • View Profile
Re: Lua Tutorial: Exceeding 112KB Script Limit
« Reply #9 on: March 23, 2022, 08:49:24 PM »
It's funny how i did not change a thing and it works for you lmao. I assume there was something on your Scripts.img that caused your game to crash. Also, make sure you didn't use Scripts.img from SE to AE because they're not interchangeable one to another. At least i wouldn't recommend that since SE version was lack of multiplayer scripts and some other things that required in AE.

Offline RBS ID

  • Jr. Member
  • **
  • Posts: 67
  • Gender: Male
  • I don't know.
    • View Profile
    • This is website title.
Re: Lua Tutorial: Exceeding 112KB Script Limit
« Reply #10 on: March 23, 2022, 10:21:14 PM »
Yeah, XD, maybe there's something wrong with my Scripts.img. Because when I try to use a new fresh Scripts.img from obb, it works fine.

Btw, thanks a lot.  :D
« Last Edit: March 24, 2022, 01:12:01 AM by RanggaBS »

Offline Altamurenza

  • Full Member
  • ***
  • Posts: 118
  • Gender: Male
  • I love cheat, unique, realistic, & expansion mod.
    • View Profile
Re: Lua Tutorial: Exceeding 112KB Script Limit
« Reply #11 on: March 25, 2022, 05:23:29 AM »
Yeah, XD, maybe there's something wrong with my Scripts.img. Because when I try to use a new fresh Scripts.img from obb, it works fine.

Btw, thanks a lot.  :D

Alright, cool 8).