Sign in to follow this  
[M]deLUX

Auto messages - a little error

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... :(

Share this post


Link to post

log in and use "/debugscript 3" without the quotes, it will show the errors. Try changing "local messageNum = 0" into "messageNum = 0" (again w/o the quotes). I don't really know how local works outside of functions, so just try it.

Share this post


Link to post

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

Share this post


Link to post

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.

Share this post


Link to post

the code is fine, just put the timer below the function

@tma local variables outside functions will still act like global variables ;)

Share this post


Link to post

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?

Share this post


Link to post
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

Share this post


Link to post

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 ...

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.