Jump to content

Auto messages - a little error


[M]deLUX

Recommended Posts

i got a little problem with the following script... supposed to show automessages from time to time...

-- Set the message number 
local messageNum = 0 
  
-- Start the message-timer 
  
setTimer ( showMessages, 5000, 0 ) 
  
-- Show the messages 
function showMessages() 
    outputDebugString("showMessages()" .. messageNum) 
     
    if (messageNum > 5) then 
        messageNum = 0 
    end 
  
    if (messageNum == 0) then 
        outputChatBox("This server is hosted by delux-host.com") 
    end 
     
    if (messageNum == 1) then 
        outputChatBox("Cheating is strictly forbidden on this server and will get you banned!") 
    end 
     
    -- other messages token out for better overview 
     
    messageNum = messageNum + 1 
end 

it just displays nothing... :(

Link to comment

Im not sure if this will get your script working as there maybe something else wrong with it but one thing I notice straight away is this...

setTimer ( showMessages, 5000, 0 ) -- this line is wrong I think,

the "0" at the end is in the place where u are telling the server how many times to perform the function "showMessages" so it will do absolutely nothing, like sending a cheque for £000000.00

change it to 1 . Also I would keep the "local" but i would try moving that line to just under the line begining with "function"

I hope you get this working as I want to use something like this too but hve been too busy to get arround to it :D

Link to comment

setTimer ( showMessages, 5000, 0 ) -- this line is wrong I think,

That's perfectly fine - the "0" param means to continually fire the event.

I tried your code - it's the "local" next to the "messageNum" variable - get rid of it. You want you counter global so it can be accessed inside your message function. The rest of it is fine.

Link to comment

You you're right about the local thing - I didn't realise "local globals" would remain. But you're wrong about the timer thing - setTimer() can forward reference - there's no need for the function to be defined first. In fact, I'm not even sure what's wrong with this code - I copy/pasted into a game mode of mine and it works fine?

Link to comment
But you're wrong about the timer thing - setTimer() can forward reference - there's no need for the function to be defined first.

It's important for the function to be defined first because the first parameter of setTimer is a pointer to that function, if that function does not exist by that time, the timer won't work. Now if you create a timer with a bit of delay (for example onResourceStart, or withing another function) then it doesn't matter if it's below or above since showMessages was already defined.

it's not really clear how you're doing it so I assumed this was your whole script, in that case it wouldn't work :P

Link to comment

Ah OK, that makes sense. Seems a little odd forward references aren't handled. I thought with LUA being "compiled" (turned into byte code), it would sort the references out ? Or is it fully interpreted in MTA ? I was thinking not because I've seen mods with pre-compiled LUA code (e.g. the AlienX freeroam) and so assumed LUA source was first turned into byte code where static references would be resolved. The more you know ...

Link to comment

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.
×
×
  • Create New...