Welcome to Open Carnage

A resource for Halo Custom Edition and MCC modding, with unique means of rewarding content creation and support. Have a wander to see why we're worth the time! - EST. 2012

MXWK

A script that auto-loads a command in another script

Hey guys! How's everyone doing today? 

 

Honestly, I've been up all night trying to accomplish something. I don't know much about Lua scripting which made it quite tough for me, I managed to get some things working, but I still want to complete the idea. I think the outcome script may be of good use.

 

Let us assume that we have script "X". Script "X" has "A" command, my objective is to load the "A" command from script "X" automatically, whether it be by editing script "X" or by making script "Y" which is a script that loads "A" command from script "X" automatically i.e when a player joins the server. 

 

So I've been searching online and have found a couple of things, all of them don't work though. I'll mention them here, maybe someone will benefit from them.

 

So I tried using the following commands:

execute_command(string, PlayerIndex, Echo)

 

lua_call <script name> <function name> [arguments]

 

os.execute("<command>")

None of them worked!

 

I tried all possible combinations of syntax, still no results. I also tried taking snippets of code from other people's work, editing them, and running them as individual scripts, that didn't go well either. Well, it was fun that's for sure. But I still want to solve this problem, any help would be greatly appreciated!

 

Thanks for reading this far,

 

Have a good day!

Share this post


Link to post
Share on other sites

Tiddy-bits:

Edit:

Sorry I misunderstood the original question, but if you wanted to call a function within an .lua script that is already loaded by halo, you can use this command from halo

lua_call <script name> <function name> [arguments]

If you wanted to run that command from within a lua script you would wrap that command within execute_command().  However if you tried to call a function that was used as an event callback within a script the parameters that are given to it through the callback system will no longer work afaik.

 

Like imagine you had a script called test1.lua

api_version = "1.12.0.0"

function OnScriptLoad()
    
end
function OnScriptUnload()
    
end

function test_function1(aString)
    say_all(aString)
end

And then you had a script called test2.lua

api_version = "1.12.0.0"

function OnScriptLoad()
    register_callback(cb['EVENT_JOIN'], "OnPlayerConnect")
end
function OnScriptUnload()

end

function OnPlayerConnect(PlayerIndex)
  execute_command("lua_call test1 test_function1 'say something'")
end

test2.lua would run the function test_function1() from the test1.lua, and it would pass the string 'say something' to it when it called it.  If you tried to do this same thing with a function that is done from a callback like register_callback(cb['EVENT_JOIN'], "OnPlayerConnect") then called OnPlayerConnect in place of test_function1 it wouldn't be able to execute with the parameters that are passed through the callback system, it would still execute, but most likely wouldn't do what you wanted it to.

 

This is what I think would work:

