Jump to content

Scripting problem at loops and tables


Zadara

Recommended Posts

Posted

After ask some people at the IRC, what gives not much result. I ask it on the forums.

The problem at the following script is that i go into the food shop markers of the ammu markers the addEventHandler for onMarkerHit doesn't work. Does anyone know why?

Thanks,

function onResStart()
AMMU1 = createMarker(296.03073120117, -38.204479217529, 1000.515625, "cylinder", 1)
AMMU2 = createMarker(289.80169677734, -109.62482452393, 1000.515625, "cylinder", 1)
 
FDBURG1 = createMarker(375.91638183594, -67.679695129395, 1000.5151367188, "cylinder", 1)
FDBURG2 = createMarker(375.91638183594, -67.679695129395, 1000.5151367188, "cylinder", 1)
FDBURG3 = createMarker(375.91638183594, -67.679695129395, 1000.5151367188, "cylinder", 1)
 
FDCHICK1 = createMarker(368.56079101563, -6.4645309448242, 1000.8515625, "cylinder", 1)
FDCHICK2 = createMarker(368.56079101563, -6.4645309448242, 1000.8515625, "cylinder", 1)
FDCHICK3 = createMarker(368.56079101563, -6.4645309448242, 1000.8515625, "cylinder", 1)
 
FDPIZZA1 = createMarker(373.77386474609, -119.16807556152, 1000.4921875, "cylinder", 1)
FDPIZZA2 = createMarker(373.77386474609, -119.16807556152, 1000.4921875, "cylinder", 1)
 
   setElementInterior(AMMU1, 1)
setElementInterior(AMMU2, 6)
setElementDimension(AMMU1, 1)
setElementDimension(AMMU2, 5)
 
setElementInterior(FDBURG1, 10)
setElementInterior(FDBURG2, 10)
setElementInterior(FDBURG3, 10)
setElementDimension(FDBURG1, 2)
setElementDimension(FDBURG2, 3)
setElementDimension(FDBURG3, 4)
 
setElementInterior(FDCHICK1, 9)
setElementInterior(FDCHICK2, 9)
setElementInterior(FDCHICK3, 9)
setElementDimension(FDCHICK1, 2)
setElementDimension(FDCHICK2, 6)
setElementDimension(FDCHICK3, 7)
 
setElementInterior(FDPIZZA1, 5)
setElementInterior(FDPIZZA2, 5)
setElementDimension(FDPIZZA1, 6)
setElementDimension(FDPIZZA2, 7)
 
ammuTable = {AMMU1, AMMU2}
foodTable = {FDBURG1, FDBURG2, FDBURG3, FDCHICK1, FDCHICK2, FDCHICK3, FDPIZZA1, FDPIZZA2}
for index, ammuMarker in next, ammuTable do
    addEventHandler("onMarkerHit", ammuMarker, onAmmuMarkerHit)
end
 
for index, foodMarker in next, foodTable do
	addEventHandler("onMarkerHit", foodMarker, onFoodMarkerHit)
end
end
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), onResStart)
 
function onAmmuMarkerHit(hitPlayer, matchingDimension)
outputChatBox("Buy your weapon!")
end
 
function onFoodMarkerHit(hitPlayer, matchingDimension)
outputChatBox("Buy your food!")
end

Posted
for index, ammuMarker in next, ammuTable do
addEventHandler("onMarkerHit", ammuMarker, onAmmuMarkerHit)
end
 
for index, foodMarker in next, foodTable do
addEventHandler("onMarkerHit", foodMarker, onFoodMarkerHit)
end

Where is the variable 'next'?

And put in all the for loops the next code:

outputDebugString("Execuded for loop");

And do that allways in your code, it helps finding things you overlooked!

Posted
...

Where is the variable 'next'?

...!

It's not a variable. It's an iterator function that is used in pairs() function. You can do either,

for key, value in next, tab do

or

for key, value in pairs( tab ) do

This should work:

-- ... Your code above
addEventHandler( "onMarkerHit", getRootElement(),
function( )
for k,marker in pairs( foodTable ) do -- go through all food markers
if source == marker then -- and find out if hit marker (source) is one of markers in foodTable
-- OK, you hit a food marker
break; -- break the loop because you found what you wanted...
end
end
end
)

Posted

And sorry to be repetitive, but you'll find it easier long-term if you specify markers (and any other elements) in a map file. You can then use the map editor and everything is generally much better!

Posted
And sorry to be repetitive, but you'll find it easier long-term if you specify markers (and any other elements) in a map file. You can then use the map editor and everything is generally much better!

How do you mean? I don't get it... :oops:

Posted
And sorry to be repetitive, but you'll find it easier long-term if you specify markers (and any other elements) in a map file. You can then use the map editor and everything is generally much better!

How do you mean? I don't get it... :oops:

He's trying to tell you to use the map files instead of creating markers inside your script.

http://development.mtasa.com/index.php? ... ement_tree

http://development.mtasa.com/index.php? ... ment_Types

Posted
And sorry to be repetitive, but you'll find it easier long-term if you specify markers (and any other elements) in a map file. You can then use the map editor and everything is generally much better!

How do you mean? I don't get it... :oops:

He's trying to tell you to use the map files instead of creating markers inside your script.

http://development.mtasa.com/index.php? ... ement_tree

http://development.mtasa.com/index.php? ... ment_Types

How can i get elements that are in a .map file into a table then? :?

Posted

getElementsByType(elementType,getResourceMapRootElement(getThisResource(),mapName))

Will get exactly the wanted elements in specified map file, usually just getElementsByType(elementType,getResourceRootElement(getThisResource())) works too

Posted

Although it is useful not to limit yourself to just your own resource - that allows other people to extend your code easily.

Posted
Although it is useful not to limit yourself to just your own resource - that allows other people to extend your code easily.

Its not gonna be a open script, its for a pretty big server... And its easy to figure out, where where is. :wink:

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