Jump to content

guiCreateStaticImage Question...


HeXo

Recommended Posts

So... I've got this script (Credits to d0NuT)...

function Countdown ( source )
guiSetVisible ( imagecount, false )
imagecount = guiCreateStaticImage(305,119,110,190,"images/3.png",false)
setTimer ( Countdown2, 1000, 1 )
outputChatBox ( "countdown started", getRootElement(), 255, 255, 255, true )
end
function Countdown2 ( source )
guiSetVisible ( imagecount, false )
imagecount = guiCreateStaticImage(305,119,110,190,"images/2.png",false)
setTimer ( Countdown1, 1000, 1 )
end
function Countdown1 ( source )
guiSetVisible ( imagecount, false )
imagecount = guiCreateStaticImage(305,119,110,190,"images/1.png",false)
setTimer ( Countdown0, 1000, 1 )
end
function Countdown0 ( source )
guiSetVisible ( imagecount, false )
imagecount = guiCreateStaticImage(305,119,320,81,"images/go.png",false)
setTimer ( CountdownEnd, 3000, 1 )
end
function CountdownEnd ( source )
guiSetVisible ( imagecount, false )
end
addCommandHandler ( "countdown", Countdown )

How can i make to the guiCreateStaticImage appear not only to the local player, but to the others that are close?

Thanks :D

Link to comment

You can make the command server-side, get the players close to the player who typed the command (you can create colshape and getElementsWithinColshape or get distance between players) and triggerClientEvent for those players.

Link to comment
local imagecount = nil; -- Static image variable!
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function()
imagecount = guiCreateStaticImage(305,119,110,190,"images/3.png",false)
guiSetVisible(imagecount, false);
end);
-- first countdown
function Countdown ( source )
if guiGetVisible(imagecount) == false then
guiSetVisible(imagecount, true);
end
guiStaticImageLoadImage(imagecount, "images/3.png")
setTimer ( Countdown2, 1000, 1 )
outputChatBox ( "countdown started", getRootElement(), 255, 255, 255, true )
end
function Countdown2 ( source )
guiStaticImageLoadImage(imagecount, "images/2.png")
setTimer ( Countdown1, 1000, 1 )
end
function Countdown1 ( source )
guiStaticImageLoadImage(imagecount, "images/1.png")
setTimer ( Countdown0, 1000, 1 )
end
function Countdown0 ( source )
guiStaticImageLoadImage(imagecount, "images/go.png")
setTimer ( CountdownEnd, 3000, 1 )
end
function CountdownEnd ( source )
guiSetVisible ( imagecount, false )
end
addCommandHandler ( "countdown", Countdown )

Now this should work. And listen to 50p, he got a big point there. If you trigger this command. Nobody else but you will see the countdown. Because it is client-side.

Happy coding, and keep smilling!

Link to comment
local imagecount = nil; -- Static image variable!...

Now this should work. And listen to 50p, he got a big point there. If you trigger this command. Nobody else but you will see the countdown. Because it is client-side.

Happy coding, and keep smilling!

I didn't get your point, it was already working the way it was.

So, now i'm trying to create a new countdown script based on that one, but i dont know LUA very well. So, if you guys could help me, i would appreciate that.

function countdownHandler()
local px, py, pz = getElementPosition ( localPlayer )
local tx, ty, tz = getElementPosition( player )
local dist = getDistanceBetweenPoints3D ( px, py, pz, tx, ty, tz )
if dist < 1000 then
--what goes here?
end
end
 
addEvent( "onCountdown", true )
addEventHandler( "onCountdown", getRootElement(), countdownHandler )

Thanks.

Link to comment

If you're planning to make your own countdown script with GUI then I'll give you some advice before you start:

- Use 1 "static image element" and use guiStaticImageLoadImage instead of creating 4 images and showing/hiding them whenever needed.. this doesn't just save bandwidth but also memory where images are stored in

- make 1 function that starts the countdown and counts down by itself.

The less code your client-side script has the more bandwidth you save and shorter download time is.

Take this as a sample:

-- create your image (any image, can be "images/0.png")
IMAGE = guiCreateStaticImage( ........ ); -- put your own arguments here
 
-- call this function to start countdown, eg.  Countdown( 3 );
function Countdown( seconds )
guiStaticImageLoadImage( IMAGE, "images/" .. tostring( seconds ) .. ".png" );
guiSetVisible( IMAGE, true );
if ( seconds > 0 ) then -- do we need to continue countdown?
setTimer( Countdown, 1000, 1, seconds - 1 ); -- call this function again in 1sec. with decreased value of "seconds"
else
       CountdownFinished( )
end
end
 
function CountdownFinished( )
--[[ DO WHATEVER YOU WANT HERE
        WHEN COUNTDOWN ENDS...
        YOU CAN HIDE THE IMAGE ]]
guiSetVisible( IMAGE, false );
-- OR SHOW "GO!" for a second or 2
guiStaticImageLoadImage( IMAGE, "images/go.png" );
setTimer( guiSetVisible, 2000, 1, IMAGE, false );
end