If you want one script to perform one action like running a bit of code or using execute_command() exactly when a player joins the server, you can register a callback function like this: (the double dash -- in front of a line means it's a comment and won't affect the code when it's being run)

api_version = "1.12.0.0"

function OnScriptLoad()
    register_callback(cb['EVENT_JOIN'], "OnPlayerJoin")
    
end
function OnScriptUnload()

end
function OnPlayerJoin(PlayerIndex)
    --put code here to execute when a player joins
    --this is an example that says to everyone in the server the player index of who just joined
    say_all(PlayerIndex)
    
    --this is an example that executes the command nades on the PlayerIndex who just joined
    execute_command("nades "..PlayerIndex.." 7")
    --the use of .. is the concatenation operator that tells lua to stick two strings together
    --strings are enclosed in "" but there are special characters that have to be escaped inside strings like
    --if you use "something\somethingelse" it needs to be changed to "something\\somethingelse" for lua to recognize it properly
    --this link goes over the special characters https://www.tutorialspoint.com/lua/lua_strings.htm
end

 

For a list of all the callback functions you can reference the sapp documentation http://halo.isimaginary.com/SAPP Documentation Revision 2.5.pdf  (The section labeled "Event Callbacks") , but they all work the same way, but some take a different number of parameters, like this example OnPlayerDeath() in addition to PlayerIndex, there is a Causer parameter to determine what killed a player.  Some of them also use a return statement, which I could give an example of if need be.

api_version = "1.12.0.0"

function OnScriptLoad()
    register_callback(cb['EVENT_DIE'], "OnPlayerDeath")
    
end
function OnScriptUnload()

end

function OnPlayerDeath(PlayerIndex,Causer)
    --^There are 2 parameters for EVENT_DIE^
    --PlayerIndex refers to who died, while the Causer refers to what caused their death, it can be a number from -1 to 16 I think, 
	--where a number between 1 to 16 is another player and -1 to 0 is a different mode of death, like switching teams, falling damage or killed with a vehicle.
    
    --put code here to execute when a player dies
    
end

For every event callback that is registered within OnScriptLoad() each one needs to be on it's own line and you'll need to create a new function somewhere else in the code to match each individual callback.  You can have all of the callbacks in a single lua script, as long as they aren't repeated I think it should be fine. Like if the two code bits above were in the same script:

api_version = "1.12.0.0"

function OnScriptLoad()
    register_callback(cb['EVENT_DIE'], "OnPlayerDeath")
    register_callback(cb['EVENT_JOIN'], "OnPlayerJoin")
    
end
function OnScriptUnload()

end

function OnPlayerDeath(PlayerIndex,Causer)
    --^There are 2 parameters for EVENT_DIE^
    --PlayerIndex refers to who died, while the Causer refers to what caused their death, it can be a number from -1 to 16 I think, where a number between 1 to 16 is another player and -1 to 0 is a different mode of death, like switching teams, falling damage or killed with  a vehicle.
    
    --put code here to execute when a player dies
    
end

function OnPlayerJoin(PlayerIndex)
    --put code here to execute when a player joins
    --this is an example that says to everyone in the server the player index of who just joined
    say_all(PlayerIndex)
    
    --this is an example that executes the command nades on the PlayerIndex
    execute_command("nades "..PlayerIndex.." 7")
    --the use of .. is the concatenation operator that tells lua to stick two strings together
    --strings are enclosed in "" but there are special characters that have to be escaped inside strings like
    --if you use "something\somethingelse" it needs to be changed to "something\\somethingelse" for lua to recognize it properly
    --this link goes over the special characters https://www.tutorialspoint.com/lua/lua_strings.htm
end

Something else to keep in mind is that when register callback is called in a script, like this:   register_callback(cb['EVENT_JOIN'], "OnPlayerJoin")   The text that says "OnPlayerJoin" is arbitrary and can be changed to whatever you want as long as there is a matching function in the code to go along with it:

api_version = "1.12.0.0"

function OnScriptLoad()
    register_callback(cb['EVENT_JOIN'], "OnPlayerConnectToServer")
    --the part that says 'EVENT_JOIN' does not change, but the 2nd part has been changed to "OnPlayerConnectToServer"
end
function OnScriptUnload()

end
--the below function was changed to OnPlayerConnectToServer to match the register_callback
function OnPlayerConnectToServer(PlayerIndex)
    --put code here to execute when a player joins
    --this is an example that says to everyone in the server the player index of who just joined
    say_all(PlayerIndex)
end

 

MXWK likes this

Share this post


Link to post
Share on other sites

@mouseboyxThanks for the explanation bro, honestly I've been playing around with one of @Chalwk's scripts lol. It's Admin Chat. I thought it would be nice to have everyone chat in the Rcon environment so I made my version called Rcon Chat. I basically edited 2 or so lines lol and got the script to show the chat to all the players, even those who are not Admin, which lead to me wanting to complete the project.

 

The only issue was that it wasn't loading automatically, and I didn't want the players to have to execute a command to chat in the Rcon environment, I wanted it to be default in my server. Hence, the above script request.

 

@ChalwkI hope you don't mind me playing around with your script bro lol, was trying to learn something new, I've read your License it's MIT so I suppose it's fine, right? If not I won't be publishing the edited script anyway.

 

 

Enclusion and mouseboyx like this

Share this post


Link to post
Share on other sites

I started out learning this stuff from a lot of Chalwk's scripts :) I checked out that admin chat you were talking about.  It looks like the program flow could be changed by removing or changing if statements to send the chat messages to all players through the console regardless of the need for a command to activate it.  However it would need some re-working to handle team chat properly.

28 minutes ago, MXWK said:

@mouseboyxThanks for the explanation bro, honestly I've been playing around with one of @Chalwk's scripts lol. It's Admin Chat. I thought it would be nice to have everyone chat in the Rcon environment so I made my version called Rcon Chat. I basically edited 2 or so lines lol and got the script to show the chat to all the players, even those who are not Admin, which lead to me wanting to complete the project.

 

The only issue was that it wasn't loading automatically, and I didn't want the players to have to execute a command to chat in the Rcon environment, I wanted it to be default in my server. Hence, the above script request.

 

@ChalwkI hope you don't mind me playing around with your script bro lol, was trying to learn something new, I've read your License it's MIT so I suppose it's fine, right? If not I won't be publishing the edited script anyway.

 

 

 

MXWK and Enclusion like this

Share this post


Link to post
Share on other sites

Posted (edited)

@MXWK

 

Kia Ora

I wrote this script for you just now. It makes all chat messages appear in the rcon environment by default.

There is a table at the top of the script that you can edit to your liking.

 

Hope this helps.

Also, I updated Admin Chat with a couple of minor tweaks. You can see all the changes here.

 

Technical Note:

Be warned, this script does not play nice with SAPP's built-in mute system. I do not plan on adding support for this (although it's doable; I've done it before).

Edited by Chalwk
MXWK and Tucker933 like this

Add me on discord: Chalwk#9284
I invite you to join my Halo: CE / Minecraft network (JeriCraft):

https://discord.gg/mEjbFJWpe5

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.