Link to comment
If you're planning to make your own countdown script with GUI then I'll give you some advice before you start:

- Use 1 "static image element" and use guiStaticImageLoadImage instead of creating 4 images and showing/hiding them whenever needed.. this doesn't just save bandwidth but also memory where images are stored in

- make 1 function that starts the countdown and counts down by itself.

The less code your client-side script has the more bandwidth you save and shorter download time is.

Take this as a sample:

-- create your image (any image, can be "images/0.png")...

I Wonder if that is right

addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function()
 
IMAGE = guiCreateStaticImage(305,119,110,190,"images/0.png",false); -- put your own arguments here
 
-- call this function to start countdown, eg.  Countdown( 3 );
function Countdown( seconds )
guiStaticImageLoadImage( IMAGE, "images/" .. tostring( seconds ) .. ".png" );
guiSetVisible( IMAGE, true );
if ( seconds > 0 ) then -- do we need to continue countdown?
setTimer( Countdown, 1000, 1, seconds - 1 ); -- call this function again in 1sec. with decreased value of "seconds"
else
       CountdownFinished( )
end
end
 
function CountdownFinished( )
guiStaticImageLoadImage( IMAGE, "images/go.png" );
setTimer( guiSetVisible, 2000, 1, IMAGE, false );
end
 
addCommandHandler ( "countdown", Countdown )

When i type "/countdown 3" nothing shows up. But if i change the guiCreateStaticImage(305,119,110,190,"images/0.png",false); to guiCreateStaticImage(305,119,110,190,"images/1.png",false); for example, the number 1 shows on the screen and stay there forever.

Thanks for the help!

Link to comment

First of all, I haven't added any commands there. You have to add it yourself.

Second of all, guiCreateStaticImage doesn't hide the image after it's created... You must hide it yourself with guiSetVisible. Besides, image shouldn't be created outside a function (move it inside a function that is called when resource starts or somewhere else).

Last but not least, debug your script.

Link to comment
First of all, I haven't added any commands there. You have to add it yourself.

Second of all, guiCreateStaticImage doesn't hide the image after it's created... You must hide it yourself with guiSetVisible. Besides, image shouldn't be created outside a function (move it inside a function that is called when resource starts or somewhere else).

Last but not least, debug your script.

Sorry. So, now i'm stuck on this:

addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function()
IMAGE = guiCreateStaticImage(305,119,110,190,"images/0.png",false);
function Countdown( seconds )
guiStaticImageLoadImage( IMAGE, "images/" .. tostring( seconds ) .. ".png" );
guiSetVisible( IMAGE, true );
if ( seconds > 0 ) then -- do we need to continue countdown?
setTimer( Countdown, 1000, 1, seconds - 1 ); -- call this function again in 1sec. with decreased value of "seconds"
else
       CountdownFinished( )
end
end
end);
 
function CountdownFinished( )
guiStaticImageLoadImage(305,119,320,81,"images/go.png" );
setTimer( guiSetVisible, 2000, 1, IMAGE, false );
end
addCommandHandler ( "countdown", Countdown )

I Have debugged it, but it don't show any error. Just don't work.

Thanks :D

Link to comment
I never asked you to add Countdown function inside onClientResourceStart... It will still not work because you don't have a command.

Ok. The last line

IMAGE = guiCreateStaticImage(305,119,110,190,"images/0.png",false);
function Countdown( seconds )
guiStaticImageLoadImage( IMAGE, "images/" .. tostring( seconds ) .. ".png" );
guiSetVisible( IMAGE, true );
if ( seconds > 0 ) then -- do we need to continue countdown?
setTimer( Countdown, 1000, 1, seconds - 1 ); -- call this function again in 1sec. with decreased value of "seconds"
else
       CountdownFinished( )
end
end
 
function CountdownFinished( )
guiStaticImageLoadImage(305,119,320,81,"images/go.png" );
setTimer( guiSetVisible, 2000, 1, IMAGE, false );
end
addCommandHandler ( "countdown", Countdown )

doesn't count as a commandhandler?

Link to comment
I never asked you to add Countdown function inside onClientResourceStart... It will still not work because you don't have a command.

Ok. The last line

IMAGE = guiCreateStaticImage(305,119,110,190,"images/0.png",false);
function Countdown( seconds )
guiStaticImageLoadImage( IMAGE, "images/" .. tostring( seconds ) .. ".png" );
guiSetVisible( IMAGE, true );
if ( seconds > 0 ) then -- do we need to continue countdown?
setTimer( Countdown, 1000, 1, seconds - 1 ); -- call this function again in 1sec. with decreased value of "seconds"
else
       CountdownFinished( )
end
end
 
function CountdownFinished( )
guiStaticImageLoadImage(305,119,320,81,"images/go.png" );
setTimer( guiSetVisible, 2000, 1, IMAGE, false );
end
addCommandHandler ( "countdown", Countdown )

doesn't count as a commandhandler?

uhm you forgot a thing:

https://wiki.multitheftauto.com/wiki/AddCommandHandler

The handler function takes one more argument (client-side)

string commandName, [string arg1, string arg2, ...]

So the countdown handler function will be:

function Countdown( commandName, seconds )
guiStaticImageLoadImage( IMAGE, "images/" .. tostring( seconds ) .. ".png" );
guiSetVisible( IMAGE, true );
if ( seconds > 0 ) then -- do we need to continue countdown?
setTimer( Countdown, 1000, 1, seconds - 1 ); -- call this function again in 1sec. with decreased value of "seconds"
else
       CountdownFinished( )
end
end

and use the command like /countdown 3

This should work.

Link to comment
.....

uhm you forgot a thing:

https://wiki.multitheftauto.com/wiki/AddCommandHandler

The handler function takes one more argument (client-side)

string commandName, [string arg1, string arg2, ...]

So the countdown handler function will be:

function Countdown( commandName, seconds )
guiStaticImageLoadImage( IMAGE, "images/" .. tostring( seconds ) .. ".png" );
guiSetVisible( IMAGE, true );
if ( seconds > 0 ) then -- do we need to continue countdown?
setTimer( Countdown, 1000, 1, seconds - 1 ); -- call this function again in 1sec. with decreased value of "seconds"
else
       CountdownFinished( )
end
end

and use the command like /countdown 3

This should work.

LEAVE THE COUNTDOWN FUNCTION ALONE! DO NOT MODIFY IT UNLESS YOU KNOW WHAT YOU'RE DOING! From what I see you don't know what you're doing.

Your code will not work... What's the purpose of Countdown function? It starts countdown and takes only 1 parameter.

I was hoping you can add command handler function yourself easily but it seems to be headache for both of you.

addCommandHandler( "countdown",
function( _, secs )
       secs = tonumber( secs )
if secs then
           Countdown( secs )
end
end
)

Link to comment

LEAVE THE COUNTDOWN FUNCTION ALONE! DO NOT MODIFY IT UNLESS YOU KNOW WHAT YOU'RE DOING! From what I see you don't know what you're doing.

Your code will not work... What's the purpose of Countdown function? It starts countdown and takes only 1 parameter.

I was hoping you can add command handler function yourself easily but it seems to be headache for both of you.

addCommandHandler( "countdown",
function( _, secs )
       secs = tonumber( secs )
if secs then
           Countdown( secs )
end
end
)

Yep, maybe you right or maybe not.

I hate to write extra code! Specially for this. It easier to bind the Countdown function directly to a command. Or am i mistaking here, aint that posible. Yes it is posible. I get everytime a headache from people who write extra code for nothing.

Link to comment

The point of this function is not to add it to command handler but call it from anywhere. You can call this function from onClientMarkerHit for example to start the countdown.. Or you can call this function when player dies to tell him how long until he respawns.

Link to comment
  • 1 month later...

well i tried with this code but doesnt work, just show 1 time the number and i get an error:

IMAGE = guiCreateStaticImage(.40,.50,.2,.2,"images/0.png",true);
 
function Countdown( commandName, seconds )
guiStaticImageLoadImage( IMAGE, "images/" .. tostring( seconds ) .. ".png" );
guiSetVisible( IMAGE, true );
if ( seconds > 0 ) then -- do we need to continue countdown?
setTimer( Countdown, 1000, 1, seconds - 1 ); -- call this function again in 1sec. with decreased value of "seconds"
else
       CountdownFinished( )
end
end
 
 
function CountdownFinished( )
guiStaticImageLoadImage(305,119,320,81,"images/0.png" );
setTimer( guiSetVisible, 2000, 1, IMAGE, false );
end
addCommandHandler ( "countdown", Countdown, true )

i use /countdown 10

and the error is:

[2010-01-02 03:02:42] - ERROR: ...Andreas\mods\deathmatch\resources\liga\aa_client.lua:180: attempt to compare number with string

Line 180: if ( seconds > 0 ) then -- do we need to continue countdown?

Link to comment

Because commands parameters are strings, you have to convert it to an integer :)

There are more problems in that code, I suggest you rewrite it from scratch and think the best way of doing things. For example try not to use -kind of- recursion, and separate command and timer function, as 50p suggested already :) Good luck.

And I wish an happy new year to everyone!

Link to comment

capitanazop, READ ENTIRE TOPIC!

.....

uhm you forgot a thing:

https://wiki.multitheftauto.com/wiki/AddCommandHandler

The handler function takes one more argument (client-side)

string commandName, [string arg1, string arg2, ...]

So the countdown handler function will be:

WRONG CODE!!!

and use the command like /countdown 3

This should work.

LEAVE THE COUNTDOWN FUNCTION ALONE! DO NOT MODIFY IT UNLESS YOU KNOW WHAT YOU'RE DOING! From what I see you don't know what you're doing.

Your code will not work... What's the purpose of Countdown function? It starts countdown and takes only 1 parameter.

I was hoping you can add command handler function yourself easily but it seems to be headache for both of you.

addCommandHandler( "countdown",
function( _, secs )
       secs = tonumber( secs )
if secs then
           Countdown( secs )
end
end
)

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